Skip to content

Commit 30db447

Browse files
committed
✅ 添加gm notifcation示例和单元测试
1 parent 96abce7 commit 30db447

3 files changed

Lines changed: 428 additions & 30 deletions

File tree

.github/ISSUE_TEMPLATE/bug_report.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ body:
2727
1. 打开 https://example.com
2828
2. 点击页面右上角红色按钮
2929
3. 等待5秒后观察控制台
30+
validations:
31+
required: true
3032

3133
- type: markdown
3234
attributes:

example/gm_notification.js

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,63 @@
55
// @description 用来发送一个浏览器通知, 支持图标/文字/进度条(进度条只在 Chrome 有效)
66
// @author You
77
// @match https://bbs.tampermonkey.net.cn/
8-
// @grant GM_notification
8+
// @grant GM_notification
99
// ==/UserScript==
1010

11+
/**
12+
* @typedef {import('../src/types/scriptcat')} ScriptCat
13+
*/
14+
1115
let i;
1216
GM_notification({
13-
title: '倒计时',
14-
text: '准备进入倒计时,创建和获取通知id',
15-
ondone: (byUser) => {
16-
console.log('done user:', byUser);
17+
title: "倒计时",
18+
text: "准备进入倒计时,创建和获取通知id",
19+
ondone: (byUser) => {
20+
console.log("done user:", byUser);
21+
clearInterval(i);
22+
},
23+
onclick: () => {
24+
console.log("click");
25+
},
26+
oncreate: (id) => {
27+
let t = 1;
28+
i = setInterval(() => {
29+
GM_updateNotification(id, {
30+
title: "倒计时",
31+
text: 60 - t + "s倒计时",
32+
progress: (100 / 60) * t,
33+
});
34+
if (t == 60) {
1735
clearInterval(i);
18-
},
19-
onclick: () => {
20-
console.log('click');
21-
},
22-
oncreate: (id) => {
23-
let t = 1;
24-
i = setInterval(() => {
25-
GM_updateNotification(id, {
26-
title: '倒计时',
27-
text: (60 - t) + 's倒计时',
28-
progress: 100 / 60 * t
29-
});
30-
if (t == 60) {
31-
clearInterval(i);
32-
GM_updateNotification(id, {
33-
title: '倒计时',
34-
text: '倒计时结束',
35-
progress: 100
36-
});
37-
}
38-
t++;
39-
}, 1000);
40-
},
41-
// 开启进度条模式
42-
progress: 0,
36+
GM_updateNotification(id, {
37+
title: "倒计时",
38+
text: "倒计时结束",
39+
progress: 100,
40+
});
41+
}
42+
t++;
43+
}, 1000);
44+
},
45+
// 开启进度条模式
46+
progress: 0,
47+
});
48+
49+
// 示例2: 综合功能通知 - 使用更多特性
50+
GM_notification({
51+
title: "综合功能通知",
52+
text: "这是一个展示多种特性的通知示例",
53+
tag: "feature-demo", // 使用相同的tag可以覆盖之前的通知,否则会创建新的通知
54+
image: "https://bbs.tampermonkey.net.cn/favicon.ico", // 自定义图标
55+
timeout: 10000, // 10秒后自动关闭
56+
url: "https://bbs.tampermonkey.net.cn/", // 关联URL
57+
onclick: (event) => {
58+
console.log("通知被点击:", event);
59+
// event.preventDefault(); // 阻止打开url
60+
},
61+
oncreate: (event) => {
62+
console.log("综合功能通知已创建,ID:", event.id);
63+
},
64+
ondone: (user) => {
65+
console.log("综合功能通知完成,用户操作:", user);
66+
},
4367
});

0 commit comments

Comments
 (0)