小红书转发

在浏览小红书收藏时将数据转发到https://mundane.ink/redbook/index.html,方便收藏的管理和导出

目前为 2023-08-23 提交的版本。查看 最新版本

// ==UserScript==
// @name         小红书转发
// @namespace    https://mundane.ink/redbook
// @version      2.0
// @description  在浏览小红书收藏时将数据转发到https://mundane.ink/redbook/index.html,方便收藏的管理和导出
// @match        https://www.xiaohongshu.com/user/profile/*
// @grant        GM_xmlhttpRequest
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";
  console.log("小红书脚本生效了");

  const userId = window.location.href.match(/\/user\/profile\/(\w+)/)[1];
  console.log(userId);
  // 创建按钮元素
  const btnScroll = document.createElement("button");
  btnScroll.innerHTML = "自动滚动";
  const btnJump = document.createElement("button");
  btnJump.innerHTML = "去下载";
  const btnTest = document.createElement("button");
  btnTest.innerHTML = "测试";

  // 设置按钮样式
  btnScroll.style.position = "fixed";
  btnScroll.style.top = "160px";
  btnScroll.style.right = "20px";
  btnScroll.style.backgroundColor = "#056b00";
  btnScroll.style.color = "#fff";
  btnScroll.style.padding = "8px";
  btnScroll.style.borderRadius = "6px";
  btnScroll.style.zIndex = "1000";

  btnJump.style.position = "fixed";
  btnJump.style.top = "210px";
  btnJump.style.right = "20px";
  btnJump.style.backgroundColor = "#056b00";
  btnJump.style.color = "#fff";
  btnJump.style.padding = "8px";
  btnJump.style.borderRadius = "6px";
  btnJump.style.zIndex = "1000";

  btnTest.style.position = "fixed";
  btnTest.style.top = "260px";
  btnTest.style.right = "20px";
  btnTest.style.backgroundColor = "#056b00";
  btnTest.style.color = "#fff";
  btnTest.style.padding = "8px";
  btnTest.style.borderRadius = "6px";
  btnTest.style.zIndex = "1000";

  // 添加按钮到页面中
  document.body.appendChild(btnScroll);
  document.body.appendChild(btnJump);
  // document.body.appendChild(btnTest);

  let isScrolling = false;
  let timerId;

  function simulateScroll() {
    window.scrollBy(0, 200);
  }

  function startScroll() {
    if (isScrolling) {
      return;
    }
    isScrolling = true;
    btnScroll.innerHTML = "停止滚动";
    btnScroll.style.backgroundColor = "#ff2442";
    timerId = setInterval(simulateScroll, 200);
  }

  function cancelScroll() {
    if (!isScrolling) {
      return;
    }
    isScrolling = false;
    btnScroll.style.backgroundColor = "#056b00";
    btnScroll.innerHTML = "自动滚动";
    if (timerId) {
      clearInterval(timerId);
    }
  }

  // 给按钮添加点击事件
  btnScroll.addEventListener("click", function () {
    if (isScrolling) {
      cancelScroll();
    } else {
      startScroll();
    }
  });

  btnJump.addEventListener("click", function () {
    window.open(
      `https://mundane.ink/redbook/index.html?userId=${userId}`,
      "_blank"
    );
  });

  btnTest.addEventListener("click", function () {
    let tab = document.querySelectorAll(".tab-content-item")[1];
    const elements = tab.querySelectorAll("a.cover.ld.mask");
    console.log(elements);
    elements[0].click();
    let timeId = setInterval(function () {
      console.log("finding");
      let closeButton = document.querySelector("div.close-circle div.close");
      console.log(closeButton);
      if (closeButton) {
        console.log("close");
        closeButton.click();
        clearInterval(timeId);
      }
    }, 500);
  });

  const originOpen = XMLHttpRequest.prototype.open;
  const collectUrl = "//edith.xiaohongshu.com/api/sns/web/v2/note/collect";
  const feedUrl = "//edith.xiaohongshu.com/api/sns/web/v1/feed";
  XMLHttpRequest.prototype.open = function (_, url) {
    const xhr = this;
    if (url.startsWith(collectUrl) || url.startsWith(feedUrl)) {
      const getter = Object.getOwnPropertyDescriptor(
        XMLHttpRequest.prototype,
        "response"
      ).get;
      Object.defineProperty(xhr, "responseText", {
        get: () => {
          let result = getter.call(xhr);
          let myUrl = "";
          if (url.startsWith(collectUrl)) {
            myUrl = "https://mundane.ink/mail/redbook/collect/save";
            // myUrl = "http://localhost:8088/mail/redbook/collect/save";
          } else if (url.startsWith(feedUrl)) {
            // myUrl = "http://localhost:8088/mail/redbook/note/save";
            myUrl = "https://mundane.ink/mail/redbook/note/save";
          }
          try {
            // 将result发送到服务器
            GM_xmlhttpRequest({
              method: "POST",
              url: myUrl,
              headers: {
                "Content-Type": "application/json",
              },
              data: JSON.stringify({ result: result, userId: userId }),
              onload: function (response) {
                console.log("Result sent to server successfully!");
              },
            });
            if (url.startsWith(collectUrl)) {
              const obj = JSON.parse(result);
              if (!obj.data.has_more) {
                console.log("没有更多了!!!");
                cancelScroll();
              }
            }
          } catch (e) {
            console.log(e);
          }
          return result;
        },
      });
    }
    originOpen.apply(this, arguments);
  };
})();