Tumblr Images to HD Redirector

Automatically promotes Tumblr image links to raw HD versions

目前为 2017-09-07 提交的版本。查看 最新版本

// ==UserScript==
// @name         Tumblr Images to HD Redirector
// @namespace    TumblrImgReszr
// @description  Automatically promotes Tumblr image links to raw HD versions
// @version      2.0
// @author       Kai Krause <[email protected]>
// @include      /^https?://.+\.media\.tumblr\.com.+?(jpe?g|png|bmp|gif)/
// @grant        GM_xmlhttpRequest
// @connect      media.tumblr.com
// @run-at       document-start
// ==/UserScript==


var imageSizes = ['raw', '1280', '540', '500', '400', '250', '100'];

function checkSize(i) {
	var i = i || 0;
	var loc = location.origin + location.pathname;
	var imageSize = loc.match(/\d+(?=.\w+$)/)[0];
	var imageType = loc.match(/[^.]*$/)[0];

	// Do not redirect if already
	if (i > imageSizes.length || imageType == "gif" && parseInt(imageSize) >= 540 || parseInt(imageSize) >= parseInt(imageSizes[i]) || loc.includes('_raw')) {
		document.body.style.cursor = "";
		return;
	}

	document.body.style.cursor = "progress"; // Show loading cursor

	// Create the HD image url
	if (imageSizes[i] == 'raw') {
		loc = loc.replace(/[^/]*media.tumblr.com/, 'media.tumblr.com');
		loc = loc.replace(imageSize, imageSizes[i]);
	} else {
		loc = loc.replace(imageSize, imageSizes[i]);
	}
	
	// If the URL is HTTP, change it to HTTPS
	if (!loc.startsWith('https://')) {
		loc = loc.replace(/^http/, 'https');
	}
	
	// Check that the HD image exists, then redirect to it
	GM_xmlhttpRequest({
		url: loc,
		method: 'HEAD',
		onload: function(response) {
			if (response.status == '200') {
				window.location = loc;
			} else {
				checkSize(i+1);
			}
		}
	});
}
checkSize();