lofterDown

lofter頁面內容下載

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         lofterDown
// @namespace    http://tampermonkey.net/
// @version      1.00
// @description  lofter頁面內容下載
// @author       You
// @match        *.lofter.com/*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/FileSaver.min.js
// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function () {
  "use strict";

  //http://miyamayukimi06288.lofter.com&t=1583587016128

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

  async function downone(url) {
    try {
      const txt = await gethtml(url);
      let doc = $("<html></html>");
      doc.html(txt);
      const tlist = doc.find(".postinner .ct");
      if (tlist.length == 0) return;

      const title = doc.find(".postinner .ct .ttl");

      var blob = new Blob([tlist.text()], {
        type: "text/plain;charset=utf-8",
      });
      saveAs(blob, `${title.text()}.txt`);
      console.log(`${url} ok`);
      return true;
    } catch (e) {
      console.log(`${url} error`);
      setTimeout(() => {
        alert(`${url} error`);
      }, 100);
      return false;
    }
  }

  function downpageall() {
    const num = $(".num").text();
    const maxnum = parseInt(num.split("/")[1].trim());
    // console.log(maxnum);

    for (let pagenum = 1; pagenum < maxnum; pagenum++) {
      const pageurl = `${document.baseURI}/?page=${pagenum}`;
      if (!downpageone(pageurl)) return;
    }
  }

  async function downpageone(pageurl) {
    const txt = await gethtml(pageurl);
    const doc = $("<html></html>");
    doc.html(txt);
    const tlist = doc.find(".m-postlst .postinner .ttl a");
    if (tlist.length == 0) return;
    for (let i = 0; i < tlist.length; i++) {
      //console.log(tlist[i].href);
      const re = await downone(tlist[i].href);
      if (!re) return;
    }
  }

  function addbuttonallpage() {
    const t = document.querySelector(".m-nav");
    // console.log(t);
    if (t) {
      let e = document.createElement("button");
      e.id = "TALLDownBtnpage";
      e.textContent = "下载所有頁";
      e.className = "btn btn-md btn-default";
      e.onclick = downpageall;
      t.parentNode.insertBefore(e, t);
    }
  }

  function addbuttonnoepage() {
    const t = document.querySelector(".m-nav");
    // console.log(t);
    if (t) {
      let e = document.createElement("button");
      e.id = "TONEDownBtnpage";
      e.textContent = "下载本頁";
      e.className = "btn btn-md btn-default";
      e.onclick = function () {
        downpageone(location.href);
      };
      t.parentNode.insertBefore(e, t);
    }
  }

  function addbuttonnoe() {
    const t = document.querySelector(".m-nav");
    // console.log(t);
    if (t) {
      let e = document.createElement("button");
      e.id = "TONEDownBtn";
      e.textContent = "下载本頁";
      e.className = "btn btn-md btn-default";
      e.onclick = function () {
        downone(location.href);
      };
      t.parentNode.insertBefore(e, t);
    }
  }

  async function run() {
    if (/lofter\.com\/post/.test(location.href)) {
      addbuttonnoe();
    } else {
      addbuttonnoepage();
      addbuttonallpage();
    }
  }
  run();
  // Your code here...
})();