Bing Image Creator Image and Prompt Downloader

Download full-size images from Bing and make the filename the initial image prompt

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Bing Image Creator Image and Prompt Downloader
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Download full-size images from Bing and make the filename the initial image prompt
// @author       quackfiend
// @match        https://www.bing.com/images/create*
// @grant        GM_download
// @license      GNU GPLv3
// ==/UserScript==

(function() {
    'use strict';

    // Function to sanitize text to be used in filenames
    function sanitizeFilename(text) {
        return text.replace(/[^a-z0-9]/gi, '_').toLowerCase();
    }

    // Function to modify the URL for the full-size image and download it
    function downloadFullSizeImage(thumbnail, index) {
        let fullSizeImageUrl = thumbnail.src.replace(/w=\d+/, 'w=1024').replace(/h=\d+/, 'h=1024');
        let promptText = sanitizeFilename(thumbnail.alt || 'image');
        let filename = promptText + '.jpg'; // Adding index to the filename
        GM_download({
            url: fullSizeImageUrl,
            name: filename
        });
    }

    // Function to add a centered save button to the page
    function addSaveButton() {
        const saveButton = document.createElement('button');
        saveButton.textContent = 'Save Image(s)';
        saveButton.style.position = 'fixed';
        saveButton.style.top = '5px';
        saveButton.style.left = '50%';
        saveButton.style.transform = 'translateX(-50%)';
        saveButton.style.zIndex = '1000';
        saveButton.style.padding = '10px 20px';
        saveButton.style.fontSize = '16px';
        saveButton.style.cursor = 'pointer';
        saveButton.addEventListener('click', function() {
            const thumbnails = document.querySelectorAll('img.mimg, img.gir_mmimg'); // Targeting both selectors
            thumbnails.forEach((thumbnail, index) => {
                downloadFullSizeImage(thumbnail, index);
            });
        });
        document.body.appendChild(saveButton);
    }

    // Add the centered save button to the page
    addSaveButton();
})();