Greasy Fork is available in English.
Automatic image downloader for Bing Image Creator.
当前为
// ==UserScript==
// @name Bing Image Creator auto-download
// @namespace http://tampermonkey.net/
// @version Alpha-v1
// @license refer to the github link and ask them, I guess
// @description Automatic image downloader for Bing Image Creator.
// @match https://www.bing.com/images/create*?*autosavetimer=*
// @grant none
// @require http://code.jquery.com/jquery-3.4.1.min.js
// ==/UserScript==
// I just pasted this together from things found scattered around the internet. Primarily: https://github.com/Emperorlou/MidJourneyTools
//
// To enable periodic downloading of newly-created images, go to a 'recent creations' page, and add "&autosavetimer=60" to the URL.
//
// If the browser prompts before every download, that's a browser setting you can change. Maybe this plugin needs to be updated to prefer GM_Download(). I don't have time to do that, though. Please somebody else take ownership and implement this.
(function() {
'use strict';
const downloadables = "img[src$='&pid=ImgGn']";
function get_download_url(img) {
const src = img.attributes['src'].nodeValue;
return src.replace(/\?.*$/, "?pid=ImgGn");
}
function get_filename(img, src, ref) {
var url = new URL(src);
var refurl = new URL(ref);
var src_filename = url.pathname.split('/').pop();
var ref_path = refurl.pathname.split('/');
while (ref_path.length && ref_path.shift() != 'create')
;
var pageid = (ref_path.length >= 2 && ref_path[1]) || refurl.searchParams.get('id') || "";
var desc = (ref_path.length >= 2 && ref_path[0]) || refurl.searchParams.get('q') || "";
//var desc = img.attr("alt", "");
console.log("page id:", pageid, " src_filename:", src_filename, " description:", desc);
return src_filename + "_" + pageid + "_" + desc + ".jpg";
}
function reload() {
$("#girrcc").load(location.href + " #girrcc a");
//window.location.reload(true);
}
function find_href(elem) {
while (elem) {
if (elem.hasAttribute('href')) return elem.href;
elem = elem.parentElement;
}
return null;
}
$(document).ready(() => {
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.saved_image { border: 3px green dashed; }';
document.head.appendChild(style);
setInterval(() => {
window.renderSavedImageIndicators();
autoSaveNextImage();
}, 500);
launchInactivityTimer();
});
function launchInactivityTimer() {
var timer;
var params = new URLSearchParams(window.location.search);
var timeout = params.get('autosavetimer') || 60;
window.onload = resetTimer;
document.onmousemove = resetTimer;
document.onkeydown = resetTimer;
function resetTimer() {
clearInterval(timer);
timer = setInterval(reload, timeout * 1000);
}
resetTimer();
}
function downloadFile(url, filename, referrer) {
// TODO: Prefer GM_Download(), with fallback...
//const download = GM_download({
// url: url,
// name: filename,
// saveAs: false,
// conflictAction: "uniquify",
// onload: function () {
// setUrlSaved(url);
// activeDownloads--;
// }
//});
fetch(url, { method: 'get' })
.then(res => res.blob())
.then(res => {
var link = document.createElement('a');
const href = URL.createObjectURL(res);
link.href = href;
link.download = filename;
link.target = '_blank';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);
// TODO: error checking
setUrlSaved(url);
});
};
function autoSaveNextImage() {
// find thumbnails
const allImages = $($(downloadables).get().reverse());
for(const img of allImages) {
const src = get_download_url(img);
const ref = find_href(img) || "https://www.example.com/";
if (src && isUrlSaved(src) == false) {
const filename = get_filename(img, src, ref);
if (filename) {
downloadFile(src, filename, ref);
}
document.dispatchEvent(new Event("mousemove"));
// TODO: allow concurrent downloads up to finite limit, not just 1.
break;
}
}
}
window.renderSavedImageIndicators = () => {
const allImages = $(downloadables);
for(const img of allImages) {
const src = get_download_url(img);
if (src && isUrlSaved(src))
$(img).addClass("saved_image");
}
}
function setUrlSaved(src) {
localStorage.setItem("savedImage-" + src, true);
}
function isUrlSaved(src) {
return localStorage.getItem("savedImage-" + src) === "true" ? true : false;
}
})();