File tree 2 files changed +20
-4
lines changed
2 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -469,10 +469,25 @@ export default class GMApi {
469
469
GM_openInTab ( request : Request , channel : Channel ) {
470
470
const url = request . params [ 0 ] ;
471
471
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
+ }
476
491
}
477
492
478
493
@PermissionVerify . API ( {
Original file line number Diff line number Diff line change @@ -327,6 +327,7 @@ declare namespace GMTypes {
327
327
active ?: boolean ;
328
328
insert ?: boolean ;
329
329
setParent ?: boolean ;
330
+ useOpen ?: boolean ;
330
331
}
331
332
332
333
interface XHRResponse {
You can’t perform that action at this time.
0 commit comments