Skip to content

Instantly share code, notes, and snippets.

@dvingerh
Last active January 5, 2022 13:54
Show Gist options
  • Save dvingerh/2e7fe15d06786d77cb75aa0e107500ea to your computer and use it in GitHub Desktop.
Save dvingerh/2e7fe15d06786d77cb75aa0e107500ea to your computer and use it in GitHub Desktop.
Twitter Image Helper modifies image sources so that the best available image quality is always being used in tweets. Also adds a download button for images in tweets and redirects direct image links from Twitter to show the largest available size. Also: No more jpg-orig etc. image extensions when downloading!
// ==UserScript==
// @name Twitter Image Helper v2
// @namespace Twitter Image Helper v2
// @version 0.1
// @description Twitter Image Helper modifies image sources so that the best available image quality is always being used in tweets. Also adds a download button for images in tweets and redirects direct image links from Twitter to show the largest available size. Also: No more jpg-orig etc. image extensions when downloading!
// @author You
// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @require https://raw.githubusercontent.com/pie6k/jquery.initialize/master/jquery.initialize.min.js
// @include *pbs.twimg.com/media/*
// @include *twitter.com*
// ==/UserScript==
this.$ = this.jQuery = jQuery.noConflict(true);
var currentUrl = window.location.href;
var mediaIdRegex = /(?<=\/)([a-zA-Z0-9\_\-]){15}/;
var mediaSizeRegex = /(name=)(.*$)/;
var mediaFormatRegex = /(format=)([aA-zZ]{3})/;
var linkHtml = '<a target="_blank" href="$LINK" style="font-family: Arial, Helvetica, sans-serif; z-index: 9999; position:absolute; margin: 8px; height:15px; width: $LWIDTH; text-align: center; line-height:15px; padding: 3px 10px; border-radius: 3px; left: 0; right: 0; margin-left: auto; margin-right: auto; margin-top: 8px; cursor:pointer; background-color: rgba(20,20,20,0.75); color: white; font-size: 11px; text-decoration: none;" class="download-button" download>Download ($IWx$IH)</a>';
function getMeta(url){
var r = $.Deferred();
$('<img/>').attr('src', url).load(function(){
var s = {w:this.width, h:this.height};
r.resolve(s)
});
return r;
}
var getOriginalSize = function(link) {
if (!link.endsWith("orig")) {
var mediaId = link.match(mediaIdRegex)[0];
var mediaFormat;
console.log("Got media Id: " + mediaId);
if (link.indexOf("format=") == -1) {
console.log("No media format found, assuming link ends in extension.");
mediaFormat = link.split(".").pop().split(":")[0];
console.log("Got media format: " + mediaFormat);
}
else {
mediaFormat = link.match(mediaFormatRegex)[2];
console.log("Got media format: " + mediaFormat);
}
return "https://pbs.twimg.com/media/" + mediaId + "?format=" + mediaFormat + "&name=orig";
}
};
if (currentUrl.indexOf("pbs.twimg.com/media/") > 0){
if (!currentUrl.endsWith("orig")) {
window.location.href = getOriginalSize(currentUrl);
}
}
else {
$("img").initialize(function() {
var imgLink = $(this).attr('src');
if (imgLink.indexOf("pbs.twimg.com/media") != -1) {
var parentEl = $(this).parent().parent().parent();
var newImgLink = getOriginalSize(imgLink);
if (parentEl.width() < 150) {
$(parentEl).prepend(linkHtml.replace("$LINK", newImgLink).replace("($IWx$IH)", "").replace("$LWIDTH", "50px"));
}
else {
getMeta(newImgLink).done(function(imgDims){
$(parentEl).prepend(linkHtml.replace("$LINK", newImgLink).replace("$IW", imgDims.w).replace("$IH", imgDims.h).replace("$LWIDTH", "115px"));
});
}
}
});
$('body').on('click', '.download-button', function(event) {
event.stopPropagation();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment