@@ -10,7 +10,7 @@ import type { ConfirmParam } from "../permission_verify";
1010import PermissionVerify , { PermissionVerifyApiGet } from "../permission_verify" ;
1111import { cacheInstance } from "@App/app/cache" ;
1212import { type RuntimeService } from "../runtime" ;
13- import { getIcon , isFirefox , openInCurrentTab , cleanFileName } from "@App/pkg/utils/utils" ;
13+ import { getIcon , isFirefox , getCurrentTab , openInCurrentTab , cleanFileName } from "@App/pkg/utils/utils" ;
1414import { type SystemConfig } from "@App/pkg/config/config" ;
1515import i18next , { i18nName } from "@App/locales/locales" ;
1616import FileSystemFactory from "@Packages/filesystem/factory" ;
@@ -43,6 +43,7 @@ import {
4343} from "./gm_xhr" ;
4444import { headerModifierMap , headersReceivedMap } from "./gm_xhr" ;
4545import { BgGMXhr } from "@App/pkg/utils/xhr/bg_gm_xhr" ;
46+ import { nativePageWindowOpen } from "../../offscreen/gm_api" ;
4647
4748let generatedUniqueMarkerIDs = "" ;
4849let generatedUniqueMarkerIDWhen = "" ;
@@ -912,6 +913,28 @@ export default class GMApi {
912913 async GM_openInTab ( request : GMApiRequest < [ string , GMTypes . SWOpenTabOptions ] > , sender : IGetSender ) {
913914 const url = request . params [ 0 ] ;
914915 const options = request . params [ 1 ] ;
916+ if ( options . useOpen ) {
917+ const prevTab = await getCurrentTab ( ) ;
918+ // 发送给offscreen页面处理 (使用window.open)
919+ let ok ;
920+ if ( typeof window === "object" && typeof window ?. open === "function" ) {
921+ // Firefox Background Page
922+ ok = nativePageWindowOpen ( { url } ) ;
923+ } else {
924+ ok = await sendMessage ( this . msgSender , "offscreen/gmApi/windowOpen" , { url } ) ;
925+ }
926+ // 注:一般而言,特殊打开的话没有实际 tab id.
927+ // ------------------------------------------
928+ if ( ok ) {
929+ // 由于window.open强制在前台打开标签,因此获取状态为 { active:true } 的标签即为新标签
930+ const tab = await getCurrentTab ( ) ;
931+ const tabId = tab ?. id ;
932+ if ( tabId && tabId !== prevTab ?. id ) return tabId ;
933+ }
934+ // 当新tab被浏览器阻止时 window.open() 会返回 null 视为已经关闭
935+ // 似乎在Firefox中禁止在background页面使用window.open(),强制返回null
936+ return false ;
937+ }
915938 const getNewTabId = async ( ) => {
916939 const { tabId, windowId } = sender . getExtMessageSender ( ) ;
917940 const active = options . active ;
0 commit comments