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

相关文章

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主

Android多线程下载见解

通过for循环开启N个线程,这是多线程,但每次循环都new一个线程肯定很耗内存的。那可以改用线程池来。 就以我个人对多线程下载的理解是开启一个线程后: 1.通过HttpUrlConnection对象获取要下载文件的总长度 2.通过RandomAccessFile流对象在本地创建一个跟远程文件长度一样大小的空文件。 3.通过文件总长度/线程个数=得到每个线程大概要下载的量(线程块大小)。

在服务器上浏览图片

@StarSky 2018-10-26 15:09 字数 15971 阅读 28 https://www.zybuluo.com/StarSky/note/1294871 来源 2018-09-27 线上服务器安装 imgcat Tool   2018-09-27 线上服务器安装 imgcat 0. 准备文件:iterm2_shell_integration.bash1. 在有权限

vue3项目将所有访问后端springboot的接口统一管理带跨域

vue3项目将所有访问后端springboot的接口统一管理带跨域 一、前言1.安装Axios2.创建Axios实例3.创建API服务文件4.在组件中使用API服务 二、跨域三、总结 一、前言 在Vue 3项目中,统一管理所有访问后端Spring Boot接口的最佳实践是创建一个专门的API服务层。这可以让你的代码更加模块化、可维护和集中管理。你可以使用Axios库作为HTT

axios全局封装AbortController取消重复请求

为什么? 问题:为什么axios要配置AbortController?防抖节流不行吗? 分析: 防抖节流本质上是用延时器来操作请求的。防抖是判断延时器是否存在,如果存在,清除延时器,重新开启一个延时器,只执行最后一次请求。节流呢,是判断延时器是否存在,如果存在,直接return掉,直到执行完这个延时器。事实上,这些体验感都不算友好,因为对于用户来说,得等一些时间,尤其是首次请求,不是那么流畅

MySQL理解-下载-安装

MySQL理解: mysql:是一种关系型数据库管理系统。 下载: 进入官网MySQLhttps://www.mysql.com/  找到download 滑动到最下方:有一个开源社区版的链接地址: 然后就下载完成了 安装: 双击: 一直next 一直next这一步: 一直next到这里: 等待加载完成: 一直下一步到这里

53、Flink Interval Join 代码示例

1、概述 interval Join 默认会根据 keyBy 的条件进行 Join 此时为 Inner Join; interval Join 算子的水位线会取两条流中水位线的最小值; interval Join 迟到数据的判定是以 interval Join 算子的水位线为基准; interval Join 可以分别输出两条流中迟到的数据-[sideOutputLeftLateData,

vue怎么处理跨域

Vue.js 本身并不直接解决跨域问题,因为跨域问题主要是浏览器基于同源策略(Same-origin policy)的一种安全限制。然而,在Vue.js项目中,我们可以采取一些策略来绕过或处理跨域问题。 解决跨域问题的常用方法: 代理服务器:在开发环境中,我们可以配置一个代理服务器来转发API请求,从而绕过浏览器的同源策略。Vue CLI 提供了内置的代理功能,可以在 vue.config.j

el-upload 上传图片及回显照片和预览图片,文件流和http线上链接格式操作

<div v-for="(info, index) in zsjzqwhxqList.helicopterTourInfoList" :key="info.id" >编辑上传图片// oss返回线上地址http链接格式:<el-form-itemlabel="巡视结果照片":label-width="formLabelWidth"><el-upload:action="'http: