From f2b67222b1179095db0ac04d8c7a1db78ce4e951 Mon Sep 17 00:00:00 2001 From: DreamNya <34838824+DreamNya@users.noreply.github.com> Date: Wed, 26 Apr 2023 16:50:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0GM=5FopenInTab=E6=96=B0?= =?UTF-8?q?=E5=8F=AF=E9=80=89=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在background页面使用window.open代替chrome.tabs.create打开目标url 兼容onclose 使用方法:参数中主动加入useOpen: true 以启用 意义:chrome.tabs.create在http页面表现情况与https不同。这一可选功能能够绕开http页面限制,让用户在extension插件页面中将url打开 注:这是一个实验/不兼容其他管理器的功能 --- src/runtime/background/gm_api.ts | 23 +++++++++++++++++++---- src/types/scriptcat.d.ts | 1 + 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/runtime/background/gm_api.ts b/src/runtime/background/gm_api.ts index 365e58c29..8d70c9b84 100644 --- a/src/runtime/background/gm_api.ts +++ b/src/runtime/background/gm_api.ts @@ -469,10 +469,25 @@ 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) { + const newWindow = window.open(url); + if (newWindow) { + // 使用onunload监听新tab关闭 + newWindow.window.onunload = () => { + channel.send({ event: "onclose" }); + channel.disChannel(); + }; + } else { + // 当新tab被浏览器阻止时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({ diff --git a/src/types/scriptcat.d.ts b/src/types/scriptcat.d.ts index b286989e1..b8ad330d3 100644 --- a/src/types/scriptcat.d.ts +++ b/src/types/scriptcat.d.ts @@ -327,6 +327,7 @@ declare namespace GMTypes { active?: boolean; insert?: boolean; setParent?: boolean; + useOpen?: boolean; } interface XHRResponse { From 4ab0d58a21569d0d9e790e4a3b2ebaa237f2f5ea Mon Sep 17 00:00:00 2001 From: DreamNya <34838824+DreamNya@users.noreply.github.com> Date: Thu, 27 Apr 2023 14:02:29 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96useOpen=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E7=9B=91=E5=90=AC=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/runtime/background/gm_api.ts | 12 +++++++----- src/types/scriptcat.d.ts | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/runtime/background/gm_api.ts b/src/runtime/background/gm_api.ts index 8d70c9b84..7ab1ac2db 100644 --- a/src/runtime/background/gm_api.ts +++ b/src/runtime/background/gm_api.ts @@ -472,13 +472,15 @@ export default class GMApi { if (options.useOpen === true) { const newWindow = window.open(url); if (newWindow) { - // 使用onunload监听新tab关闭 - newWindow.window.onunload = () => { - channel.send({ event: "onclose" }); - channel.disChannel(); - }; + // 由于不符合同源策略无法直接监听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(); } diff --git a/src/types/scriptcat.d.ts b/src/types/scriptcat.d.ts index b8ad330d3..36b7044ee 100644 --- a/src/types/scriptcat.d.ts +++ b/src/types/scriptcat.d.ts @@ -327,7 +327,7 @@ declare namespace GMTypes { active?: boolean; insert?: boolean; setParent?: boolean; - useOpen?: boolean; + useOpen?: boolean; // 这是一个实验性/不兼容其他管理器/不兼容Firefox的功能 } interface XHRResponse {