小红书转发

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

目前為 2023-04-23 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         小红书转发
// @namespace    https://mundane.ink/redbook
// @version      1.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 = '去下载';

    // 设置按钮样式
    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';

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

    let isScrolling = false;
    let timerId;

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

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

    function cancelScroll() {
        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');
    });


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