weiback

微博数据下载备份

// ==UserScript==
// @name         weiback
// @namespace    https://github.com/Shapooo/
// @version      0.9.4
// @author       Shapooo
// @description  微博数据下载备份
// @license      GPL
// @icon         https://www.google.com/s2/favicons?sz=64&domain=weibo.com
// @homepageURL  https://github.com/Shapooo/WeiBack
// @match        *://*.weibo.com/*
// @require      https://cdn.bootcdn.net/ajax/libs/jszip/3.10.1/jszip.min.js
// @require      https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const domain$2 = window.location.host;
  const STATUSES_LONGTEXT_API = `https://${domain$2}/ajax/statuses/longtext`;
  async function generateHTMLPage(posts, storage) {
    return Promise.all(posts.map((post) => generateHTMLPost(post, storage))).then((posts2) => posts2.join(""));
  }
  function composePost(postMeta, isRetweet = false) {
    let prefix = "bk-post";
    if (isRetweet) {
      prefix = "bk-retweet";
    }
    const postDiv = document.createElement("div");
    postDiv.className = prefix;
    if (postMeta.posterName) {
      const posterDiv = document.createElement("div");
      posterDiv.className = prefix + "er";
      const avatar = document.createElement("img");
      avatar.className = prefix + "er-avatar";
      avatar.alt = "头像";
      avatar.src = postMeta.posterAvatar;
      posterDiv.appendChild(avatar);
      const name = document.createElement("a");
      name.className = prefix + "er-name";
      name.href = postMeta.posterUrl;
      name.innerText = postMeta.posterName;
      posterDiv.appendChild(name);
      const createDetail = document.createElement("p");
      createDetail.className = "bk-create-detail";
      createDetail.innerHTML = `&nbsp&nbsp&nbsp${postMeta.createdAt} ${postMeta.regionName}`;
      posterDiv.appendChild(createDetail);
      postDiv.appendChild(posterDiv);
    }
    const textDiv = document.createElement("div");
    textDiv.className = prefix + "-text bk-content";
    textDiv.innerHTML = postMeta.text;
    postDiv.appendChild(textDiv);
    if (postMeta.medium) {
      const mediaDiv = document.createElement("div");
      mediaDiv.className = prefix + "-media bk-content";
      mediaDiv.innerHTML = postMeta.medium.join("");
      postDiv.appendChild(mediaDiv);
    }
    if (postMeta.postUrl) {
      const postUrl = document.createElement("a");
      postUrl.innerHTML = "[原贴链接]";
      postUrl.href = postMeta.postUrl;
      postUrl.className = "bk-link bk-content";
      postDiv.appendChild(postUrl);
    }
    return postDiv;
  }
  async function generateHTMLPost(post, storage) {
    const wrapper = document.createElement("div");
    wrapper.className = "bk-post-wrapper";
    wrapper.appendChild(composePost(await parsePost(post, storage)));
    if (post.retweeted_status) {
      wrapper.appendChild(composePost(await parsePost(post.retweeted_status, storage), true));
    }
    return wrapper.outerHTML;
  }
  function getMedium(post, storage) {
    const imageDefinition2 = globalThis.imageDefinition;
    if (post.mix_media_info) {
      return post.mix_media_info.items.map((item) => {
        const data = item.data;
        if (item.type === "pic") {
          const pic = document.createElement("img");
          pic.className = "bk-pic";
          pic.alt = "[图片]";
          let picObj;
          if (imageDefinition2 === 3) {
            picObj = item.data.mw2000 || item.data.largest || item.data.original || item.data.large || item.data.bmiddle;
          } else if (imageDefinition2 === 2) {
            picObj = item.data.large || item.data.bmiddle;
          } else {
            picObj = item.data.bmiddle || item.data.thumbnail;
          }
          pic.src = picObj.url;
          return pic.outerHTML;
        } else if (item.type === "video") {
          const video = document.createElement("a");
          video.className = "bk-link";
          video.innerHTML = data.page_title;
          video.href = data.media_info && data.media_info.h5_url;
          const pic = document.createElement("img");
          pic.className = "bk-pic";
          pic.alt = "[视频]";
          pic.src = url2path(data.page_pic, storage);
          video.appendChild(pic);
          return video.outerHTML;
        } else {
          return "";
        }
      });
    } else if (post.pic_ids && post.pic_infos) {
      return post.pic_ids.map((id) => {
        const tmpObj = post.pic_infos[id];
        let picObj;
        if (imageDefinition2 === 3) {
          picObj = tmpObj.mw2000 || tmpObj.largest || tmpObj.original || tmpObj.large || tmpObj.bmiddle;
        } else if (imageDefinition2 === 2) {
          picObj = tmpObj.large || tmpObj.bmiddle;
        } else {
          picObj = tmpObj.bmiddle || tmpObj.thumbnail;
        }
        return url2path(picObj.url, storage);
      }).map((loc) => {
        const pic = document.createElement("img");
        pic.className = "bk-pic";
        pic.alt = "[图片]";
        pic.src = loc;
        return pic.outerHTML;
      });
    } else {
      return [];
    }
  }
  async function parsePost(post, storage) {
    let text = post.isLongText && await fetchLongText(post.mblogid) || post.text_raw;
    text = await transText(text || post.text_raw, post.topic_struct, post.url_struct, storage);
    let posterAvatar;
    if (post.user && post.user.id) {
      let avatarUrl;
      switch (imageDefinition) {
        case 1:
          avatarUrl = post.user.profile_image_url || post.user.avatar_large || post.user.avatar_hd;
          break;
        case 2:
          avatarUrl = post.user.avatar_large || post.user.avatar_hd || post.user.profile_image_url;
          break;
        default:
          avatarUrl = post.user.avatar_hd || post.user.avatar_large || post.user.profile_image_url;
      }
      posterAvatar = avatarUrl && url2path(avatarUrl, storage);
    }
    return {
      posterName: post.user && post.user.screen_name,
      posterUrl: post.user && "https://weibo.com" + post.user.profile_url,
      posterAvatar,
      text,
      postUrl: post.user && post.user.id && `https://weibo.com/${post.user.idstr}/${post.mblogid}`,
      mblogid: post.mblogid,
      createdAt: new Date(post.created_at).toLocaleString("zh-CN"),
      regionName: post.region_name ? post.region_name : "",
      medium: getMedium(post, storage)
    };
  }
  async function fetchLongText(mblogid) {
    const api = `${STATUSES_LONGTEXT_API}?id=${mblogid}`;
    const res = await fetch(api);
    if (res.ok) {
      const jsn = await res.json();
      return jsn.data && jsn.data.longTextContent || "";
    } else {
      return "";
    }
  }
  async function transText(text, topicStruct, urlStruct, storage) {
    const o = {
      url: "(http|https)://[a-zA-Z0-9$%&~_#/.\\-:=,?]{5,280}",
      stock: "\\$([^\\$]+)\\$",
      br: "\\n"
    };
    const atExpr = /@[\u4e00-\u9fa5|\uE7C7-\uE7F3|\w_\-·]+/g;
    const emojiExpr = /(\[.*?\])(?!#)/g;
    const r = new RegExp(Object.values(o).join("|"), "g");
    const emailExpr = /[A-Za-z0-9]+([_.][A-Za-z0-9]+)*@([A-Za-z0-9-]+\.)+[A-Za-z]{2,6}/g;
    const topicExpr = /#([^#]+)#/g;
    const u = [];
    const ret = text && (text = text.replace(emailExpr, function(t) {
      if (t.match(atExpr)) {
        const e = t.match(atExpr);
        u.push(e && e[0]);
      }
      return t;
    }), text = text.replace(r, function(e) {
      if (e) {
        const o2 = e.slice(0, 1);
        return e === "\n" ? transBr() : o2 === "h" || urlStruct ? transUrl(e, urlStruct) : e;
      }
    }), text = text.replace(atExpr, function(e) {
      return u.indexOf(e) !== -1 ? e : transUser(e);
    }), text = text.replace(topicExpr, function(e) {
      const a = e.slice(0, 1);
      return a === "#" && "#&#" && isSuperTopic(e, urlStruct) !== e ? transTopic(e, topicStruct) : e;
    }), console.assert(globalThis.globalConfig.emoticon), text = text.replace(emojiExpr, function(e) {
      return transEmoji(e, storage);
    }), text);
    return ret;
  }
  function isSuperTopic(input, urlStruct) {
    input = input !== void 0 ? input : "";
    urlStruct = urlStruct !== void 0 ? urlStruct : [];
    return input && urlStruct.length && urlStruct.find(function(e) {
      return e.short_url === input;
    });
  }
  function transUrl(input, urlStruct) {
    const urlObj = urlStruct && urlStruct.find(function(e) {
      return e.short_url === input;
    });
    const urlExpr = /(http|https):\/\/([\w.]+\/?)\S*/;
    if (!urlExpr.test(input)) {
      return input;
    }
    const u = /^http:\/\/t\.cn/;
    u.test(input) && (input = input.replace(/http:/, "https:"));
    let urlTitle = "网页链接";
    let url = input;
    if (urlObj && urlObj.url_title) {
      urlTitle = urlObj.url_title;
    }
    if (urlObj && urlObj.long_url) {
      url = urlObj.long_url;
    }
    return `<a class="bk-link" target="_blank" href="${url}"><img class="bk-icon-link" src="https://h5.sinaimg.cn/upload/2015/09/25/3/timeline_card_small_web_default.png"/>${urlTitle}</a>`;
  }
  function transTopic(t, topicStruct) {
    const i = t && t.slice(1, -1);
    const a = topicStruct && topicStruct.find(function(t2) {
      return t2.topic_title === i && t2.is_invalid === 1;
    });
    if (a) {
      return t;
    }
    const o = "https://s.weibo.com/weibo?q=".concat(encodeURIComponent(t));
    const n = "_blank";
    return '<a class ="bk-link" href="'.concat(o, '" target="').concat(n, '">').concat(t, "</a>");
  }
  function transUser(t) {
    const e = t.slice(1);
    return e ? '<a class="bk-user" href=https://weibo.com/n/'.concat(e, ">").concat(t, "</a>") : t;
  }
  function transBr() {
    return "<br />";
  }
  function transEmoji(t, storage) {
    const location = getEmoji(t, storage);
    const o = t.slice(1, -1);
    return location ? '<img class="bk-emoji" alt="['.concat(o, ']" title="[').concat(o, ']" src="').concat(location, '" />') : t;
  }
  function getEmoji(e, storage) {
    const emoticon = globalThis.globalConfig.emoticon;
    if (emoticon === void 0) {
      return "";
    }
    const url = emoticon.get(e);
    if (url) {
      return url2path(url, storage);
    } else {
      return "";
    }
  }
  function getFilename(url) {
    return url.split("/").slice(-1)[0].split("?")[0];
  }
  function url2path(url, storage) {
    const fileName = getFilename(url);
    const filePath = `./${storage.taskName}-${storage.index}_files/` + fileName;
    storage.picUrls.add(url);
    return filePath;
  }
  function LRUCache(capacity) {
    this.capacity = capacity;
    this.next = new Uint16Array(capacity);
    this.prev = new Uint16Array(capacity);
    this.K = new Array(capacity);
    this.V = new Array(capacity);
    this.size = 0;
    this.head = 0;
    this.tail = 0;
    this.items = /* @__PURE__ */ new Map();
  }
  LRUCache.prototype.splayOnTop = function(pointer) {
    const oldHead = this.head;
    if (this.head === pointer) {
      return this;
    }
    const previous = this.prev[pointer];
    const next = this.next[pointer];
    if (this.tail === pointer) {
      this.tail = previous;
    } else {
      this.prev[next] = previous;
    }
    this.next[previous] = next;
    this.prev[oldHead] = pointer;
    this.head = pointer;
    this.next[pointer] = oldHead;
    return this;
  };
  LRUCache.prototype.set = function(key, value) {
    let pointer = this.items.get(key);
    if (typeof pointer !== "undefined") {
      this.splayOnTop(pointer);
      this.V[pointer] = value;
      return;
    }
    if (this.size < this.capacity) {
      pointer = this.size++;
    } else {
      pointer = this.tail;
      this.tail = this.prev[pointer];
      this.items.delete(this.K[pointer]);
    }
    this.items.set(key, pointer);
    this.K[pointer] = key;
    this.V[pointer] = value;
    this.next[pointer] = this.head;
    this.prev[this.head] = pointer;
    this.head = pointer;
  };
  LRUCache.prototype.get = function(key) {
    const pointer = this.items.get(key);
    if (typeof pointer === "undefined") {
      return;
    }
    this.splayOnTop(pointer);
    return this.V[pointer];
  };
  const domain$1 = window.location.host;
  const HTML_GEN_TEMP = '<html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><style>body{background-color:#f1f2f5}.bk-post-wrapper{border-radius:4px;background:#fff;width:700px;margin:8px auto;padding:10px 0}.bk-poster{display:flex;align-items:center;vertical-align:middle;height:60px}.bk-poster-avatar,.bk-retweeter-avatar{height:60px;width:60px;border-radius:50%;margin:auto 8px}.bk-retweeter-avatar{height:40px;width:40px}.bk-post-text{background:#fff}.bk-content{margin:8px 24px 8px 76px;font-size:15px;line-height:24px}.bk-retweeter{display:flex;align-items:center;vertical-align:middle;height:40px;margin:8px 24px 8px 76px}.bk-retweet{background:#f9f9f9;padding:2px 0}.bk-pic{max-width:600px;max-height:400px;margin:auto}.bk-poster-name,.bk-retweeter-name{color:#000;font-weight:700;text-decoration:none;font-family:Arial,Helvetica,sans-serif}.bk-poster-name:hover,.bk-retweeter-name:hover{color:#eb7350}.bk-icon-link{height:20px;filter:sepia(100%) saturate(3800%) contrast(75%);vertical-align:middle;margin-bottom:4px}.bk-link,.bk-user{color:#eb7350;text-decoration:none}.bk-link:hover,.bk-user:hover{text-decoration:underline}.bk-emoji{height:20px;vertical-align:middle;margin-bottom:4px}.bk-create-detail{font-size:10px;color:#939393}</style><title>微博备份</title></head><body></body></html>';
  const STATUSES_MY_MICRO_BLOG_API = `https://${domain$1}/ajax/statuses/mymblog`;
  const STATUSES_CONFIG_API = `https://${domain$1}/ajax/statuses/config`;
  const STATUSES_LIKE_LIST_API = `https://${domain$1}/ajax/statuses/likelist`;
  const FAVORITES_ALL_FAV_API = `https://${domain$1}/ajax/favorites/all_fav`;
  async function fetchPostMeta(uid = 0, page = 1, type = "myblog") {
    let api = `${STATUSES_MY_MICRO_BLOG_API}?uid=${uid}&page=${page}&feature=0`;
    if (type === "fav") {
      api = `${FAVORITES_ALL_FAV_API}?uid=${uid}&page=${page}`;
    }
    if (type === "like") {
      api = `${STATUSES_LIKE_LIST_API}?uid=${uid}&page=${page}`;
    }
    console.log(`request ${api}`);
    const req = await fetch(api);
    const data = await req.json();
    return data;
  }
  async function fetchAllPosts(type = "myblog", range, downloadPic, uid) {
    console.log(`fetching ${type} post ${range}`);
    const storage = {
      cache: new LRUCache(200),
      taskName: `WeiBack-${type}-${Date.now()}`,
      picUrls: /* @__PURE__ */ new Set(),
      index: 1
    };
    await fetchEmoticon();
    uid = uid ? uid : globalConfig.uid;
    const downloadPeriod = globalThis.downloadPeriod || 10;
    let allPageData = [];
    let noMore = false;
    let index = 0;
    const zip = new JSZip();
    const name = storage.taskName;
    const resources = /* @__PURE__ */ new Map();
    for (let page = range[0]; page <= range[1]; page++) {
      console.log("scan", "page", page);
      showTip(`正在备份第 ${page} 页<br>因微博速率限制,过程可能较长,先干点别的吧`);
      let data;
      for (let i = 0; i < 10; i++) {
        const pageData = await fetchPostMeta(uid, page, type);
        if (pageData.ok) {
          data = type === "fav" ? pageData.data : pageData.data.list;
          if (data.length === 0)
            noMore = true;
          break;
        }
        await new Promise((resolve) => {
          setTimeout(resolve, 8 * 1e3);
        });
        console.log("retry", i);
        showTip(
          `[重试]备份第 ${index} 页,错误内容: ${JSON.stringify(pageData)}`
        );
      }
      allPageData.push(await generateHTMLPage(data, storage));
      if (downloadPic) {
        for (const url of storage.picUrls) {
          if (resources.get(url)) {
            continue;
          }
          const blob = storage.cache.get(url);
          if (blob) {
            resources.set(url, blob);
          } else {
            const blob2 = await fetchPic(url);
            storage.cache.set(url, blob2);
            resources.set(url, blob2);
          }
        }
      }
      storage.picUrls.clear();
      index++;
      if (index % downloadPeriod === 0 || page === range[1] || noMore) {
        const taskName = `${name}-${storage.index}`;
        storage.index++;
        const doc = new DOMParser().parseFromString(HTML_GEN_TEMP, "text/html");
        doc.body.innerHTML = allPageData.join("");
        allPageData = [];
        zip.file(taskName + ".html", doc.documentElement.outerHTML);
        const resourcesFolder = zip.folder(taskName + "_files");
        resources.forEach((blob, url) => {
          resourcesFolder.file(getFilename(url), blob, { base64: true });
        });
        zip.generateAsync({ type: "blob" }).then(function(content) {
          saveAs(content, taskName + ".zip");
        }).catch((err) => {
          console.error(err);
        });
        zip.remove(taskName);
        resources.clear();
      }
      if (noMore || page === range[1])
        break;
      await new Promise((resolve) => {
        setTimeout(resolve, 10 * 1e3);
      });
    }
    showTip("数据拉取完成,等待下载到本地");
    console.log("all done");
    showTip("完成,可以进行其它操作");
  }
  async function fetchPic(url) {
    for (let i = 0; i < 5; ++i) {
      try {
        const res = await fetch(url);
        return await res.blob();
      } catch (err) {
        console.log("fic fetch occurs: ", err);
        await new Promise((resolve) => {
          setTimeout(resolve, i * 300);
        });
      }
    }
    console.log(`pic ${url} download failed`);
  }
  async function fetchEmoticon() {
    const api = STATUSES_CONFIG_API;
    const res = await fetch(api);
    const rawEmoticon = (await res.json()).data.emoticon;
    console.assert(rawEmoticon !== void 0);
    const emoticon = /* @__PURE__ */ new Map();
    for (const lang in rawEmoticon) {
      const emojiSets = rawEmoticon[lang];
      for (const setName in emojiSets) {
        for (const obj of emojiSets[setName]) {
          emoticon.set(obj.phrase, obj.url);
        }
      }
    }
    globalThis.globalConfig.emoticon = emoticon;
  }
  const bkBoxStyle = ".bkBox{position:fixed;border-radius:5px;background:#fff;top:80px;right:20px;z-index:100000;padding:10px 15px;text-align:center;border:1px solid #a7a6a6}.bkBox-title{font-size:20px;color:#ff8200;margin:8px 0;font-weight:400;border:1px solid transparent;border-bottom-color:#a7a6a6}.bkBox-button{border-radius:2.25rem;display:block;background:#ffd3a3;padding:5px 8px;color:#fff;text-align:center;cursor:pointer;margin:16px auto 7px;border:0;font:white}.bkBox-button:hover{background:#ff8200}.box,.nav{width:320px;margin:auto;display:flex}.nav{height:30px;background-color:#fff;border:1px gray}.box{overflow:hidden}.bk-naviItem{color:#000;height:30px;width:80px;margin:auto;border:8px #000;float:left}.bk-naviItem,.bk-naviItem:hover{text-decoration:none}.bk-navi-inner{border-radius:8px;height:30px}.bk-navi-inner:hover{background-color:#f2f0ef}.bk-pageNumIndicator{margin:16px}.bk-settingPage{border-radius:5px;width:320px;height:220px;background-color:#f2f0ef;flex-shrink:0}.bk-input{border-radius:1em;border:2px solid #ffd3a3;height:20px;width:50px;text-align:center}.bk-input:hover{border:2px solid #ff8200}.bk-textinput{width:120px}.bkBox-tip{height:100px;width:320px;display:flex;align-items:center;justify-content:center}";
  const SETTINGS_TEMP = '<div class="bk-pageNumIndicator"></div><div>下载范围:<input type="number" value="1" min="1" max="22" class="bk-input range"> 至 <input type="number" value="22" min="1" max="1" class="bk-input range"> 页</div><div>分段下载:每 <input type="number" value="10" min="1" max="20" class="bk-input period"> 页分段</div><div>图片清晰度:<input type="number" value="3" min="1" max="3" class="bk-input definition">(越大越清晰)</div><div><input type="checkbox" id="download-pic" name="download-pic" checked /><label for="download-pic">同时下载图片</label></div><div><button class="bkBox-button">开始下载</button></div>';
  const OTHERS_SETTINGS_TEMP = '<div>用户id:<input type="text" class="bk-input bk-textinput" required minlength="10" maxlength="10"><div>下载范围:<input type="number" value="1" min="1" max="22" class="bk-input range"> 至 <input type="number" value="22" min="1" max="1" class="bk-input range"> 页</div><div>分段下载:每 <input type="number" value="10" min="1" max="20" class="bk-input period"> 页分段</div><div>图片清晰度:<input type="number" value="3" min="1" max="3" class="bk-input definition">(越大越清晰)</div><div><input type="checkbox" id="download-pic" name="download-pic" checked /><label for="download-pic">同时下载图片</label></div><div><button class="bkBox-button">开始下载</button></div>';
  const TITLE_PIC = '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKkAAAAoCAIAAAAKW00FAAAQVklEQVR4nO1ce3Cc1XU/595vdyWtVo+1LFmWbcmWJVm2wY9ACANmCGMnpQbXBcxjCNOWpkM6GdrpHx06k5YZ2j/6R9sZZhgmk2TKJ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Xw/+tTv7L/A+jCnQKFhZb1Sic3di0VbTsxOYdZTPPryT+F1+jb3ToSCBsAAAAAElFTkSuQmCCcmVzb3VyY2UoMikgb2YgdHlwZSAoZ2QpCg==">';
  const BKBOX_TEMP = `<div class='bkBox-title'>${TITLE_PIC}</div><nav><div class='navi'></div></nav><section><div class='box'></div></section>`;
  const domain = window.location.host;
  const FAVORITES_TAGS_API = `https://${domain}/ajax/favorites/tags?page=1&is_show_total=1`;
  const PROFILE_INFO_API = `https://${domain}/ajax/profile/info`;
  const globalConfig$1 = {
    uid: $CONFIG.uid
  };
  globalThis.globalConfig = globalConfig$1;
  const backType = (type, name) => {
    return (async () => {
      const amount = await getPageAmount(type);
      console.log(`get amount of ${type} post: ${amount}`);
      return { type, name, pageAmount: amount };
    })();
  };
  const bkBox = document.createElement("div");
  bkBox.className = "bkBox";
  bkBox.innerHTML = BKBOX_TEMP;
  const bkBoxStyleSheet = document.createElement("style");
  bkBoxStyleSheet.innerText = bkBoxStyle;
  bkBox.appendChild(bkBoxStyleSheet);
  const bkBoxTip = document.createElement("div");
  bkBoxTip.className = "bkBox-tip";
  bkBox.appendChild(bkBoxTip);
  globalThis.downloadPeriod = 10;
  globalThis.imageDefinition = 3;
  const bkTypes = [backType("fav", "我的收藏"), backType("myblog", "我的发布"), backType("like", "我的点赞"), backType("others", "他人发布")];
  Promise.all(bkTypes).then((values) => {
    values.map((t) => {
      return {
        type: t.type,
        name: t.name,
        settingsPage: (() => {
          const type = t.type;
          const name = t.name;
          const pageAmount = t.pageAmount;
          const page = document.createElement("div");
          if (type === "others") {
            page.innerHTML = OTHERS_SETTINGS_TEMP;
          } else {
            page.innerHTML = SETTINGS_TEMP;
          }
          if (type !== "like" && type !== "others") {
            const pageNumIndicator = page.getElementsByClassName("bk-pageNumIndicator")[0];
            pageNumIndicator.innerHTML = name + "的总页数约为 " + pageAmount;
            pageNumIndicator.title = "微博每页返回博文数目不定,每页接近二十条博文。如果设置最高页数,则默认下载完全部";
            const pageEndInput = page.querySelectorAll(".range")[1];
            pageEndInput.max = pageAmount;
            pageEndInput.value = pageAmount;
          }
          const btn = page.getElementsByClassName("bkBox-button")[0];
          btn.addEventListener("click", async () => {
            const range = page.querySelectorAll(".range");
            let dlRange = [parseInt(range[0].value), parseInt(range[1].value)];
            dlRange = dlRange === pageAmount ? Infinity : dlRange;
            const period = page.querySelector(".period");
            globalThis.downloadPeriod = parseInt(period.value);
            globalThis.downloadPeriod = globalThis.downloadPeriod > 20 ? 20 : globalThis.downloadPeriod;
            globalThis.downloadPeriod = globalThis.downloadPeriod < 1 ? 1 : globalThis.downloadPeriod;
            globalThis.imageDefinition = parseInt(page.querySelector(".definition").value);
            globalThis.imageDefinition = globalThis.imageDefinition > 3 ? 3 : globalThis.imageDefinition;
            globalThis.imageDefinition = globalThis.imageDefinition < 1 ? 1 : globalThis.imageDefinition;
            const downloadPic = page.querySelector("#download-pic").checked;
            try {
              hideAllButton();
              if (type === "others") {
                const uid = page.querySelector(".bk-textinput").value;
                await fetchAllPosts(type, dlRange, downloadPic, uid);
              } else {
                await fetchAllPosts(type, dlRange, downloadPic);
              }
              showAllButton();
            } catch (err) {
              showTip(`程序崩了,原因: ${err}`);
              console.error(err);
            }
          });
          return page;
        })()
      };
    }).forEach((c) => {
      settleDownBkTypes(c.type, c.name, c.settingsPage);
      console.log(`settled post type ${c.type} to bkBox`);
    });
    document.body.appendChild(bkBox);
  });
  function hideAllButton() {
    const buttons = document.getElementsByClassName("bkBox-button");
    buttons.forEach((btn) => {
      btn.style.display = "none";
    });
  }
  function showAllButton() {
    const buttons = document.getElementsByClassName("bkBox-button");
    buttons.forEach((btn) => {
      btn.style.display = "block";
    });
  }
  function settleDownBkTypes(type, name, settingsPage) {
    const nav = bkBox.getElementsByClassName("navi")[0];
    const settingBox = bkBox.getElementsByClassName("box")[0];
    const naviButton = document.createElement("a");
    naviButton.innerHTML = '<div class="bk-navi-inner">' + name + "</div>";
    naviButton.className = "bk-naviItem";
    naviButton.href = "#" + type;
    nav.appendChild(naviButton);
    const page = document.createElement("div");
    page.id = type;
    page.className = "bk-settingPage";
    page.appendChild(settingsPage);
    settingBox.appendChild(page);
  }
  async function getPageAmount(type) {
    if (type === "like") {
      return Infinity;
    }
    if (type === "fav") {
      const req2 = await fetch(FAVORITES_TAGS_API);
      const json2 = await req2.json();
      return json2 && json2.ok && Math.ceil(json2.fav_total_num / 20);
    }
    const req = await fetch(`${PROFILE_INFO_API}?uid=${globalConfig$1.uid}`);
    const json = await req.json();
    return json && json.ok && Math.ceil(json.data.user.statuses_count / 20);
  }
  function showTip(msg) {
    bkBoxTip.innerHTML = msg;
  }

})();