Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Scrape and Save Image and Text
- // @namespace http://tampermonkey.net/
- // @version 1.1
- // @description Scrape image and text from a specific page and save them
- // @author You
- // @match https://civitai.com/images/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- // Функция для сохранения файла
- function saveFile(content, fileName, contentType) {
- const blob = new Blob([content], { type: contentType });
- const url = URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.href = url;
- a.download = fileName;
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- URL.revokeObjectURL(url);
- }
- // Функция для извлечения текста из блока
- function extractText(selector) {
- const element = document.querySelector(selector);
- return element ? element.innerText.trim() : '';
- }
- // Функция для обрезки имени файла до 25 символов
- function truncateFileName(fileName) {
- return fileName.length > 25 ? fileName.substring(0, 25) : fileName;
- }
- // Функция для скачивания изображения
- function downloadImage(imgElement, savePath) {
- const imgSrc = imgElement.src;
- let imgName = imgSrc.split('/').pop();
- imgName = truncateFileName(imgName);
- fetch(imgSrc)
- .then(response => response.blob())
- .then(blob => {
- const url = URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.href = url;
- a.download = imgName;
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- URL.revokeObjectURL(url);
- // Сохраняем текстовый файл
- const text = extractText('.mantine-mp7k2v.mantine-Card-root');
- saveFile(text, `${imgName}.txt`, 'text/plain');
- });
- }
- // Добавляем кнопку для скачивания
- const button = document.createElement('button');
- button.innerText = 'Dnld Img&Txt';
- button.style.position = 'fixed';
- button.style.top = '30px';
- button.style.right = '450px';
- button.style.zIndex = '1000';
- button.style.padding = '10px';
- button.style.backgroundColor = 'blue';
- button.style.color = 'white';
- button.addEventListener('click', () => {
- const imgElement = document.querySelector('.EdgeImage_image__iH4_q');
- if (imgElement) {
- const { pathname } = window.location;
- const defaultPath = pathname.split('/').slice(0, -1).join('/');
- const { value: savePath } = window.prompt('Enter the save path (default is the current page path):', defaultPath);
- downloadImage(imgElement, savePath || defaultPath);
- } else {
- alert('Image not found!');
- }
- });
- document.body.appendChild(button);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement