File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ( {
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments