哔哩哔哩历史转发

在浏览哔哩哔哩历史时将数据转发到https://bilibili.mundane.ink

// ==UserScript==
// @name         哔哩哔哩历史转发
// @namespace    https://bilibili.mundane.ink
// @version      0.3
// @description  在浏览哔哩哔哩历史时将数据转发到https://bilibili.mundane.ink
// @match        https://www.bilibili.com/account/history*
// @match        *://www.bilibili.com
// @run-at       document-start
// @license      MIT
// @icon         https://www.bilibili.com/favicon.ico
// ==/UserScript==

(function () {
  "use strict";
  const baseUrl = "https://mundane.ink";
  // const baseUrl = "http://localhost:8088";

  function getUserId() {
    return getCookie("DedeUserID");
  }

  function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(";");
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i].trim();
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  }

  const originOpen = XMLHttpRequest.prototype.open;
  const historyUrl = "https://api.bilibili.com/x/web-interface/history/cursor";
  XMLHttpRequest.prototype.open = function (_, url) {
    const xhr = this;
    if (url.includes(historyUrl)) {
      const getter = Object.getOwnPropertyDescriptor(
        XMLHttpRequest.prototype,
        "response"
      ).get;
      Object.defineProperty(xhr, "responseText", {
        get: () => {
          const result = getter.call(xhr);
          const saveUrl = `${baseUrl}/mail/bilibili/history/save`;
          const requestData = JSON.stringify({ result, userId: getUserId() });
          try {
            // 将result发送到服务器
            fetch(saveUrl, {
              method: "POST",
              headers: {
                "Content-Type": "application/json",
              },
              body: requestData,
            })
              .then((response) => {
                if (!response.ok) {
                  throw new Error(`HTTP error! Status: ${response.status}`);
                }
                console.log("ok");
              })
              .catch((error) => console.error(error));
          } catch (e) {
            console.error(e);
          }
          return result;
        },
      });
    }
    originOpen.apply(this, arguments);
  };
  window.addEventListener("DOMContentLoaded", function () {
    const btnJump = document.createElement("button");
    btnJump.innerHTML = "去历史管理页面";

    btnJump.style.position = "fixed";
    btnJump.style.top = "90px";
    btnJump.style.right = "20px";
    btnJump.style.backgroundColor = "#FB7299";
    btnJump.style.color = "#fff";
    btnJump.style.padding = "8px";
    btnJump.style.borderRadius = "6px";
    btnJump.style.zIndex = "1000";
    btnJump.style.border = "none";

    btnJump.addEventListener("click", function () {
      const userId = getUserId();
      window.open(
        `https://bilibili.mundane.ink/manage/history?userId=${userId}`,
        "_blank"
      );
    });

    // 添加按钮到页面中
    document.body.appendChild(btnJump);
  });
  console.log("bilibili历史记录转发脚本生效了 ");
})();