Bing Image Creator Image and Prompt Downloader

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

  1. // ==UserScript==
  2. // @name Bing Image Creator Image and Prompt Downloader
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.6
  5. // @description Download full-size images from Bing and make the filename the initial image prompt
  6. // @author quackfiend
  7. // @match https://www.bing.com/images/create*
  8. // @grant GM_download
  9. // @license GNU GPLv3
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to sanitize text to be used in filenames
  16. function sanitizeFilename(text) {
  17. return text.replace(/[^a-z0-9]/gi, '_').toLowerCase();
  18. }
  19.  
  20. // Function to modify the URL for the full-size image and download it
  21. function downloadFullSizeImage(thumbnail, index) {
  22. let fullSizeImageUrl = thumbnail.src.replace(/w=\d+/, 'w=1024').replace(/h=\d+/, 'h=1024');
  23. let promptText = sanitizeFilename(thumbnail.alt || 'image');
  24. let filename = promptText + '.jpg'; // Adding index to the filename
  25. GM_download({
  26. url: fullSizeImageUrl,
  27. name: filename
  28. });
  29. }
  30.  
  31. // Function to add a centered save button to the page
  32. function addSaveButton() {
  33. const saveButton = document.createElement('button');
  34. saveButton.textContent = 'Save Image(s)';
  35. saveButton.style.position = 'fixed';
  36. saveButton.style.top = '5px';
  37. saveButton.style.left = '50%';
  38. saveButton.style.transform = 'translateX(-50%)';
  39. saveButton.style.zIndex = '1000';
  40. saveButton.style.padding = '10px 20px';
  41. saveButton.style.fontSize = '16px';
  42. saveButton.style.cursor = 'pointer';
  43. saveButton.addEventListener('click', function() {
  44. const thumbnails = document.querySelectorAll('img.mimg, img.gir_mmimg'); // Targeting both selectors
  45. thumbnails.forEach((thumbnail, index) => {
  46. downloadFullSizeImage(thumbnail, index);
  47. });
  48. });
  49. document.body.appendChild(saveButton);
  50. }
  51.  
  52. // Add the centered save button to the page
  53. addSaveButton();
  54. })();