// ==UserScript==
// @name Download high-res waifus for free!
// @include /^https?://(img4\.)?gelbooru.com/.*/
// @grant GM_download
// @grant window.close
// ==/UserScript==
function download (targeturl) {
var currentURL = window.location.href
frame = {
method: "GET",
url: targeturl,
headers: {
"User-Agent": navigator.userAgent,
"Access-Control-Allow-Origin": currentURL,
"Access-Control-Request-Method": "GET",
"Accept": "*/*",
"Referer": currentURL,
"Accept-Language": "en-US,en;q=0.5"
},
name: targeturl.substring(targeturl.lastIndexOf("/")+1),
onload: function(response) {
window.close();
}
};
GM_download(frame);
}
//main
//download source image from img4.gelbooru.com/images/.* page,
//try looking for "Original image" link otherwise
const regex = /^https?:\/\/img4\.gelbooru\.com\/images\/.*/;
let pathToHighresImage;
if ((pathToHighresImage = regex.exec(window.location.href)) !== null) {
download(pathToHighresImage[0]);
} else {
const targetIdent = "Original image";
var target;
var aTags = document.getElementsByTagName("a");
for (var i=0; i<aTags.length; i++) {
if (aTags[i].textContent == targetIdent) {
target = aTags[i];
download(target.href);
break;
}
}
}