Advertisement
Guest User

Scrape and Save Image and Text

a guest
May 30th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name Scrape and Save Image and Text
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Scrape image and text from a specific page and save them
  6. // @author You
  7. // @match https://civitai.com/images/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Функция для сохранения файла
  15. function saveFile(content, fileName, contentType) {
  16. const blob = new Blob([content], { type: contentType });
  17. const url = URL.createObjectURL(blob);
  18. const a = document.createElement('a');
  19. a.href = url;
  20. a.download = fileName;
  21. document.body.appendChild(a);
  22. a.click();
  23. document.body.removeChild(a);
  24. URL.revokeObjectURL(url);
  25. }
  26.  
  27. // Функция для извлечения текста из блока
  28. function extractText(selector) {
  29. const element = document.querySelector(selector);
  30. return element ? element.innerText.trim() : '';
  31. }
  32.  
  33. // Функция для обрезки имени файла до 25 символов
  34. function truncateFileName(fileName) {
  35. return fileName.length > 25 ? fileName.substring(0, 25) : fileName;
  36. }
  37.  
  38. // Функция для скачивания изображения
  39. function downloadImage(imgElement, savePath) {
  40. const imgSrc = imgElement.src;
  41. let imgName = imgSrc.split('/').pop();
  42. imgName = truncateFileName(imgName);
  43.  
  44. fetch(imgSrc)
  45. .then(response => response.blob())
  46. .then(blob => {
  47. const url = URL.createObjectURL(blob);
  48. const a = document.createElement('a');
  49. a.href = url;
  50. a.download = imgName;
  51. document.body.appendChild(a);
  52. a.click();
  53. document.body.removeChild(a);
  54. URL.revokeObjectURL(url);
  55.  
  56. // Сохраняем текстовый файл
  57. const text = extractText('.mantine-mp7k2v.mantine-Card-root');
  58. saveFile(text, `${imgName}.txt`, 'text/plain');
  59. });
  60. }
  61.  
  62. // Добавляем кнопку для скачивания
  63. const button = document.createElement('button');
  64. button.innerText = 'Dnld Img&Txt';
  65. button.style.position = 'fixed';
  66. button.style.top = '30px';
  67. button.style.right = '450px';
  68. button.style.zIndex = '1000';
  69. button.style.padding = '10px';
  70. button.style.backgroundColor = 'blue';
  71. button.style.color = 'white';
  72.  
  73. button.addEventListener('click', () => {
  74. const imgElement = document.querySelector('.EdgeImage_image__iH4_q');
  75. if (imgElement) {
  76. const { pathname } = window.location;
  77. const defaultPath = pathname.split('/').slice(0, -1).join('/');
  78.  
  79. const { value: savePath } = window.prompt('Enter the save path (default is the current page path):', defaultPath);
  80. downloadImage(imgElement, savePath || defaultPath);
  81. } else {
  82. alert('Image not found!');
  83. }
  84. });
  85.  
  86. document.body.appendChild(button);
  87. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement