迁移部分内容
Some checks failed
test / Run tests (push) Failing after 55s
build / Build (push) Failing after 1m18s

This commit is contained in:
2025-01-10 17:56:30 +08:00
parent c2e5c7600e
commit af15d67cb3
52 changed files with 6308 additions and 23 deletions

View File

@@ -0,0 +1,50 @@
// ==UserScript==
// @name cat file storage
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 脚本同步储存空间操作
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant CAT_fileStorage
// @run-at document-start
// ==/UserScript==
CAT_fileStorage("upload", {
path: "test.txt",
baseDir: "test-dir",
data: new Blob(["Hello World"]),
onload() {
CAT_fileStorage("list", {
baseDir: "test-dir",
onload(list) {
console.log(list);
list.forEach(value => {
if (value.name === "test.txt") {
CAT_fileStorage("download", {
file: value,
baseDir: "test-dir",
async onload(data) {
console.log(await data.text());
CAT_fileStorage("delete", {
path: value.name,
baseDir: "test-dir",
onload() {
console.log('ok');
}
});
}
});
}
});
}
})
}, onerror(err) {
console.log(err);
switch (err.code) {
case 1:
case 2:
CAT_fileStorage("config");
break;
}
}
})

16
example/cloudcat.js Normal file
View File

@@ -0,0 +1,16 @@
// ==UserScript==
// @name cloudscript
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 可以导出成nodejs可执行的包,在云端执行
// @author You
// @crontab * * once * *
// @cloudCat
// @exportCookie domain=.scriptscat.org
// ==/UserScript==
return new Promise((resolve, reject) => {
// Your code here...
resolve();
});

18
example/error_retry.js Normal file
View File

@@ -0,0 +1,18 @@
// ==UserScript==
// @name 重试示例
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description try to take over the world!
// @author You
// @crontab * * once * *
// @grant GM_notification
// ==/UserScript==
return new Promise((resolve, reject) => {
// Your code here...
GM_notification({
title: "retry",
text: "10秒后重试"
});
reject(new CATRetryError("xxx错误", 10));
});

15
example/gm_add_element.js Normal file
View File

@@ -0,0 +1,15 @@
// ==UserScript==
// @name gm add element
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 在页面中插入元素,可以绕过CSP限制
// @author You
// @match https://github.com/scriptscat/scriptcat
// @grant GM_addElement
// ==/UserScript==
const el = GM_addElement(document.querySelector('.BorderGrid-cell'), "img", {
src: "https://bbs.tampermonkey.net.cn/uc_server/avatar.php?uid=4&size=small&ts=1"
});
console.log(el);

18
example/gm_bg_menu.js Normal file
View File

@@ -0,0 +1,18 @@
// ==UserScript==
// @name bg gm menu
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 在后台脚本中使用菜单
// @author You
// @background
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// ==/UserScript==
return new Promise((resolve) => {
const id = GM_registerMenuCommand("测试菜单", () => {
console.log(id);
GM_unregisterMenuCommand(id);
resolve();
}, "z");
});

11
example/gm_clipboard.js Normal file
View File

@@ -0,0 +1,11 @@
// ==UserScript==
// @name gm clipboard
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description try to take over the world!
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant GM_setClipboard
// ==/UserScript==
GM_setClipboard("我爱ScriptCat");

44
example/gm_cookie.js Normal file
View File

@@ -0,0 +1,44 @@
// ==UserScript==
// @name New Userscript
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 可以控制浏览器的cookie, 必须指定@connect, 并且每次一个新的域调用都需要用户确定
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant GM_cookie
// @connect example.com
// ==/UserScript==
// GM_cookie("store") 方法请看gm_value.js的例子, 可用于隐身窗口的操作
GM_cookie("set", {
url: "http://example.com/cookie",
name: "cookie1", value: "value"
}, () => {
GM_cookie("set", {
url: "http://www.example.com/",
domain: ".example.com", path: "/path",
name: "cookie2", value: "path"
}, () => {
GM_cookie("list", {
domain: "example.com"
}, (cookies) => {
console.log("domain", cookies);
});
GM_cookie("list", {
url: "http://example.com/cookie",
}, (cookies) => {
console.log("domain", cookies);
});
GM_cookie("delete", {
url: "http://www.example.com/path",
name: "cookie2"
}, () => {
GM_cookie("list", {
domain: "example.com"
}, (cookies) => {
console.log("delete", cookies);
});
})
});
});

20
example/gm_download.js Normal file
View File

@@ -0,0 +1,20 @@
// ==UserScript==
// @name gm download
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description try to take over the world!
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant GM_download
// ==/UserScript==
GM_download({
url: "https://scriptcat.org/api/v1/gm_crx/download/ScriptCat",
name: "scriptcat.crx",
headers: {
"referer": "http://www.example.com/",
"origin": "www.example.com"
}, onprogress(data) {
console.log(data);
}
});

View File

@@ -0,0 +1,17 @@
// ==UserScript==
// @name gm get resource
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 通过@resource引用资源,这个资源会被管理器进行缓存,不可修改
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @resource bbs https://bbs.tampermonkey.net.cn/
// @grant GM_getResourceURL
// @grant GM_getResourceText
// ==/UserScript==
console.log(GM_getResourceURL("bbs"));
console.log(GM_getResourceURL("bbs", false));
console.log(GM_getResourceURL("bbs", true));
console.log(GM_getResourceText("bbs"));

11
example/gm_log.js Normal file
View File

@@ -0,0 +1,11 @@
// ==UserScript==
// @name gm log
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 日志功能,为你的脚本加上丰富的日志吧,支持日志分级与日志标签
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant GM_log
// ==/UserScript==
GM_log("log message", "info", { component: "example" });

16
example/gm_menu.js Normal file
View File

@@ -0,0 +1,16 @@
// ==UserScript==
// @name gm menu
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 创建菜单, 可以显示在右上角的插件弹出页和浏览器右键菜单中
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// ==/UserScript==
const id = GM_registerMenuCommand("测试菜单", () => {
console.log(id);
GM_unregisterMenuCommand(id);
}, "h");

View File

@@ -0,0 +1,43 @@
// ==UserScript==
// @name gm notification
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 用来发送一个浏览器通知, 支持图标/文字/进度条(进度条只在 Chrome 有效)
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant GM_notification
// ==/UserScript==
let i;
GM_notification({
title: '倒计时',
text: '准备进入倒计时,创建和获取通知id',
ondone: (byUser) => {
console.log('done user:', byUser);
clearInterval(i);
},
onclick: () => {
console.log('click');
},
oncreate: (id) => {
let t = 1;
i = setInterval(() => {
GM_updateNotification(id, {
title: '倒计时',
text: (60 - t) + 's倒计时',
progress: 100 / 60 * t
});
if (t == 60) {
clearInterval(i);
GM_updateNotification(id, {
title: '倒计时',
text: '倒计时结束',
progress: 100
});
}
t++;
}, 1000);
},
// 开启进度条模式
progress: 0,
});

21
example/gm_save_tab.js Normal file
View File

@@ -0,0 +1,21 @@
// ==UserScript==
// @name gm get/save tab
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 用于保存当前标签页的数据, 关闭后会自动删除, 可以获取其它标签页的数据
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant GM_saveTab
// @grant GM_getTab
// @grant GM_getTabs
// ==/UserScript==
GM_saveTab({ test: "save" });
GM_getTab(data => {
console.log(data);
});
GM_getTabs(data => {
console.log(data);
});

20
example/gm_tab.js Normal file
View File

@@ -0,0 +1,20 @@
// ==UserScript==
// @name gm open tab
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 打开一个标签页
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @grant GM_openInTab
// ==/UserScript==
const tab = GM_openInTab("https://scriptcat.org/search");
tab.onclose = () => {
console.log("close");
}
setTimeout(() => {
tab.close();
}, 3000)

32
example/gm_value.js Normal file
View File

@@ -0,0 +1,32 @@
// ==UserScript==
// @name gm value
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 可以持久化存储数据, 并且可以监听数据变化
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @run-at document-start
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addValueChangeListener
// @grant GM_listValues
// @grant GM_deleteValue
// @grant GM_cookie
// ==/UserScript==
GM_addValueChangeListener("test_set", function (name, oldval, newval, remote, tabid) {
GM_cookie("store", tabid,(storeId) => {
console.log("store",storeId);
});
});
setInterval(() => {
console.log(GM_getValue("test_set"));
console.log(GM_listValues());
}, 2000);
setTimeout(() => {
GM_deleteValue("test_set");
}, 3000);
GM_setValue("test_set", new Date().getTime());

36
example/gm_xhr.js Normal file
View File

@@ -0,0 +1,36 @@
// ==UserScript==
// @name gm xhr
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 无视浏览器的cors的跨域请求,可以设置各种unsafeHeader与cookie,需要使用@connect获取权限,或者由用户确认
// @author You
// @grant GM_xmlhttpRequest
// @match https://bbs.tampermonkey.net.cn/
// @connect tampermonkey.net.cn
// ==/UserScript==
const data = new FormData();
data.append("username", "admin");
GM_xmlhttpRequest({
url: "https://bbs.tampermonkey.net.cn/",
method: "POST",
responseType: "blob",
data: data,
headers: {
"referer": "http://www.example.com/",
"origin": "www.example.com",
// 为空将不会发送此header
"sec-ch-ua-mobile": "",
},
onload(resp) {
console.log("onload", resp);
},
onreadystatechange(resp) {
console.log("onreadystatechange", resp);
},
onloadend(resp) {
console.log("onloadend", resp);
},
});

92
example/userconfig.js Normal file
View File

@@ -0,0 +1,92 @@
// ==UserScript==
// @name userconfig
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 会在页面上显示用户配置,可以可视化的进行配置
// @author You
// @background
// @grant GM_getValue
// @grant CAT_userConfig
// ==/UserScript==
/* ==UserConfig==
group1:
configA: # 键值为group.config,例如本键为:group1.configA
title: 配置A # 配置的标题
description: 这是一个文本类型的配置 # 配置的描述内容
type: text # 选项类型,如果不填写会根据数据自动识别
default: 默认值 # 配置的默认值
min: 2 # 文本最短2个字符
max: 18 # 文本最长18个字符
password: true # 设置为密码
configB:
title: 配置B
description: 这是一个选择框的配置
type: checkbox
default: true
configC:
title: 配置C
description: 这是一个列表选择的配置
type: select
default: 1
values: [1,2,3,4,5]
configD:
title: 配置D
description: 这是一个动态列表选择的配置
type: select
bind: $cookies # 动态显示绑定的values,值是以$开头的key,value需要是一个数组
configE:
title: 配置E
description: 这是一个多选列表的配置
type: mult-select
default: [1]
values: [1,2,3,4,5]
configF:
title: 配置F
description: 这是一个动态多选列表的配置
type: mult-select
bind: $cookies
configG:
title: 配置G
description: 这是一个数字的配置
type: number
default: 11
min: 10 # 最小值
max: 16 # 最大值
unit: 分 # 表示单位
configH:
title: 配置H
description: 这是一个长文本类型的配置
type: textarea
default: 默认值
rows: 6
---
group2:
configX:
title: 配置A
description: 这是一个文本类型的配置
default: 默认值
==/UserConfig== */
// 通过GM_info新方法获取UserConfig对象
const rawUserConfig = GM_info.userConfig;
// 定义一个对象暂存读取到的UserConfig值
const userConfig = {};
// 解构遍历读取UserConfig并赋缺省值
Object.entries(rawUserConfig).forEach(([mainKey, configs]) => {
Object.entries(configs).forEach(([subKey, { default: defaultValue }]) => {
userConfig[`${mainKey}.${subKey}`] = GM_getValue(`${mainKey}.${subKey}`, defaultValue)
})
})
setInterval(() => {
// 传统方法读取UserConfig每个缺省值需要单独静态声明修改UserConfig缺省值后代码也需要手动修改
console.log(GM_getValue("group1.configA", "默认值"));
console.log(GM_getValue("group1.configG", 11));
// GM_info新方法读取UserConfig可直接关联读取缺省值无需额外修改
console.log(userConfig["group1.configA"]);
console.log(userConfig["group1.configG"]);
}, 5000)
// 打开用户配置
CAT_userConfig();

View File

@@ -0,0 +1,8 @@
// ==UserSubscribe==
// @name 订阅脚本
// @description 可以通过指定脚本url订阅一系列的脚本
// @version 1.0.0
// @author You
// @connect www.baidu.com
// @scriptUrl https://scriptcat.org/scripts/code/22/test.user.js
// ==/UserSubscribe==

13
example/vscode.user.js Normal file
View File

@@ -0,0 +1,13 @@
// ==UserScript==
// @name vscode 同步测试
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description vscode scriptcat 插件同步测试
// @author You
// @match https://bbs.tampermonkey.net.cn/
// ==/UserScript==
(function() {
'use strict';
// Your code here...
})();