Last active
May 17, 2021 19:29
-
-
Save EvgenyOrekhov/d6b21fb35843c606bbd9 to your computer and use it in GitHub Desktop.
A bookmarklet for downloading currently viewed Flickr image in the biggest available size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint browser, maxlen: 80 */ | |
/*global DOMParser, fetch */ | |
(function () { | |
"use strict"; | |
var originalSizeLink = document.querySelector(".Original > a"); | |
var canonicalUrl = document.querySelector("#canonicalurl").href; | |
var biggestSizeUrl = canonicalUrl + "/sizes/k/"; | |
var secondBiggestSizeUrl = canonicalUrl + "/sizes/h/"; | |
function downloadImage(href) { | |
var a = document.createElement("a"); | |
a.href = href; | |
a.download = document.title.split(" |")[0] + ".jpg"; | |
document.body.appendChild(a); | |
a.click(); | |
} | |
function getHrefAndDownloadImage(responseText) { | |
var href = new DOMParser() | |
.parseFromString(responseText, "text/html") | |
.querySelector("#allsizes-photo > img") | |
.src; | |
downloadImage(href); | |
} | |
if (originalSizeLink) { | |
return originalSizeLink.click(); | |
} | |
fetch(biggestSizeUrl) | |
.then(function (response) { | |
return (response.ok && response.url === biggestSizeUrl) | |
? response | |
: fetch(secondBiggestSizeUrl); | |
}) | |
.then(function (response) { | |
return response.text(); | |
}) | |
.then(getHrefAndDownloadImage); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to add it to Chrome?
I created a new bookmark and edited the URL as
javascript:your code
But nothing happen when clicked!!