琦玉 发表于 2023-7-21 10:38:24

很厉害的,大佬,好棒,使用简单很多

王一之 发表于 2025-5-29 11:48:27

给哥哥提供一个完整示例:

```js
// ==UserScript==
// @name         gmCookie test
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.1.0
// @descriptiontry to take over the world!
// @author       You
// @match      https://bbs.tampermonkey.net.cn/*
// @require      https://scriptcat.org/lib/721/1.0.1/gmCookie.js
// @grant      GM_cookie
// @connect      www.baidu.com
// ==/UserScript==

console.log('gmcookie start');

gmCookie('https://www.baidu.com/').then(async cookie => {
    // 读取cookie
    console.log(cookie);
    // 修改cookie
    cookie.BAIDUID.value = 'hello';
    // 新增cookie
    cookie.test = {
      path: '/',
      value: 'world'
    };
    // 删除cookie
    delete cookie.BAIDUID;
    // 由于GM_cookie是异步,调用$alldone方法可以等待所有操作完成
    await cookie.$alldone();
});
```

tfsn20 发表于 2025-6-25 12:32:16

本帖最后由 tfsn20 于 2025-6-25 12:34 编辑

@cxxjackie 你好,最近脚本猫自动更新了,导致使用你的库会发生下面错误。
!(data/attachment/forum/202506/25/123008w3hv52vh5mmezqji.png)
我的代码:
```
const tools = {
      gmCookie: async url => {
            if (!window.GM_cookie) return console.error('缺少GM_cookie,请先通过@grant引入!');
            return new Promise((resolve, reject) => {
                GM_cookie('list', { url }, (cookie, error) => {
                  if (error || !Array.isArray(cookie)) {
                        reject(error);
                  } else {
                        const promises = [];
                        async function alldone() {
                            await Promise.all(promises);
                            promises.length = 0;
                        }
                        function proxySet(target, prop, value) {
                            if (prop !== 'name' && target !== value) {
                              promises.push(new Promise(resolve => {
                                    GM_cookie('set', { ...target, url }, resolve);
                              }));
                              target = value;
                            }
                        }
                        const cookieObj = { $alldone: alldone };
                        for (const item of cookie) {
                            cookieObj = new Proxy(item, { set: proxySet });
                        }
                        resolve(new Proxy(cookieObj, {
                            set: function (target, prop, value) {
                              value.name = prop;
                              promises.push(new Promise(resolve => {
                                    GM_cookie('set', { ...value, url }, resolve);
                              }));
                              target = new Proxy(value, { set: proxySet });
                            },
                            deleteProperty: function (target, prop) {
                              promises.push(new Promise(resolve => {
                                    GM_cookie('delete', { ...target, url }, resolve);
                              }));
                              return delete target;
                            }
                        }));
                  }
                });
            });
      },
}
```
调用函数
```
      let cookies = await tools.gmCookie('http://cgyy.zzuli.edu.cn/');
      for (let key in cookies) {
            delete cookies;
      }
```

cxxjackie 发表于 2025-6-25 22:51:34

tfsn20 发表于 2025-6-25 12:32
@cxxjackie 你好,最近脚本猫自动更新了,导致使用你的库会发生下面错误。
!

感谢反馈,这个问题是因为直接遍历会把$alldone当成cookie导致的,1.0.2已修复。

s11s 发表于 2025-11-10 23:57:43

本帖最后由 s11s 于 2025-11-10 23:59 编辑

各位油友, 请问下面的代码 ,为什么 gmCookie中 log cookie的代码没有执行,但是也没有报错,我哪里下写错了吗?

<div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 254); font-family: Consolas, &quot;Courier New&quot;, monospace; line-height: 19px; white-space: pre;">
<div><span style="color: #008000;">// ==UserScript==</span>
<span style="color: #008000;">// @name         Bilibili Cookie 自动上报插件</span>
<span style="color: #008000;">// @namespace    http://tampermonkey.net/</span>
<span style="color: #008000;">// @version      1.0</span>
<span style="color: #008000;">// @description检测登录后自动将 Bilibili Cookie 发送到本地 FastAPI 接口</span>
<span style="color: #008000;">// @author       YourName</span>
<span style="color: #008000;">// @grant      GM_cookie</span>
<span style="color: #008000;">// @require      https://scriptcat.org/lib/721/1.0.2/gmCookie.js</span>
<span style="color: #008000;">// @match      https://www.bilibili.com/*</span>
<span style="color: #008000;">// @match      https://space.bilibili.com/*</span>
<span style="color: #008000;">// @grant      GM_xmlhttpRequest</span>
<span style="color: #008000;">// @connect      192.168.0.101</span>
<span style="color: #008000;">// ==/UserScript==</span>



(<span style="color: #0000ff;">function</span> () {
    <span style="color: #a31515;">'use strict'</span>;

    gmCookie(<span style="color: #a31515;">'https://www.bilibili.com/'</span>).then(<span style="color: #0000ff;">async</span> cookie => {
      <span style="color: #008000;">// 读取cookie</span>
      console.log(cookie);
    });</div>
</div>

s11s 发表于 2025-11-11 10:28:32

本帖最后由 s11s 于 2025-11-11 10:32 编辑

> 本帖最后由 s11s 于 2025-11-11 10:31 编辑

给哥哥提供一个完整示例:

```js

王老师,这个示例,运行会报错了,控制台日志如下:
gmcookie start
gmCookie test.user.js:19Uncaught (in promise) ReferenceError: gmCookie is not defined
    at Window.<anonymous> (gmCookie test.user.js:19:1)
    at window.-mhty8p7n.blr257db (gmCookie test.user.js:35:8)
    at Y.exec (417cdef9-d665-4668-a6aa-79d0e4545277:2:32576)
    at Object.execScript (417cdef9-d665-4668-a6aa-79d0e4545277:2:41298)
    at set (417cdef9-d665-4668-a6aa-79d0e4545277:2:39963)
    at gmCookie test.user.js:
```!(data/attachment/forum/202511/11/103235dplq0h5uuqkjnd7l.png)

王一之 发表于 2025-11-11 11:53:19

s11s 发表于 2025-11-11 10:28
> 本帖最后由 s11s 于 2025-11-11 10:31 编辑

给哥哥提供一个完整示例:


打开再关闭一下脚本呢?看起来是@require资源没加载成功,可能和这段时间被攻击,我开启了waf有关
页: 1 [2]
查看完整版本: 封装了一下GM_cookie