Greasy Fork

Greasy Fork is available in English.

获取竞价数据(shop.48.cn)

自动提取 Cookie 并请求竞价数据接口

当前为 2025-06-27 提交的版本,查看 最新版本

// ==UserScript==
// @name         获取竞价数据(shop.48.cn)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动提取 Cookie 并请求竞价数据接口
// @author       GPT
// @match        https://shop.48.cn/pai/item/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // ✅ 获取 URL 中的商品 ID
    function getItemIdFromURL() {
        const match = window.location.href.match(/item\/(\d+)/);
        return match ? match[1] : null;
    }

    // ✅ 构建 POST 参数
    function buildParams(itemId) {
        return new URLSearchParams({
            id: itemId,
            numPerPage: 20,
            pageNum: 0,
            r: Math.random().toString()
        });
    }

    // ✅ 获取指定 Cookie 值
    function getCookie(name) {
        const cookies = document.cookie.split("; ");
        for (const cookie of cookies) {
            const [key, value] = cookie.split("=");
            if (key === name) return value;
        }
        return null;
    }

    // ✅ 构建 Cookie 请求头
    function buildCookieHeader() {
        const cookieNames = [
            "Hm_lvt_f32737cfa62ed971bb3185792d3204eb",
            "route",
            ".AspNet.ApplicationCookie",
            "HMACCOUNT",
            "__RequestVerificationToken",
            "Hm_lpvt_f32737cfa62ed971bb3185792d3204eb"
        ];

        const cookieMap = {};
        cookieNames.forEach(name => {
            const value = getCookie(name);
            if (value) {
                cookieMap[name] = value;
            } else {
                console.warn(`未找到 cookie: ${name}`);
            }
        });

        if (Object.keys(cookieMap).length === 0) return null;

        return Object.entries(cookieMap)
            .map(([k, v]) => `${k}=${v}`)
            .join("; ");
    }

    // ✅ 发起 POST 请求
    async function fetchBids(params, cookieHeader) {
        const response = await fetch("https://shop.48.cn/pai/GetShowBids", {
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Cookie": cookieHeader
            },
            body: params
        });

        if (!response.ok) {
            throw new Error(`请求失败,状态码 ${response.status}`);
        }

        return await response.json();
    }
    
    // ✅ 主执行函数
    async function run() {
        const itemId = getItemIdFromURL();
        if (!itemId) {
            console.warn("未找到商品 ID");
            return;
        }

        const params = buildParams(itemId);
        const cookieHeader = buildCookieHeader();

        if (!cookieHeader) {
            console.error("未能获取必要的 cookie,可能是未登录");
            return;
        }

        try {
            const data = await fetchBids(params, cookieHeader);
            console.log("竞价数据:", data);
        } catch (error) {
            console.error("竞价请求失败:", error);
        }
    }

    // ✅ 启动脚本
    run();

})();