Greasy Fork

Greasy Fork is available in English.

Bing Image Creator auto-download

Automatic image downloader for Bing Image Creator.

当前为 2023-08-10 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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;
    }

})();