Skip to content

Instantly share code, notes, and snippets.

@oxguy3
Last active December 5, 2018 17:23
Show Gist options
  • Save oxguy3/fcf6fe961ff6a467caeb941708f5b433 to your computer and use it in GitHub Desktop.
Save oxguy3/fcf6fe961ff6a467caeb941708f5b433 to your computer and use it in GitHub Desktop.
Finds the biggest possible version of any image
function runParsers() {
var parsers = [
parseBandcamp,
parseFlickr,
parseSportsEngine,
parseTwitter,
parseWikimedia,
parseYouTube,
];
var result = null;
parsers.forEach(function(parser) {
if (result !== null) return;
console.log("Testing "+parser.name.substring(5)+"...");
result = parser(window.location);
});
if (result !== null) {
window.location = result;
} else {
alert("Failed to embiggen! :(");
}
}
function parseBandcamp(location) {
if (/f\d\.bcbits\.com/gi.test(location.hostname)) {
var re = /^\/img\/(a?\d{10})_\d+.jpg$/ig;
var result = re.exec(location.pathname);
if (result !== null) {
var imgId = result[1];
return "https://f1.bcbits.com/img/"+imgId+"_0.jpg";
}
}
return null;
}
function parseFlickr(location) {
if (/farm\d\.staticflickr\.com/gi.test(location.hostname)) {
var re = /^\/\d{4}\/(\d{1,11})_[0-9a-f]{10}_\w(?:_d)?.jpg$/ig;
var result = re.exec(location.pathname);
if (result !== null) {
var photoId = result[1];
return "https://flickr.com/photo.gne?id="+photoId;
}
}
return null;
}
function parseSportsEngine(location) {
var hostnames = [
"assets.ngin.com",
"cdn1.sportngin.com",
"cdn2.sportngin.com",
"cdn3.sportngin.com",
"cdn4.sportngin.com",
// "assets.ngin.com.s3.amazonaws.com", // not actually used in the real world
];
if (hostnames.indexOf(location.hostname) != -1) {
var re = /^\/attachments\/photo\/([0-9a-f\-]{9,}\/.*?)_(?:large|medium|small|thumb)(\.\w+)$/ig;
var result = re.exec(location.pathname);
if (result !== null) {
var fileIdAndName = result[1];
var fileExt = result[2];
return "https://"+location.hostname+"/attachments/photo/"+fileIdAndName+fileExt;
}
}
return null;
}
function parseTwitter(location) {
if (location.hostname == "pbs.twimg.com") {
var re = /^\/media\/([\w\d_]+)\.(\w+)(?:\:\w+)?$/ig;
var result = re.exec(location.pathname);
if (result !== null) {
var imageId = result[1];
var fileExt = result[2];
return "https://pbs.twimg.com/media/"+imageId+"."+fileExt+":large";
}
}
return null;
}
function parseWikimedia(location) {
if (location.hostname == "upload.wikimedia.org") {
var re = /^\/(\w+)\/(\w+)(?:\/thumb)?\/[0-9a-f]\/[0-9a-f]{2}\/([^#<>\[\]\|\{\}\/]+)(?:\/(?:page\d+-)?\d+px-[^#<>\[\]\|\{\}\/]+)?$/ig;
var result = re.exec(location.pathname);
if (result !== null) {
var website = result[1];
var language = result[2];
var filename = result[3];
// special case for .wikimedia.org domains
var specialLanguages = [ "commons", "species", "meta" ];
if (website == "wikipedia" && specialLanguages.indexOf(language) != -1) {
website = "wikimedia";
}
return "https://"+language+"."+website+".org/wiki/File:"+filename;
}
}
return null;
}
function parseYouTube(location) {
if (location.hostname == "i.ytimg.com") {
var re = /^\/vi\/([^"&?/ ]{11})\/\w+.jpg$/ig;
var result = re.exec(location.pathname);
if (result !== null) {
var videoId = result[1];
return "https://i.ytimg.com/vi/"+videoId+"/maxresdefault.jpg";
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment