Skip to content

Commit 4ab0d58

Browse files
committed
优化useOpen标签关闭监听方式
1 parent f2b6722 commit 4ab0d58

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/runtime/background/gm_api.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,15 @@ export default class GMApi {
472472
if (options.useOpen === true) {
473473
const newWindow = window.open(url);
474474
if (newWindow) {
475-
// 使用onunload监听新tab关闭
476-
newWindow.window.onunload = () => {
477-
channel.send({ event: "onclose" });
478-
channel.disChannel();
479-
};
475+
// 由于不符合同源策略无法直接监听newWindow关闭事件,因此改用CDP方法监听
476+
// 由于window.open强制在前台打开标签,因此获取状态为{ active:true }的标签即为新标签
477+
chrome.tabs.query({ active: true }, ([tab]) => {
478+
Cache.getInstance().set(`GM_openInTab:${tab.id}`, channel);
479+
channel.send({ event: "oncreate", tabId: tab.id });
480+
});
480481
} else {
481482
// 当新tab被浏览器阻止时window.open()会返回null 视为已经关闭
483+
// 似乎在Firefox中禁止在background页面使用window.open(),强制返回null
482484
channel.send({ event: "onclose" });
483485
channel.disChannel();
484486
}

src/types/scriptcat.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ declare namespace GMTypes {
327327
active?: boolean;
328328
insert?: boolean;
329329
setParent?: boolean;
330-
useOpen?: boolean;
330+
useOpen?: boolean; // 这是一个实验性/不兼容其他管理器/不兼容Firefox的功能
331331
}
332332

333333
interface XHRResponse {

0 commit comments

Comments
 (0)