Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/runtime/background/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,27 @@ export default class GMApi {
GM_openInTab(request: Request, channel: Channel) {
const url = request.params[0];
const options = request.params[1] || {};
chrome.tabs.create({ url, active: options.active }, (tab) => {
Cache.getInstance().set(`GM_openInTab:${tab.id}`, channel);
channel.send({ event: "oncreate", tabId: tab.id });
});
if (options.useOpen === true) {
Copy link
Member

Choose a reason for hiding this comment

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

可修改成if (options.useOpen)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个判断我觉得不要改,限定死为true才使用新window.open比较好
万一用户加进来奇奇怪怪的参数,可能会产生无法预料到的后果,不是true的话直接忽略,用老方法,老方法也会忽略这个参数的

const newWindow = window.open(url);
if (newWindow) {
// 由于不符合同源策略无法直接监听newWindow关闭事件,因此改用CDP方法监听
// 由于window.open强制在前台打开标签,因此获取状态为{ active:true }的标签即为新标签
chrome.tabs.query({ active: true }, ([tab]) => {
Cache.getInstance().set(`GM_openInTab:${tab.id}`, channel);
channel.send({ event: "oncreate", tabId: tab.id });
});
} else {
// 当新tab被浏览器阻止时window.open()会返回null 视为已经关闭
// 似乎在Firefox中禁止在background页面使用window.open(),强制返回null
channel.send({ event: "onclose" });
channel.disChannel();
}
} else {
chrome.tabs.create({ url, active: options.active }, (tab) => {
Cache.getInstance().set(`GM_openInTab:${tab.id}`, channel);
channel.send({ event: "oncreate", tabId: tab.id });
});
}
}

@PermissionVerify.API({
Expand Down
1 change: 1 addition & 0 deletions src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ declare namespace GMTypes {
active?: boolean;
insert?: boolean;
setParent?: boolean;
useOpen?: boolean; // 这是一个实验性/不兼容其他管理器/不兼容Firefox的功能
}

interface XHRResponse {
Expand Down