Skip to content

[BUG] GM_cookie 與 油猴 有偏差 #520

@cyfung1031

Description

@cyfung1031

Problem Description

// ==UserScript==
// @name         Full GM_cookie & GM.cookie API Demo
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Demonstrates all GM_cookie and GM.cookie APIs including functions GM_cookie and GM.cookie directly
// @author       OpenAI
// @match        https://example.com/*
// @grant        GM_cookie
// @grant        GM.cookie
// ==/UserScript==

(function () {
    'use strict';

    const cookieNameLegacy = 'gm_test_cookie_legacy';
    const cookieNameModern = 'gm_test_cookie_modern';
    const cookieValue = 'hello_cookie';
    const domain = location.hostname;

    // === LEGACY GM_cookie (callback-based) ===
    function testGM_cookie_API() {
        console.log('===== Testing GM_cookie APIs =====');

        // Set cookie using GM_cookie.set
        GM_cookie.set({
            name: cookieNameLegacy,
            value: cookieValue,
            domain: domain,
            path: '/',
            secure: location.protocol === 'https:',
        }, function (error) {
            if (error) return console.error('GM_cookie.set error:', error);
            console.log('GM_cookie.set: Cookie set');

            // List cookies using GM_cookie.list
            GM_cookie.list({ domain }, function (cookies, error) {

                if (!error) console.log('GM_cookie.list returned:', cookies);
                else return console.error('GM_cookie.list error:', cookies, error);

                const found = cookies.find(c => c.name === cookieNameLegacy);
                console.log('GM_cookie.list result:', cookies);
                if (found) {
                    console.log(`GM_cookie.list: Found cookie: ${found.name}=${found.value}`);
                } else {
                    console.warn('GM_cookie.list: Cookie not found.');
                }

                // Delete cookie using GM_cookie.delete
                GM_cookie.delete({
                    name: cookieNameLegacy,
                    domain: domain,
                    path: '/',
                }, function (error) {
                    if (error) return console.error('GM_cookie.delete error:', error);
                    console.log('GM_cookie.delete: Cookie deleted');
                });
            });
        });
    }

    // === MODERN GM.cookie (promise-based) ===
    async function testGM_cookie_modern_API() {
        console.log('===== Testing GM.cookie APIs =====');

        // Set cookie using GM.cookie.set
        await GM.cookie.set({
            name: cookieNameModern,
            value: cookieValue,
            domain: domain,
            path: '/',
            secure: location.protocol === 'https:',
            httpOnly: false,
            sameSite: 'lax',
        });
        console.log('GM.cookie.set: Cookie set');

        // List cookies using GM.cookie.list
        const cookies = await GM.cookie.list({ domain });
        const found = cookies.find(c => c.name === cookieNameModern);
        console.log('GM.cookie.list result:', cookies);
        if (found) {
            console.log(`GM.cookie.list: Found cookie: ${found.name}=${found.value}`);
        } else {
            console.warn('GM.cookie.list: Cookie not found.');
        }

        // Delete cookie using GM.cookie.delete
        await GM.cookie.delete({
            name: cookieNameModern,
            domain: domain,
            path: '/',
        });
        console.log('GM.cookie.delete: Cookie deleted');
    }

    // === DIRECT FUNCTION CALL: GM_cookie (function) ===
    function testGM_cookie_function() {
        console.log('===== Testing GM_cookie (function) entry point =====');
        if (typeof GM_cookie === 'function') {
            GM_cookie('list', { domain }, function (cookies, error) {
                if (!error) return console.log('GM_cookie("list") returned:', cookies);
                return console.error('GM_cookie(list) error:', cookies, error);
            });
        } else {
            console.warn('GM_cookie is not a function.');
        }
    }

    // === DIRECT FUNCTION CALL: GM.cookie (function) ===
    async function testGM_cookie_function_modern() {
        console.log('===== Testing GM.cookie (function) entry point =====');
        if (typeof GM.cookie === 'function') {
            try {
                const cookies = await GM.cookie('list', { domain });
                console.log('GM.cookie("list") returned:', cookies);
            } catch (e) {
                console.error('GM.cookie("list") error:', e);
            }
        } else {
            console.warn('GM.cookie is not a function.');
        }
    }

    // Run all tests
    testGM_cookie_API();
    testGM_cookie_modern_API();
    testGM_cookie_function();
    testGM_cookie_function_modern();
})();

油猴 能執行

ScriptCat會出現 hostname must be in the definition of connect

Reproduction Steps

ScriptCat Version

0.18.x

Operating System and Browser Information

Macbook Brave

Additional Information (Optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions