Tampermonkey油猴 跨域请求下载图片示例

2024-06-22 19:28

本文主要是介绍Tampermonkey油猴 跨域请求下载图片示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Tampermonkey油猴 跨域请求下载图片示例

  • 前言
  • 项目
    • 目标网站
    • 代码编写
  • 运行效果

前言

需要用油猴采集并下载一个网站的图片,直接下下不了,搜了一下,是禁止跨域,使用CORS Unblock也不行,所以使用油猴自带的GM_xmlhttpRequest发送跨域请求。

项目

目标网站

目标网站

代码编写

代码仅作为学习使用,禁止商用。

// ==UserScript==
// @name         ***** Image Downloader and Zipper with GM_xmlhttpRequest
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  Download all matching product images as a zip file from the **** product page using GM_xmlhttpRequest (For learning purposes only)
// @author       slowfeather@163.com
// @match        
// @icon         
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @require      https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
// ==/UserScript==(function() {'use strict';// List to store image URLslet imageUrls = new Set();// Function to periodically collect image URLsfunction collectImageUrls() {const imageElements = document.querySelectorAll('img');const regex1 = /*****\.com\/package-screenshot\/.+?_scaled\.jpg$/;const regex2 = /*****\.****\.com\/key-image\/.+?\.jpg$/;imageElements.forEach((img) => {const url = img.src;if ((regex1.test(url) || regex2.test(url)) && !imageUrls.has(url)) {imageUrls.add(url);showNotification('捕获了一张图片地址 :'+url);}});}// Function to show notificationfunction showNotification(message) {const notification = document.createElement('div');notification.innerText = message;notification.style.position = 'fixed';notification.style.top = '50px';notification.style.right = '10px';notification.style.zIndex = 1000;notification.style.backgroundColor = 'lightgreen';notification.style.padding = '5px';notification.style.border = '1px solid green';notification.style.borderRadius = '5px';document.body.appendChild(notification);setTimeout(() => {document.body.removeChild(notification);}, 1000);}// Set an interval to collect image URLs every secondsetInterval(collectImageUrls, 300);// Function to download all images in the list and zip themasync function downloadAndZipImages() {const zip = new JSZip();let count = 0;const fetchImage = (url, filename) => {return new Promise((resolve, reject) => {GM_xmlhttpRequest({method: 'GET',url: url,responseType: 'blob',onload: function(response) {if (response.status === 200) {zip.file(filename, response.response);count++;resolve();} else {reject(`HTTP error! status: ${response.status}`);}},onerror: function(error) {reject(`Failed to fetch image ${url}: ${error}`);}});});};const fetchPromises = Array.from(imageUrls).map((url, index) => {const filename = `image_${index + 1}.jpg`;return fetchImage(url, filename);});try {await Promise.all(fetchPromises);if (count > 0) {const content = await zip.generateAsync({ type: "blob" });saveAs(content, "images.zip");} else {alert("No matching images found.");}} catch (error) {console.error(error);}}// Wait for the page to fully loadwindow.addEventListener('load', () => {// Create a button to trigger the downloadconst button = document.createElement('button');button.innerText = 'Download All Images as Zip';button.style.position = 'fixed';button.style.top = '10px';button.style.right = '10px';button.style.zIndex = 1000;button.addEventListener('click', downloadAndZipImages);document.body.appendChild(button);});
})();

运行效果

成功捕获图片
下载图片

这篇关于Tampermonkey油猴 跨域请求下载图片示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1085194

相关文章

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java高效实现PowerPoint转PDF的示例详解

《Java高效实现PowerPoint转PDF的示例详解》在日常开发或办公场景中,经常需要将PowerPoint演示文稿(PPT/PPTX)转换为PDF,本文将介绍从基础转换到高级设置的多种用法,大家... 目录为什么要将 PowerPoint 转换为 PDF安装 Spire.Presentation fo

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

python中的高阶函数示例详解

《python中的高阶函数示例详解》在Python中,高阶函数是指接受函数作为参数或返回函数作为结果的函数,下面:本文主要介绍python中高阶函数的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录1.定义2.map函数3.filter函数4.reduce函数5.sorted函数6.自定义高阶函数

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

JAVA实现Token自动续期机制的示例代码

《JAVA实现Token自动续期机制的示例代码》本文主要介绍了JAVA实现Token自动续期机制的示例代码,通过动态调整会话生命周期平衡安全性与用户体验,解决固定有效期Token带来的风险与不便,感兴... 目录1. 固定有效期Token的内在局限性2. 自动续期机制:兼顾安全与体验的解决方案3. 总结PS

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python屏幕抓取和录制的详细代码示例

《Python屏幕抓取和录制的详细代码示例》随着现代计算机性能的提高和网络速度的加快,越来越多的用户需要对他们的屏幕进行录制,:本文主要介绍Python屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill

Java中的Schema校验技术与实践示例详解

《Java中的Schema校验技术与实践示例详解》本主题详细介绍了在Java环境下进行XMLSchema和JSONSchema校验的方法,包括使用JAXP、JAXB以及专门的JSON校验库等技术,本文... 目录1. XML和jsON的Schema校验概念1.1 XML和JSON校验的必要性1.2 Sche