小红书转发

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

目前为 2023-04-23 提交的版本,查看 最新版本

// ==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);
    };
})();