lofterDown

lofter頁面內容下載

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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...
})();