Skip to content

Commit f9cb6dc

Browse files
committed
🐛 修复定时脚本计时器停止错误 #147 #149
1 parent e67883e commit f9cb6dc

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

‎src/runtime/content/gm_api.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable camelcase */
22
/* eslint-disable max-classes-per-file */
3+
import { ExtVersion } from "@App/app/const";
34
import LoggerCore from "@App/app/logger/core";
45
import { Channel, ChannelHandler } from "@App/app/message/channel";
56
import MessageContent from "@App/app/message/content";
@@ -134,16 +135,18 @@ export default class GMApi {
134135
icon: (script.metadata.icon && script.metadata.icon[0]) || null,
135136
icon64: (script.metadata.icon64 && script.metadata.icon64[0]) || null,
136137
header: metadataStr,
138+
grant: script.metadata.grant || [],
137139
};
140+
138141
return {
139142
// downloadMode
140143
// isIncognito
141-
scriptWillUpdate: false,
144+
scriptWillUpdate: true,
142145
scriptHandler: "ScriptCat",
143-
scriptUpdateURL: script.checkUpdateUrl,
146+
scriptUpdateURL: script.downloadUrl,
144147
scriptMetaStr: metadataStr,
145-
scriptSource: script.sourceCode,
146-
version: script.metadata.version && script.metadata.version[0],
148+
// scriptSource: script.sourceCode,
149+
version: ExtVersion,
147150
script: {
148151
// TODO: 更多完整的信息(为了兼容Tampermonkey,后续待定)
149152
name: script.name,

‎src/runtime/content/sandbox.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type SandboxEvent = "enable" | "disable" | "start" | "stop";
1717

1818
type Handler = (data: any) => Promise<any>;
1919

20-
// 沙盒运行环境
20+
// 沙盒运行环境, 后台脚本与定时脚本的运行环境
2121
@IoC.Singleton(MessageSandbox)
2222
export default class SandboxRuntime {
2323
message: MessageSandbox;
@@ -76,6 +76,7 @@ export default class SandboxRuntime {
7676
switch (script.type) {
7777
case SCRIPT_TYPE_CRONTAB:
7878
// 定时脚本
79+
this.stopCronJob(script.id);
7980
return this.crontabScript(script);
8081
case SCRIPT_TYPE_BACKGROUND:
8182
// 后台脚本, 直接执行脚本
@@ -86,23 +87,27 @@ export default class SandboxRuntime {
8687
}
8788

8889
disable(id: number): Promise<boolean> {
89-
const exec = this.execScripts.get(id);
90-
if (!exec) {
91-
return Promise.resolve(false);
92-
}
9390
// 停止脚本运行,主要是停止定时器
9491
// 后续考虑停止正在运行的脚本的方法
9592
// 现期对于正在运行的脚本仅仅是在background中判断是否运行
9693
// 未运行的脚本不处理GMApi的请求
97-
this.execStop(exec);
94+
this.stopCronJob(id);
95+
const exec = this.execScripts.get(id);
96+
if (!exec) {
97+
return Promise.resolve(false);
98+
}
99+
return Promise.resolve(true);
100+
}
101+
102+
// 停止计时器
103+
stopCronJob(id: number) {
98104
const list = this.cronJob.get(id);
99105
if (list) {
100106
list.forEach((val) => {
101107
val.stop();
102108
});
103109
this.cronJob.delete(id);
104110
}
105-
return Promise.resolve(true);
106111
}
107112

108113
// 执行脚本

0 commit comments

Comments
 (0)