推特圖片自動 orig

推特圖片自動 orig,請先將圖片另開啟新分頁才會有效

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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
// @license      MIT
// ==/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`
		}

	})();

})();