Conversation
src/types/scriptcat.d.ts
Outdated
| active?: boolean; // FM & TM & VM; TM default false | ||
| insert?: boolean; // FM & TM & VM; TM default true | ||
| setParent?: boolean; // FM & TM | ||
| incognito?: boolean; // FM & TM | ||
| loadInBackground?: boolean; // TM - A boolean value has the opposite meaning of active (just for TM. Not Recommended) | ||
| pinned?: boolean; // FM & VM |
There was a problem hiding this comment.
Pull Request Overview
这个PR实现了GM_openInTab API的完整功能,支持多种选项参数以提供更精细的标签页控制。
- 更新类型定义文件,为OpenTabOptions接口添加详细的中文文档和新的选项字段
- 重构服务工作者中的GM_openInTab实现,增加对insert、setParent、incognito、pinned等选项的支持
- 修改内容脚本中的参数处理逻辑,改进active和loadInBackground的兼容性
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/template/scriptcat.d.tpl | 为OpenTabOptions接口添加完整的类型定义和中文文档,包括active、insert、setParent、incognito、loadInBackground、pinned、useOpen等选项 |
| src/app/service/service_worker/gm_api.ts | 重构GM_openInTab方法实现,添加对所有新选项的处理逻辑,包括隐私窗口创建、标签页位置插入、父子关系设置等 |
| src/app/service/content/gm_api.ts | 改进参数处理逻辑,修复active和loadInBackground的兼容性问题,并修正代码注释中的简繁体混用 |
| // 似乎在Firefox中禁止在background页面使用window.open(),强制返回null | ||
| return false; | ||
| const { tabId, windowId } = sender.getExtMessageSender(); | ||
| const active = options.active; |
There was a problem hiding this comment.
在处理active选项时缺少默认值处理。根据内容脚本中的逻辑,当active未定义时应该默认为true,但这里直接使用可能导致undefined值传递给Chrome API。
| const active = options.active; | |
| const active = options.active ?? true; |
There was a problem hiding this comment.
content 的 GM_openInTab API 有处理default值问题。TypeScript这里要加 !
默认值处理 只在 content / service_worker 其中一边做,否则会很混乱
| if ((typeof options.insert === "number" || options.insert === true) && currentTab && currentTab.index >= 0) { | ||
| // insert 为 boolean 时,插入至当前Tab下一格 (TM行为) | ||
| // insert 为 number 时,插入至相对位置 (SC独自) | ||
| const insert = +options.insert; |
There was a problem hiding this comment.
当options.insert为boolean类型时,使用+操作符转换会得到0或1,但对于true应该表示插入到当前标签后一格。应该使用条件判断:const insert = options.insert === true ? 1 : +options.insert;
| const insert = +options.insert; | |
| const insert = options.insert === true ? 1 : +options.insert; |
There was a problem hiding this comment.
JavaScript +options.insert 就是 1
| if ((typeof options.insert === "number" || options.insert === true) && currentTab && currentTab.index >= 0) { | ||
| // insert 为 boolean 时,插入至当前Tab下一格 (TM行为) | ||
| // insert 为 number 时,插入至相对位置 (SC独自) | ||
| const insert = +options.insert; | ||
| newTabIndex = currentTab.index + insert; | ||
| if (newTabIndex < 0) newTabIndex = 0; |
There was a problem hiding this comment.
条件判断逻辑复杂且容易出错。建议重构为更清晰的结构,先检查options.insert的类型,然后分别处理boolean和number的情况。
| if ((typeof options.insert === "number" || options.insert === true) && currentTab && currentTab.index >= 0) { | |
| // insert 为 boolean 时,插入至当前Tab下一格 (TM行为) | |
| // insert 为 number 时,插入至相对位置 (SC独自) | |
| const insert = +options.insert; | |
| newTabIndex = currentTab.index + insert; | |
| if (newTabIndex < 0) newTabIndex = 0; | |
| if (currentTab && currentTab.index >= 0) { | |
| let insert: number | undefined; | |
| if (typeof options.insert === "number") { | |
| // insert 为 number 时,插入至相对位置 (SC独自) | |
| insert = options.insert; | |
| } else if (options.insert === true) { | |
| // insert 为 boolean 时,插入至当前Tab下一格 (TM行为) | |
| insert = 1; | |
| } | |
| if (typeof insert === "number") { | |
| newTabIndex = currentTab.index + insert; | |
| if (newTabIndex < 0) newTabIndex = 0; | |
| } |
|
如沒問題,可合併PR |
概述
关联的 issue: #685
https://www.tampermonkey.net/documentation.php?locale=en#api:GM_openInTab
active
falsetruetrueinsert
原描述有误,TM 实际为 boolean,预设
true(与VM 一致)应与 TM 一致,改成 default true(让 SC 与 TM、VM 保持一致)
若
insert为数字,则为 SC 独有功能,相对于现有 tab:insert = -1→ 加在当前 tab 前两格insert = 0→ 加在当前 tab 前一格insert = 1→ 加在当前 tab 后一格 =insert = trueinsert = 2→ 加在当前 tab 后两格setParent
falseopenerTabId问题,不跟随 TM,改为 default trueincognito
由于
manifest中设定incognito: "split",tabId、windowId皆无法取得loadInBackground
active相反active为优先另外加入 FM&VM 的 pinned . 见 https://violentmonkey.github.io/api/gm/
变更内容
截图