中午 发表于 2021-12-11 14:45:41

为图片添加下载按钮,鼠标下滚20次后,下载失效了


看到个脚本是 给网站添加批量按钮,点击按钮后能打开大图,然后再手动下载。
原始脚本 地址https://greasyfork.org/zh-CN/scripts/398197-堆糖下载原图

现在尝试把点击按钮后图片在新窗口打开,修改成点击按钮后直接下载图片。
前面1-3页,都能点击按钮后成功下载,
但在鼠标向下滚动10-20次后,点击按钮后下载会无效。

案例网址:
https://www.duitang.com/search/?kw=李现&type=feed

// ==UserScript==
// @name         堆糖下载
// @namespace    http://tampermonkey.net/
// @version      0.1
// @descriptiontry to take over the world!
// @author       wlor
// @match      https://www.duitang.com/album/?id=*
// @match      https://class.duitang.com/album/?id=*
// @match      https://www.duitang.com/search/?kw=*
// @grant      GM_download
// ==/UserScript==

(function() {
    'use strict';
   function append_download_btn() {
let picList = $('.woo-pcont .woo');
for (let i = 0; i < picList.length; i++) {
    let no_btn = picList.eq(i).find(".download_btn2").length === 0;
    if (no_btn) {
      let original_url = picList.eq(i).find('.mbpho>.a>img').attr('src').replace(/\.thumb\.400_0/, "");
      let button = document.createElement("a");
      button.className = "download_btn2";
      button.innerText = "下载";
      button.style.position = "absolute";
      button.style.right = "0";
      button.style.top = "0";
      button.style.zIndex = "10";
      button.style.backgroundColor = "rgba(0,0,0,.5)";
      button.style.color = "#fff";
      button.style.padding = "0 10px";
      button.style.height = "30px";
      button.style.lineHeight = "30px";
      button.style.cursor = "pointer";
      button.href = original_url;
      button.target="_blank";
      picList.eq(i).append(button);
      //---增加的部分增加了下载权限 GM_download 。    和匹配的网址https://class.duitang.com/album/?id=*https://www.duitang.com/search/?kw=*
       button2.addEventListener('click',() => {
       GM_download(original_url,(decodeURI(original_url).split("/")));
      });
      //---增加的部分
    }
}
};

function debounce(fn, wait) {
var timeout = null;
return function () {
    if (timeout !== null) clearTimeout(timeout);
    timeout = setTimeout(fn, wait);
}
}
window.addEventListener('scroll', debounce(append_download_btn, 500));
append_download_btn()
    // Your code here...
})();

cxxjackie 发表于 2021-12-11 14:45:42

我看了一下,应该是图片后缀的问题,前面的图片都是jpg/jpeg,后面的图片变成了jpeg_webp(好像是浏览器自动做的压缩),然后你下载的时候文件名设置的XXX.jpeg_webp,这一后缀不受支持,导致下载失败。改一下文件名就行了:
GM_download(original_url, decodeURI(original_url).split("/").replace(/_webp$/, ''));

李恒道 发表于 2021-12-11 15:10:24

这个我没太理解具体遇到了什么问题...
测试了一下脚本也没太懂功能
但是遇到与逻辑不符
可以打debugger断点一步一步跟,可能你处理哪里没搞好
页: [1]
查看完整版本: 为图片添加下载按钮,鼠标下滚20次后,下载失效了