Skip to content

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Nov 3, 2025

概述 Descriptions

close #796 #840

变更内容 Changes

用新角度处理 chrome.alarm 相关设计。 简单直接解决问题

截图 Screenshots

@cyfung1031 cyfung1031 force-pushed the pr-check_script_update_cycle-004 branch 2 times, most recently from 5e12458 to d7f90be Compare November 3, 2025 22:44
@cyfung1031 cyfung1031 requested a review from CodFrm November 3, 2025 23:14
@cyfung1031
Copy link
Collaborator Author

cyfung1031 commented Nov 3, 2025

@CodFrm

src/app/service/service_worker/index.ts

这个里面的 systemConfig 应该取 "@App/pages/store/global" 里的,还是要 const systemConfig = new SystemConfig(this.mq); 这样建一个?

import { SystemConfig } from "@App/pkg/config/config";
import { systemConfig } from "@App/pages/store/global";

const systemConfig = new SystemConfig(this.mq);

src/service_worker.ts

这个 messageQueue 是不是不用建立?
在ServiceWorkerManager里(src/app/service/service_worker/index.ts)直接取 "@App/pages/store/global" 里的?

const messageQueue = new MessageQueue();
const manager = new ServiceWorkerManager(server, messageQueue, new ServiceWorkerMessageSend());

@CodFrm
Copy link
Member

CodFrm commented Nov 4, 2025

@cyfung1031 src/app/service/service_worker/index.ts 里的 应该是用 const systemConfig = new SystemConfig(this.mq); 新建一个,不然会创建多个 mq 进行监听,global 那里的,主要是给前端页面使用的

const systemConfig = new SystemConfig(this.mq);

应该要删除import { systemConfig } from "@App/pages/store/global";,不过看起来确实容易误用,看看咋调整好点

@cyfung1031
Copy link
Collaborator Author

@cyfung1031 src/app/service/service_worker/index.ts 里的 应该是用 const systemConfig = new SystemConfig(this.mq); 新建一个,不然会创建多个 mq 进行监听,global 那里的,主要是给前端页面使用的

const systemConfig = new SystemConfig(this.mq);

应该要删除import { systemConfig } from "@App/pages/store/global";,不过看起来确实容易误用,看看咋调整好点

d6c952f 修好了

@cyfung1031 cyfung1031 force-pushed the pr-check_script_update_cycle-004 branch 2 times, most recently from 8184c05 to 8434b1f Compare November 6, 2025 05:44
@cyfung1031 cyfung1031 force-pushed the pr-check_script_update_cycle-004 branch from 6276829 to e2aae39 Compare November 15, 2025 09:41
@CodFrm CodFrm requested a review from Copilot November 15, 2025 12:12
Copilot finished reviewing on behalf of CodFrm November 15, 2025 12:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

这个 PR 重构了脚本定时更新检查(check_script_update_cycle)的实现方式,通过引入新的 regular_updatecheck.ts 模块来统一管理 chrome.alarm 的创建和触发逻辑。

主要变更:

  • 创建了新的 regular_updatecheck.ts 模块,实现了更智能的 alarm 管理逻辑,包括基于上次检查时间的计算、防止 Service Worker 启动时立即触发检查等功能
  • 将原本分散在 script.tssubscribe.ts 中的两个独立 alarm(checkScriptUpdatecheckSubscribeUpdate)合并为一个统一的 checkScriptUpdate alarm
  • 更新了所有语言的翻译文件,将 check_frequency 重命名为 script_update_check_frequency 以提高清晰度

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/app/service/service_worker/regular_updatecheck.ts 新增模块,实现统一的定时更新检查逻辑,包括 alarm 管理和更新时间戳跟踪
src/app/service/service_worker/script.ts 移除了原有的 alarm 创建代码,改为调用 initRegularUpdateCheck
src/app/service/service_worker/subscribe.ts 重构 checkSubscribeUpdate 方法,将其改为接受参数而非内部获取配置;移除了独立的 checkSubscribeUpdate alarm
src/app/service/service_worker/index.ts 重构了 alarm 触发逻辑,使用新的 onRegularUpdateCheckAlarm 函数;重构了扩展更新检查函数
src/pages/options/routes/Setting.tsx 在用户更改更新频率设置时添加了对 initRegularUpdateCheck 的调用,并更新了翻译 key
src/locales/*/translation.json 将翻译 key 从 check_frequency 更新为 script_update_check_frequency 以提高描述准确性

Comment on lines +29 to +43
chrome.alarms.create(
"checkScriptUpdate",
{
when,
periodInMinutes: targetPeriodInMinutes,
},
() => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.alarms.create:", lastError);
// Starting in Chrome 117, the number of active alarms is limited to 500. Once this limit is reached, chrome.alarms.create() will fail.
console.error("Chrome alarm is unable to create. Please check whether limit is reached.");
}
}
);
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initRegularUpdateCheck 函数中创建新的 alarm 之前,应该先清除旧的 alarm,以避免创建重复的 alarm。建议在 chrome.alarms.create 之前添加 chrome.alarms.clear("checkScriptUpdate")

示例:

chrome.alarms.clear("checkScriptUpdate");
chrome.alarms.create(
  "checkScriptUpdate",
  {
    when,
    periodInMinutes: targetPeriodInMinutes,
  },
  // ...
);

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

@cyfung1031 cyfung1031 Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

官方文档写明了 chrome.alarms.create 会把旧的清除

https://developer.chrome.com/docs/extensions/reference/api/alarms?hl=en#method-create

Creates an alarm. Near the time(s) specified by alarmInfo, the onAlarm event is fired. If there is another alarm with the same name (or no name if none is specified), it will be cancelled and replaced by this alarm.

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/alarms/create#name

Alarm names are unique within the scope of a single extension. If an alarm with an identical name exists, the existing alarm will be cleared and the alarm being created will replace it.

Comment on lines +319 to +321
Promise.resolve().then(() => {
initRegularUpdateCheck(systemConfig);
});
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在选项页面(Options Page)上下文中直接调用 initRegularUpdateCheck 存在问题。该函数修改模块级变量 allowRegularUpdateCheck,但在选项页面上下文中修改的变量不会影响到 Service Worker 上下文中的同名变量。这可能导致 Service Worker 中的 onRegularUpdateCheckAlarm 函数无法正确工作,因为它依赖 allowRegularUpdateCheck 的值来决定是否执行更新检查。

建议通过消息传递机制来触发 Service Worker 重新初始化定时检查,例如:

// 在 Service Worker 中添加消息处理
this.group.on("reinitRegularUpdateCheck", async () => {
  await initRegularUpdateCheck(this.systemConfig);
});

// 在选项页面中发送消息
scriptClient.reinitRegularUpdateCheck();

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

@cyfung1031 cyfung1031 Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没有影响。已测试过
在 扩展页面可以呼叫 initRegularUpdateCheck
alarm 呼叫回传会在 service_worker 里进行
allowRegularUpdateCheck 的值是以现在时间判断不是以 时间间隔 来判断
所以 service_worker 的 allowRegularUpdateCheck 不用更新

Comment on lines 21 to 44
if (next > now + 30000) {
when = next;
}
}
when = when || now + 6000; // 六秒后触发第一个alarm
let targetPeriodInMinutes = Math.ceil(updateCycleSecond / 60); // 分钟
targetPeriodInMinutes = Math.ceil(targetPeriodInMinutes / 5) * 5; // 5的倍数
if (targetPeriodInMinutes < 15) targetPeriodInMinutes = 15; // 至少15的分钟
chrome.alarms.create(
"checkScriptUpdate",
{
when,
periodInMinutes: targetPeriodInMinutes,
},
() => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.alarms.create:", lastError);
// Starting in Chrome 117, the number of active alarms is limited to 500. Once this limit is reached, chrome.alarms.create() will fail.
console.error("Chrome alarm is unable to create. Please check whether limit is reached.");
}
}
);
allowRegularUpdateCheck = now + 3000; // 可以触发alarm的更新程序了
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第 21 行和第 25 行使用了魔法数字 30000(30 秒)和 6000(6 秒)。这些值应该定义为常量并添加注释说明其用途。

建议在文件顶部定义常量:

// 如果距离下次检查还有超过30秒,则使用计算的时间;否则使用默认延迟
const MIN_REMAINING_TIME_MS = 30000;
// Service Worker 启动后的默认延迟时间,给予足够的初始化时间
const DEFAULT_FIRST_CHECK_DELAY_MS = 6000;
// 允许在预定时间前最多65秒内触发检查(考虑 alarm 触发时间的不精确性)
const ALARM_TRIGGER_WINDOW_MS = 65000;
// Service Worker 启动后允许执行 alarm 的延迟时间
const ALLOW_CHECK_DELAY_MS = 3000;

然后在代码中使用这些常量。

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot 的 注释没问题。跟随了 26f7e09

@CodFrm CodFrm added P0 🚑 需要紧急处理的内容 hotfix 需要尽快更新到扩展商店 labels Nov 15, 2025
cyfung1031 and others added 2 commits November 16, 2025 00:30
@cyfung1031
Copy link
Collaborator Author

@CodFrm Copilot 指出了我把 this.logger.debug 之类的删了。你有需要的话可以加一下。

@CodFrm CodFrm merged commit 760562f into scriptscat:main Nov 17, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotfix 需要尽快更新到扩展商店 P0 🚑 需要紧急处理的内容

Projects

None yet

Development

Successfully merging this pull request may close these issues.

check_script_update_cycle 未處理

2 participants