Bing Image Creator Image and Prompt Downloader

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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();
})();