Skip to content

Conversation

@DreamNya
Copy link
Contributor

改动内容

在background页面使用window.open代替chrome.tabs.create打开目标url
兼容onclose

使用方法

参数中主动加入useOpen: true以启用

功能意义

chrome.tabs.createhttp页面表现情况与https不同。
这一可选功能能够绕开http页面限制,让用户在extension插件页面中将url打开

附注

这是一个实验/不兼容其他管理器的功能
未测试火狐兼容性

部分应用

上图为GM_openInTab(url)http页面运行脚本打开协议url每次强制弹出确认框
下图为GM_openInTab(url,{useOpen:true}),在http页面运行脚本打开协议url只会弹出第一次确认框,勾选后后续不会再弹出
726~1XL{H74O6SJ)7GZI4S7
FOBEQP5)PO$L862VZ$GLEB3

在background页面使用window.open代替chrome.tabs.create打开目标url
兼容onclose

使用方法:参数中主动加入useOpen: true 以启用
意义:chrome.tabs.create在http页面表现情况与https不同。这一可选功能能够绕开http页面限制,让用户在extension插件页面中将url打开

注:这是一个实验/不兼容其他管理器的功能
Copy link
Member

@CodFrm CodFrm left a comment

Choose a reason for hiding this comment

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

另外关于这个的使用场景是什么呢?

建议再验证一下firefox的兼容性

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的话直接忽略,用老方法,老方法也会忽略这个参数的

@CodFrm
Copy link
Member

CodFrm commented Apr 27, 2023

另外,.d.ts标注一下,这是一个试验性质的功能吧

@codecov-commenter
Copy link

codecov-commenter commented Apr 27, 2023

Codecov Report

Patch coverage: 38.46% and project coverage change: -0.08 ⚠️

Comparison is base (d456cfd) 50.90% compared to head (d1fa4dd) 50.82%.

❗ Current head d1fa4dd differs from pull request most recent head 4ab0d58. Consider uploading reports for the commit 4ab0d58 to get more accurate results

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #178      +/-   ##
==========================================
- Coverage   50.90%   50.82%   -0.08%     
==========================================
  Files          64       64              
  Lines        3931     3941      +10     
  Branches      905      909       +4     
==========================================
+ Hits         2001     2003       +2     
- Misses       1930     1938       +8     
Impacted Files Coverage Δ
src/runtime/background/gm_api.ts 62.23% <38.46%> (-0.92%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@DreamNya
Copy link
Contributor Author

另外关于这个的使用场景是什么呢?

建议再验证一下firefox的兼容性

经测试无法兼容Firefox,Firefox似乎禁止在background页面使用window.open()会直接返回null

这个功能主要是提供了一个让(自定义协议或其他)url可以绕过http页面限制的手段
使用场景主要是方便让自定义协议直接打开而不需要强制确认,以便前端可以和本地后端进行无缝通信,增强使用体验
而且不排除有除了自定义协议外的其他url也会受到http页面限制,通过这个功能同样能绕过限制
(类似GM_addElement绕过CSP限制、GM_openInTab能打开window.open打不开的浏览器设置页、插件页等)

@CodFrm CodFrm merged commit 9f39dec into scriptscat:main Apr 28, 2023
@CodFrm
Copy link
Member

CodFrm commented Apr 28, 2023

LGTM

@DreamNya DreamNya deleted the GM_openInTab branch April 28, 2023 07:45
@DreamNya DreamNya restored the GM_openInTab branch April 28, 2023 07:45
@DreamNya DreamNya deleted the GM_openInTab branch April 28, 2023 07:45
@cyfung1031
Copy link
Collaborator

cyfung1031 commented Oct 19, 2025

@CodFrm 是不是API改变了? 现在用 http 执行一般的 GM_openInTab 也是弹「ScriptCat要打开」而不是「http网站要打开」
是因为这个GM_openInTab搬到了Service_worker吗?

我是用 http://httpforever.com/ 执行以下脚本

// ==UserScript==
// @name         New Userscript A7B1-1
// @namespace    https://docs.scriptcat.org/
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @match        https://*/*
// @match        http://*/*
// @grant        GM_openInTab
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
    'use strict';

    // Your code here...

    GM_registerMenuCommand("ab", () => {
        setTimeout(()=>{

        GM_openInTab("magnet:?xt=urn:btih:xxxxxxxxxxxxxxxxxxxxxxxx");
        }, 400);

    })


})();


當然原生 chrome.tabs.create 還是有限制
Brave - OK GM_openInTab("chrome-extension://apmmpaebfobifelkijhaljbmpcgbjbdo/manage.html#stylus-options");
Firefox - NG GM_openInTab("moz-extension://da7197d4-4117-4ec3-ad91-8bcacd815466/manage.html#stylus-options");

@CodFrm
Copy link
Member

CodFrm commented Oct 20, 2025

@CodFrm 是不是API改变了? 现在用 http 执行一般的 GM_openInTab 也是弹「ScriptCat要打开」而不是「http网站要打开」 是因为这个GM_openInTab搬到了Service_worker吗?

我是用 http://httpforever.com/ 执行以下脚本

// ==UserScript==
// @name         New Userscript A7B1-1
// @namespace    https://docs.scriptcat.org/
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @match        https://*/*
// @match        http://*/*
// @grant        GM_openInTab
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
    'use strict';

    // Your code here...

    GM_registerMenuCommand("ab", () => {
        setTimeout(()=>{

        GM_openInTab("magnet:?xt=urn:btih:xxxxxxxxxxxxxxxxxxxxxxxx");
        }, 400);

    })


})();

當然原生 chrome.tabs.create 還是有限制 Brave - OK GM_openInTab("chrome-extension://apmmpaebfobifelkijhaljbmpcgbjbdo/manage.html#stylus-options"); Firefox - NG GM_openInTab("moz-extension://da7197d4-4117-4ec3-ad91-8bcacd815466/manage.html#stylus-options");

看起来是的,这是mv2时候的pr了,感觉可以直接取消useOpen这个方式了,我测试可以直接通过 chrome.tabs.create({url:"vscode://test"}) 打开(Edge)

Firefox 我测试chrome.tabs.create({url:"vscode://test"})也可以

@cyfung1031
Copy link
Collaborator

cyfung1031 commented Oct 20, 2025

@CodFrm 是不是API改变了? 现在用 http 执行一般的 GM_openInTab 也是弹「ScriptCat要打开」而不是「http网站要打开」 是因为这个GM_openInTab搬到了Service_worker吗?
我是用 http://httpforever.com/ 执行以下脚本

// ==UserScript==
// @name         New Userscript A7B1-1
// @namespace    https://docs.scriptcat.org/
// @version      0.1.0
// @description  try to take over the world!
// @author       You
// @match        https://*/*
// @match        http://*/*
// @grant        GM_openInTab
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
    'use strict';

    // Your code here...

    GM_registerMenuCommand("ab", () => {
        setTimeout(()=>{

        GM_openInTab("magnet:?xt=urn:btih:xxxxxxxxxxxxxxxxxxxxxxxx");
        }, 400);

    })


})();

當然原生 chrome.tabs.create 還是有限制 Brave - OK GM_openInTab("chrome-extension://apmmpaebfobifelkijhaljbmpcgbjbdo/manage.html#stylus-options"); Firefox - NG GM_openInTab("moz-extension://da7197d4-4117-4ec3-ad91-8bcacd815466/manage.html#stylus-options");

看起来是的,这是mv2时候的pr了,感觉可以直接取消useOpen这个方式了,我测试可以直接通过 chrome.tabs.create({url:"vscode://test"}) 打开(Edge)

Firefox 我测试chrome.tabs.create({url:"vscode://test"})也可以

很好。
moz-extension://da7197d4-4117-4ec3-ad91-8bcacd815466/manage.html#stylus-options 在FF是打不开
所以还是会有 #863 redirect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants