Greasy Fork

Greasy Fork is available in English.

豆瓣图片下载

全部图片页添加下载按钮;图片页添加下载按钮;图片页显示原图

当前为 2022-08-11 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         豆瓣图片下载
// @namespace    http://tampermonkey.net/
// @version      1.03
// @description  全部图片页添加下载按钮;图片页添加下载按钮;图片页显示原图
// @author       backrock12
// @match        *.douban.com/*
// @icon         https://www.google.com/s2/favicons?domain=douban.com
// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @grant       GM_download
// @grant       GM_xmlhttpRequest
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  //自动下载
  const isauto = true;

  const prefixs = ["r", "raw", "l", "large"];

  function replacefun(prefix) {
    return `$1${prefix}$3`;
  }
  const pic_regex =
    /^(https:\/\/img\d\.doubanio\.com\/p?view\/(event_poster|subject|note|status|group|group_topic|photo|richtext)\/).+(\/public\/.+\..+)$/;

  async function gethtml(url) {
    return new Promise((resolve, reject) => {
      GM_xmlhttpRequest({
        url: url,
        method: "GET",
        onload: function (response) {
          resolve(response.responseText);
        },
      });
    });
  }

  function subject_button() {
    if (/movie\.douban\.com\/subject/.test(location.href)) {
      const h1_id = "div.mod div > h2";
      const a_id = "div.mod div  h2   span a";
      const doc_id = ".poster-col3  li  div  a  img";

      add_down(h1_id, a_id, doc_id);

      const h1_id2 = "div.mod:nth-child(2) > div > h2";
      const a_id2 = " div.mod:nth-child(2) > div h2 span a ";
      const doc_id2 = ".poster-col3  li  div  a  img";

      add_down(h1_id2, a_id2, doc_id2);

      const h1_id3 = "div.mod:nth-child(3) > div > h2 ";
      const a_id3 = " div.mod:nth-child(3) > div h2 span a ";
      const doc_id3 = ".poster-col3  li  div  a  img";
      add_down(h1_id3, a_id3, doc_id3);
    }
  }

  function add_down(h1_id, a_id, doc_id) {
    const h1 = document.querySelector(h1_id);
    const a = document.querySelector(a_id);
    const url = a.href;

    let title = "";
    const titleobj = document.querySelector("#content > h1");
    if (titleobj) title = titleobj.innerText.replace("的全部图片", "");

    const ce = document.createElement("button");
    ce.id = "CDownBtn";
    ce.textContent = "全部下载";
    ce.className = "btn btn-md btn-default";
    ce.onclick = function () {
      down_all(url);
    };
    h1.append(ce);

    async function down_all(aurl) {
      let index = 0;
      let url_list = [];
      let isloop = true;
      while (isloop) {
        const url = `${aurl}&start=${index * 30}&sortby=like&size=a&subtype=a`;
        console.log(url);
        const htmltext = await gethtml(url);

        let doc = $("<html></html>");
        doc.html(htmltext);
        const tlist = doc.find(doc_id);
        if (tlist.length == 0) {
          isloop = false;
        }

        for (let i = 0; i < tlist.length; i++) {
          const url = tlist[i].src.replace(pic_regex, replacefun(prefixs[0]));
          url_list.push(url);
        }

        index++;
      }

      console.log(url_list);
      let oknum = 0;
      let errornum = 0;

      for (let i = 0; i < url_list.length; i++) {
        const url = url_list[i];
        const name = title + "_" + url.substring(url.lastIndexOf("/") + 1); //.replace(".", "_");
        GM_download({
          url: url,
          name: name,
          onerror: (error) => {
            errornum++;
            console.log(url);
            console.log(error);
          },
          onload: () => {
            oknum++;
          },
        });
      }

      const time = url_list.length > 20 ? 2000 : 1000;

      const IntervalId = setInterval(() => {
        if (url_list.length == oknum + errornum) {
          const msg = `下载完成,共${url_list.length}个文件,成功${oknum},失败${errornum}`;
          alert(msg);
          clearInterval(IntervalId);
        }
      }, time);
    }
  }

  function pic_button() {
    if (/movie\.douban\.com\/photos\/photo/.test(location.href)) {
      const pic = document.querySelector(".mainphoto img");
      const h1 = document.querySelector("#content > h1");
      let url = pic.src;
      const name = url.substring(url.lastIndexOf("/") + 1).replace(".", "_");

      console.log(url);

      const ce = document.createElement("button");
      ce.id = "CDownBtn";
      ce.textContent = "全部下载";
      ce.className = "btn btn-md btn-default";
      ce.onclick = function () {
        GM_download(url, name);
        ce.style.backgroundColor = "#87CEFA";
      };
      h1.append(ce);
      if (isauto) ce.click();
    }
  }

  function bigpic() {
    if (/all_photos/.test(location.href)) return;

    const imgs = document.querySelectorAll("img");
    let tindex = 0;
    imgs.forEach((img) => {
      if (img.src && pic_regex.test(img.src)) {
        let index = 0;
        img.src = img.src.replace(pic_regex, replacefun(prefixs[tindex]));
        img.onerror = (e) => {
          if (tindex != index) {
            index = 0;
          } else {
            index += 1;
            tindex = index;
          }
          if (index >= prefixs.length) return;
          img.src = img.src.replace(pic_regex, replacefun(prefixs[index]));
        };
      }
    });
  }

  function start() {
      if (/douban\.com\/game/.test(location.href))return;
    bigpic();
    pic_button();
    subject_button();
  }

  setTimeout(start, 300);
  // Your code here...
})();