-
Notifications
You must be signed in to change notification settings - Fork 310
GM_openInTab #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GM_openInTab #788
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -809,38 +809,77 @@ export default class GMApi { | |||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| @PermissionVerify.API({}) | ||||||||||||||||||||||||||||||||||||||||
| async GM_openInTab(request: Request, sender: IGetSender) { | ||||||||||||||||||||||||||||||||||||||||
| const url = request.params[0]; | ||||||||||||||||||||||||||||||||||||||||
| const options = request.params[1] || {}; | ||||||||||||||||||||||||||||||||||||||||
| if (options.useOpen === true) { | ||||||||||||||||||||||||||||||||||||||||
| // 发送给offscreen页面处理 | ||||||||||||||||||||||||||||||||||||||||
| const ok = await sendMessage(this.msgSender, "offscreen/gmApi/openInTab", { url }); | ||||||||||||||||||||||||||||||||||||||||
| if (ok) { | ||||||||||||||||||||||||||||||||||||||||
| // 由于window.open强制在前台打开标签,因此获取状态为{ active:true }的标签即为新标签 | ||||||||||||||||||||||||||||||||||||||||
| const tab = await getCurrentTab(); | ||||||||||||||||||||||||||||||||||||||||
| await cacheInstance.set(`GM_openInTab:${tab.id}`, { | ||||||||||||||||||||||||||||||||||||||||
| uuid: request.uuid, | ||||||||||||||||||||||||||||||||||||||||
| sender: sender.getExtMessageSender(), | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| return tab.id; | ||||||||||||||||||||||||||||||||||||||||
| const url = request.params[0] as string; | ||||||||||||||||||||||||||||||||||||||||
| const options = (request.params[1] || {}) as GMTypes.OpenTabOptions & | ||||||||||||||||||||||||||||||||||||||||
| Required<Pick<GMTypes.OpenTabOptions, "active">>; | ||||||||||||||||||||||||||||||||||||||||
| const getNewTabId = async () => { | ||||||||||||||||||||||||||||||||||||||||
| if (options.useOpen === true) { | ||||||||||||||||||||||||||||||||||||||||
| // 发送给offscreen页面处理 (使用window.open) | ||||||||||||||||||||||||||||||||||||||||
| const ok = await sendMessage(this.msgSender, "offscreen/gmApi/openInTab", { url }); | ||||||||||||||||||||||||||||||||||||||||
| if (ok) { | ||||||||||||||||||||||||||||||||||||||||
| // 由于window.open强制在前台打开标签,因此获取状态为{ active:true }的标签即为新标签 | ||||||||||||||||||||||||||||||||||||||||
| const tab = await getCurrentTab(); | ||||||||||||||||||||||||||||||||||||||||
| return tab.id; | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| // 当新tab被浏览器阻止时window.open()会返回null 视为已经关闭 | ||||||||||||||||||||||||||||||||||||||||
| // 似乎在Firefox中禁止在background页面使用window.open(),强制返回null | ||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| // 当新tab被浏览器阻止时window.open()会返回null 视为已经关闭 | ||||||||||||||||||||||||||||||||||||||||
| // 似乎在Firefox中禁止在background页面使用window.open(),强制返回null | ||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||
| const { tabId, windowId } = sender.getExtMessageSender(); | ||||||||||||||||||||||||||||||||||||||||
| const active = options.active; | ||||||||||||||||||||||||||||||||||||||||
| const currentTab = await chrome.tabs.get(tabId); | ||||||||||||||||||||||||||||||||||||||||
| let newTabIndex = -1; | ||||||||||||||||||||||||||||||||||||||||
| if (options.incognito && !currentTab.incognito) { | ||||||||||||||||||||||||||||||||||||||||
| // incognito: "split" 在 normal 里不会看到 incognito | ||||||||||||||||||||||||||||||||||||||||
| // 只能创建新 incognito window | ||||||||||||||||||||||||||||||||||||||||
| // pinned 无效 | ||||||||||||||||||||||||||||||||||||||||
| // insert 不重要 | ||||||||||||||||||||||||||||||||||||||||
| await chrome.windows.create({ | ||||||||||||||||||||||||||||||||||||||||
| url, | ||||||||||||||||||||||||||||||||||||||||
| incognito: true, | ||||||||||||||||||||||||||||||||||||||||
| focused: active, | ||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| if ((typeof options.insert === "number" || options.insert === true) && currentTab && currentTab.index >= 0) { | ||||||||||||||||||||||||||||||||||||||||
| // insert 为 boolean 时,插入至当前Tab下一格 (TM行为) | ||||||||||||||||||||||||||||||||||||||||
| // insert 为 number 时,插入至相对位置 (SC独自) | ||||||||||||||||||||||||||||||||||||||||
| const insert = +options.insert; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| const insert = +options.insert; | |
| const insert = options.insert === true ? 1 : +options.insert; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JavaScript +options.insert 就是 1
Copilot
AI
Oct 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
条件判断逻辑复杂且容易出错。建议重构为更清晰的结构,先检查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; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要冗长代码。原代码足够清晰
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在处理active选项时缺少默认值处理。根据内容脚本中的逻辑,当active未定义时应该默认为true,但这里直接使用可能导致undefined值传递给Chrome API。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
content 的 GM_openInTab API 有处理default值问题。TypeScript这里要加 !
默认值处理 只在 content / service_worker 其中一边做,否则会很混乱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0192500