番号数据库查询与管理

在特定网页中搜索7位数字的番号,并查询是否在数据库中。未找到时可点击添加到数据库,支持备注输入。

// ==UserScript==
// @name         番号数据库查询与管理
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  在特定网页中搜索7位数字的番号,并查询是否在数据库中。未找到时可点击添加到数据库,支持备注输入。
// @author       Your Name
// @match        https://www.javbus.com/*
// @match        https://thisav.com/*
// @match        https://south-plus.org/*
// @match        https://javfree.me/*
// @grant        GM_xmlhttpRequest
// @connect      localhost
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    // 匹配7位数字的正则表达式
    const regex = /\b\d{7}\b/g;

    // 获取页面中的所有文本节点
    const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);

    // 存储已经处理过的节点,避免重复处理
    const processedNodes = new Set();

    // 遍历文本节点
    while (walker.nextNode()) {
        const node = walker.currentNode;

        // 避免重复处理相同节点
        if (processedNodes.has(node)) continue;
        processedNodes.add(node);

        // 检查文本内容是否包含7位数字
        if (regex.test(node.nodeValue)) {
            // 重置正则表达式
            regex.lastIndex = 0;

            // 获取所有匹配的番号
            const codes = node.nodeValue.match(regex);

            // 遍历匹配的番号
            codes.forEach(code => {
                // 调用数据库接口查询番号
                queryDatabase(code, node);
            });
        }
    }

    // 查询数据库
    function queryDatabase(code, textNode) {
        const apiUrl = `http://localhost:5002/check-code?code=${code}`;

        GM_xmlhttpRequest({
            method: "GET",
            url: apiUrl,
            onload: function (response) {
                try {
                    const result = JSON.parse(response.responseText);
                    if (result.exists) {
                        // 如果番号在数据库中,检查 ready_to_exe
                        if (result.data.ready_to_exe === 1) {
                            // 显示“待处理中”
                            showResult(code, "待处理中", textNode);
                        } else {
                            // 显示备注信息和是否值得观看
                            const remark = result.data.remark || "无备注";
                            const worthWatching = result.data.worth_watching ? "值得观看" : "不值得观看";
                            showResult(code, `备注: ${remark}, 是否值得观看: ${worthWatching}`, textNode);
                        }
                    } else {
                        // 如果番号不在数据库中,显示未找到并提供添加按钮
                        showResult(code, "未找到该番号", textNode, true);
                    }
                } catch (e) {
                    console.error("解析响应时出错:", e);
                    showResult(code, "解析响应失败", textNode);
                }
            },
            onerror: function (error) {
                console.error("查询数据库时发生错误:", error);
                showResult(code, "查询失败", textNode);
            }
        });
    }

    // 在匹配的字符后插入提示信息
    function showResult(code, data, textNode, isAddable = false) {
        // 创建一个 span 元素来显示结果
        const resultSpan = document.createElement("span");
        resultSpan.style.marginLeft = "5px";
        resultSpan.style.color = data.includes("待处理中") || data.includes("已添加") ? "orange" :
                                 data.includes("未找到") ? "red" : "green";

        if (isAddable) {
            // 创建两个按钮用于不同的添加操作

            // 按钮容器
            const buttonContainer = document.createElement("span");
            buttonContainer.style.marginLeft = "5px";

            // 创建“添加为值得后处理”按钮
            const addButton1 = document.createElement("button");
            addButton1.textContent = "添加为值得后处理";
            addButton1.style.marginRight = "5px";
            addButton1.style.cursor = "pointer";

            addButton1.addEventListener("click", function () {
                addCodeToDatabase(code, {
                    ready_to_exe: 1,
                    worth_watching: 1, // 根据需求调整
                    remark: ""
                }, addButton1);
            });

            // 创建“不值得后处理”按钮
            const addButton2 = document.createElement("button");
            addButton2.textContent = "添加为不值得后处理";
            addButton2.style.cursor = "pointer";

            addButton2.addEventListener("click", function () {
                // 弹出输入框,允许用户输入备注,默认为 "ug"
                let userRemark = prompt("请输入备注信息(默认为 'ug'):", "ug");
                if (userRemark === null) {
                    // 用户取消输入
                    return;
                }
                userRemark = userRemark.trim() === "" ? "ug" : userRemark.trim();

                addCodeToDatabase(code, {
                    ready_to_exe: 0,
                    worth_watching: 0,
                    remark: userRemark
                }, addButton2);
            });

            buttonContainer.appendChild(addButton1);
            buttonContainer.appendChild(addButton2);

            resultSpan.textContent = `[${data}] `;
            resultSpan.appendChild(buttonContainer);
        } else {
            resultSpan.textContent = `[${data}]`;
        }

        // 将 span 元素插入到文本节点后
        textNode.parentNode.insertBefore(resultSpan, textNode.nextSibling);
    }

    // 添加番号到数据库
    function addCodeToDatabase(code, params, buttonElement) {
        const apiUrl = `http://localhost:5002/add-code`;

        // 显示加载状态
        const originalText = buttonElement.textContent;
        buttonElement.textContent = "添加中...";
        buttonElement.disabled = true;

        GM_xmlhttpRequest({
            method: "POST",
            url: apiUrl,
            headers: {
                "Content-Type": "application/json"
            },
            data: JSON.stringify({
                code: code,
                ready_to_exe: params.ready_to_exe,
                worth_watching: params.worth_watching,
                remark: params.remark
            }),
            onload: function (response) {
                try {
                    const result = JSON.parse(response.responseText);
                    if (result.success) {
                        // 添加成功,更新显示
                        const parentSpan = buttonElement.parentNode.parentNode;
                        parentSpan.textContent = `[已添加]`;
                        parentSpan.style.color = "green";
                    } else {
                        // 添加失败,恢复按钮并显示错误信息
                        buttonElement.textContent = "添加失败";
                        buttonElement.style.color = "red";
                        console.error("添加番号失败:", result.message);
                    }
                } catch (e) {
                    console.error("解析添加响应时出错:", e);
                    buttonElement.textContent = "添加失败";
                    buttonElement.style.color = "red";
                }
            },
            onerror: function (error) {
                console.error("添加番号时发生错误:", error);
                buttonElement.textContent = "添加失败";
                buttonElement.style.color = "red";
            }
        });
    }
})();