测试中.....

// ==UserScript==
// @name         从多个网页抓取信息并保存到TXT
// @namespace    http://*
// @version      1.0
// @description  从已打开的两个网页中通过XPath抓取信息并保存到TXT文件
// @author       Jagyl
// @match        https://admin.jagyl.top/*
// @icon         https://*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // 定义要抓取的网址和对应的XPath表达式
    const pages = [
        { urlPattern: 'https://admin.jagyl.top/#/carrier/details/*', xpath: '//div[@class=\'ivu-card-body\']/form/div[2]/div[1]/div[1]/div' }
    ];

    // 存储所有数据
    let allData = '';

    // 用于存储已完成的请求计数
    let completedRequests = 0;

    // 检查是否所有请求都已完成
    function checkAllRequestsCompleted() {
        if (completedRequests === pages.length) {
            // 所有请求已完成,生成并下载TXT文件
            generateAndDownloadFile(allData);
        }
    }

    // 从给定的URL获取数据
    function fetchData(page) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: page.url,
            onload: function(response) {
                if (response.status === 200) {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(response.responseText, 'text/html');
                    const result = doc.evaluate(page.xpath, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
                    if (result) {
                        allData += `Data from ${page.url}:\n${result.textContent.trim()}\n\n`;
                    } else {
                        allData += `Error fetching data from ${page.url}: Element not found\n\n`;
                    }
                } else {
                    allData += `Error fetching ${page.url}: ${response.statusText}\n\n`;
                }
                completedRequests++;
                checkAllRequestsCompleted();
            },
            onerror: function(error) {
                allData += `Error fetching ${page.url}: ${error}\n\n`;
                completedRequests++;
                checkAllRequestsCompleted();
            }
        });
    }

    // 生成并下载TXT文件
    function generateAndDownloadFile(data) {
        const blob = new Blob([data], { type: 'text/plain' });
        const url = URL.createObjectURL(blob);

        const a = document.createElement('a');
        a.style.display = 'none';
        a.href = url;
        a.download = 'collected_data.txt';
        document.body.appendChild(a);
        a.click();

        window.URL.revokeObjectURL(url);
        document.body.removeChild(a);
    }

    // 主函数:开始获取数据
    function main() {
        allData = ''; // 清空数据
        completedRequests = 0; // 重置计数器

        for (const page of pages) {
            const url = window.location.href;
            const urlPattern = new RegExp(page.urlPattern.replace(/\*/g, '.*'));
            if (urlPattern.test(url)) {
                fetchData({ url: url, xpath: page.xpath });
            }
        }
    }

    // 创建按钮并绑定点击事件
    function createButton() {
        const button = document.createElement('button');
        button.textContent = '抓取并下载数据';
        button.style.position = 'fixed';
        button.style.bottom = '10px'; // 调整位置
        button.style.right = '10px'; // 调整位置
        button.style.padding = '10px';
        button.style.zIndex = '1000';
        button.addEventListener('click', main);

        document.body.appendChild(button);
    }

    // 只在特定页面执行
    if (window.location.href.match(/https:\/\/admin\.jagyl\.top\//)) {
        // 确保只运行一次
        if (!GM_getValue('scriptRun')) {
            GM_setValue('scriptRun', true);
            createButton();
        }
    }
})();