Skip to content

Commit f2b6722

Browse files
committed
添加GM_openInTab新可选功能
在background页面使用window.open代替chrome.tabs.create打开目标url 兼容onclose 使用方法:参数中主动加入useOpen: true 以启用 意义:chrome.tabs.create在http页面表现情况与https不同。这一可选功能能够绕开http页面限制,让用户在extension插件页面中将url打开 注:这是一个实验/不兼容其他管理器的功能
1 parent d456cfd commit f2b6722

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/runtime/background/gm_api.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,25 @@ export default class GMApi {
469469
GM_openInTab(request: Request, channel: Channel) {
470470
const url = request.params[0];
471471
const options = request.params[1] || {};
472-
chrome.tabs.create({ url, active: options.active }, (tab) => {
473-
Cache.getInstance().set(`GM_openInTab:${tab.id}`, channel);
474-
channel.send({ event: "oncreate", tabId: tab.id });
475-
});
472+
if (options.useOpen === true) {
473+
const newWindow = window.open(url);
474+
if (newWindow) {
475+
// 使用onunload监听新tab关闭
476+
newWindow.window.onunload = () => {
477+
channel.send({ event: "onclose" });
478+
channel.disChannel();
479+
};
480+
} else {
481+
// 当新tab被浏览器阻止时window.open()会返回null 视为已经关闭
482+
channel.send({ event: "onclose" });
483+
channel.disChannel();
484+
}
485+
} else {
486+
chrome.tabs.create({ url, active: options.active }, (tab) => {
487+
Cache.getInstance().set(`GM_openInTab:${tab.id}`, channel);
488+
channel.send({ event: "oncreate", tabId: tab.id });
489+
});
490+
}
476491
}
477492

478493
@PermissionVerify.API({

src/types/scriptcat.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ declare namespace GMTypes {
327327
active?: boolean;
328328
insert?: boolean;
329329
setParent?: boolean;
330+
useOpen?: boolean;
330331
}
331332

332333
interface XHRResponse {

0 commit comments

Comments
 (0)