Skip to content

Instantly share code, notes, and snippets.

@alashow
Last active March 19, 2018 01:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alashow/104342730a519239cab817822bd163a3 to your computer and use it in GitHub Desktop.
Save alashow/104342730a519239cab817822bd163a3 to your computer and use it in GitHub Desktop.
Force tumblr posts use hight quality images. Tempermonkey user script.
// ==UserScript==
// @name Tumblr image size converter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Force tumblr post photos use high quality
// @author Alashov Berkeli
// @include https://www.tumblr.com/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
// @grant none
// ==/UserScript==
(function() {
'use strict';
String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
function isString(str){
return typeof str === 'string' || str instanceof String;
}
var sizes = ['_540.', '_500.', '_400.', '_250.', '_100.' ];
function convertSizes(sel){
var j = jQuery;
var images = j(sel);
j.each(images, function(e){
var e = j(images[e]);
var oldSrc = e.attr("src");
if (oldSrc !== undefined && isString(oldSrc)) {
var isGif = oldSrc.endsWith(".gif");
if (! isGif) {
for (var i = sizes.length - 1; i >= 0; i--) {
if (oldSrc.contains(sizes[i])){
var newSrc = oldSrc.replaceAll(sizes[i], "_1280.");
e.attr("src", newSrc);
console.log("Converted to big size: " + newSrc);
} else {
console.log("Didn't convert because it has no size or already converted: " + oldSrc);
}
}
}
}
});
}
convertSizes(".post_container img");
jQuery("img").live("mouseover", function(e){
convertSizes(".post_container img");
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment