Tumblr Images to HD Redirector

Automatically promotes Tumblr image links to raw HD versions

目前为 2017-08-04 提交的版本。查看 最新版本

// ==UserScript==
// @name         Tumblr Images to HD Redirector
// @namespace    TumblrImgReszr
// @description  Automatically promotes Tumblr image links to raw HD versions
// @version      1.0
// @author       Kai Krause <[email protected]>
// @match        http://*.media.tumblr.com/*
// @match        https://*.media.tumblr.com/*
// @grant        GM_xmlhttpRequest
// @connect      media.tumblr.com
// @run-at       document-start
// ==/UserScript==

// Check if this page contains a single image whose source is also the location.
var image = document.getElementsByTagName('img')[0];
var loc = location.toString();
var imageType = loc.match(/[^.]*$/);
var checkSize;

if (image && image.getAttribute('src') == loc) {
	// Do not create a looped redirect by redirecting already HD images to itself
	if (loc.includes('_raw') || loc.includes('_1280')) return;
	
	// If the URL is HTTP, change it to HTTPS
	if (!loc.startsWith('https://')) {
		loc = loc.replace(/^http/, 'https');
	}
	
	checkSize = function(i) {
		// Define which patterns to check
		var i = i || 0;
		var imageSizes = ['raw', '1280'];
		if (i > imageSizes.length) return;
		
		// Create the HD image url pattern
		var newLoc = loc;
		if (imageSizes[i] == 'raw') {
			newLoc = newLoc.replace(/[^/]*media.tumblr.com/, 'media.tumblr.com');
			newLoc = newLoc.replace(/[^_]*$/, imageSizes[i] + '.' + imageType[0]);
		} else {
			newLoc = newLoc.replace(/[^_]*$/, imageSizes[i] + '.' + imageType[0]);
		}
		
		// Check that the HD image exists, then redirect to it
		GM_xmlhttpRequest({
			url: newLoc,
			method: 'HEAD',
			onload: function(response) {
				if (response.status != '200') {
					checkSize(i+1);
					return;
				}
				window.location = newLoc;
			}
		});
	};
	checkSize();
}