今天終於受夠了 webp,所以重寫了之前寫的轉 orig 程式。
這樣 webp 就可以自動轉換成 png 或 jpg 了
// ==UserScript==
// @name 推特圖片自動 orig
// @namespace https://www.plurk.com/SpyMomiji
// @version 1.2
// @description 推特圖片自動 orig,請先將圖片另開啟新分頁才會有效
// @author SpyMomiji
// @match ^https://pbs.twimg.com/media/*
// @run-at document-start
// @noframes true
// @grant none
// ==/UserScript==
(function() {
'use strict';
if( location.hash.startsWith == '#r' ) return;
var [_,imageID,spliter,args] = location.href.substr(28).match(/^([\w_-]*)(\?|\.)(.*)/);
var format;
var isOrig = false;
if(!imageID) return;
if( spliter == '?' ){
args.split('&').forEach((v)=>{
var [name,value] = v.split('=');
switch(name){
case 'format': format = value; break;
case 'name': isOrig = value == 'orig'; break;
}
})
if(!format) return;
} else {
var splited = args.match(/^(jpg|jpeg|png|webp)(:orig)?/i)
if(!splited) return;
format = splited[1].toLowerCase();
isOrig = Boolean(splited[2]);
}
if(['jpg','png','jpeg'].includes(format)&&isOrig) return;
window.stop();
var tryFormat = format=='webp' ? ['jpg','png','jpeg','webp'] : [format];
async function sendRequest(url){
return new Promise(function(reslove,reject){
var hr = new XMLHttpRequest();
hr.onreadystatechange = function(state){
if(hr.readyState != XMLHttpRequest.DONE) return;
reslove( hr.status == 200 && hr.getResponseHeader('Content-Length') !== '0' );
}
hr.open("HEAD", url, true);
hr.send();
})
}
(async function(){
var availableURL;
if(
format=='webp' &&(
(await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.jpg:orig`
)) ||
(await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.png:orig`
)) ||
(await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.jpeg:orig`
)) ||
(await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.png`
)) ||
(await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.jpg`
)) ||
(await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.jpeg`
)) ||
(await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.webp:orig`
)) ||
(await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.webp`
))
)){
location.href = availableURL + '#r';
}
else if(
await sendRequest(
availableURL = `https://pbs.twimg.com/media/${imageID}.${format}:orig`
)
){
location.href = availableURL + '#r';
}
else {
location.href = `https://pbs.twimg.com/media/${imageID}.${format}#r`
}
})();
})();
我是知道還有更高級的寫法,但想想也沒必要w
1.2 (2023-08-13)
修正導向後的網址為空白內容的問題 (以往應該會回傳 404,但這次卻回傳 200)