@@ -3,7 +3,7 @@ import {k_deepCopy, kApplyPort} from '@/js/consts';
33import { onMessage } from '@/js/msg' ;
44import { API , isFrame , TDM , updateTDM } from '@/js/msg-api' ;
55import * as styleInjector from './style-injector' ;
6- import { FF , isXml , own , ownId } from './style-injector' ;
6+ import { FF , isXml , own , ownId , runtime } from './style-injector' ;
77
88const SYM_ID = 'styles' ;
99const kPageShow = 'pageshow' ;
@@ -27,7 +27,7 @@ const instanceId = (FF || !__.MV3 && !CSS.supports('top', '1ic')) && Math.random
2727 * so we'll add the styles only if the iframe becomes visible */
2828const xoEventId = `${ instanceId || Math . random ( ) } ` ;
2929
30- const NAV_ID = 'url:' + chrome . runtime . id ;
30+ const NAV_ID = 'url:' + runtime . id ;
3131/** Firefox disallows direct access to global variables in the parent's "isolated world".
3232 * Chrome 63 and older can't construct EventTarget, so we detect them via ResizeObserver,
3333 * using a typeof check to skip an implicit global for <html id="ResizeObserver"> */
@@ -70,16 +70,8 @@ if (TDM < 0) {
7070}
7171styleInjector . onInjectorUpdate = ( ) => {
7272 updateCount ( ) ;
73- if ( isFrame ) {
74- updateExposeIframes ( ) ;
75- if ( ! styleInjector . list . length ) {
76- port ?. disconnect ( ) ;
77- port = null ;
78- } else if ( ! port ) {
79- port = chrome . runtime . connect ( { name : kApplyPort } ) ;
80- port . onDisconnect . addListener ( ( ) => ( port = null ) ) ;
81- }
82- }
73+ if ( isFrame ) updateExposeIframes ( ) ;
74+ if ( isFrame || own . cfg . wake ) updatePort ( ) ;
8375} ;
8476styleInjector . selfDestruct = selfDestruct ;
8577// Declare all vars before init() or it'll throw due to "temporal dead zone" of const/let
@@ -98,7 +90,8 @@ async function init() {
9890 else data = ! __ . ENTRY && ! isFrameSameOrigin && ! isXml && getStylesViaXhr ( ) ;
9991 // XML in Chrome will be auto-converted to html later, so we can't style it via XHR now
10092 }
101- if ( ! chrome . runtime . id ) return selfDestruct ( ) ;
93+ if ( ! runtime . id )
94+ return selfDestruct ( ) ;
10295 await applyStyles ( data , true ) ;
10396}
10497
@@ -118,7 +111,7 @@ function getStylesViaXhr() {
118111 try {
119112 const blobId = ( document . cookie . split ( ownId + '=' ) [ 1 ] || '' ) . split ( ';' ) [ 0 ] ;
120113 if ( ! blobId ) return ; // avoiding an exception so we don't spoil debugging in devtools
121- const url = 'blob:' + chrome . runtime . getURL ( blobId ) ;
114+ const url = 'blob:' + runtime . getURL ( blobId ) ;
122115 document . cookie = `${ ownId } =1; max-age=0; SameSite=Lax` ; // remove our cookie
123116 const xhr = new XMLHttpRequest ( ) ;
124117 xhr . open ( 'GET' , url , false ) ; // synchronous
@@ -189,14 +182,15 @@ function processThrottled() {
189182}
190183
191184function updateConfig ( { cfg} ) {
192- for ( const k in cfg ) {
185+ for ( const /** @type { keyof Injection.Config }*/ k in cfg ) {
193186 const v = cfg [ k ] ;
194187 if ( v === own . cfg [ k ] ) continue ;
195188 if ( k === 'top' && ! isFrame ) continue ;
196189 own . cfg [ k ] = v ;
197190 if ( k === 'off' ) updateDisableAll ( ) ;
198191 else if ( k === 'order' ) styleInjector . sort ( ) ;
199192 else if ( k === 'top' ) updateExposeIframes ( ) ;
193+ else if ( k === 'wake' && __ . MV3 ) updatePort ( ) ;
200194 else styleInjector . updateConfig ( own . cfg ) ;
201195 }
202196}
@@ -234,6 +228,16 @@ function updateCount() {
234228 }
235229}
236230
231+ function updatePort ( ) {
232+ if ( ! ( __ . MV3 && own . cfg . wake ) && ! styleInjector . list . length ) {
233+ port ?. disconnect ( ) ;
234+ port = null ;
235+ } else if ( ! port && ( isFrame || __ . MV3 && own . cfg . wake ) ) {
236+ port = runtime . connect ( { name : kApplyPort } ) ;
237+ port . onDisconnect . addListener ( onPortDisconnected ) ;
238+ }
239+ }
240+
237241function updateUrl ( url ) {
238242 if ( url !== matchUrl ) {
239243 matchUrl = url ;
@@ -249,7 +253,8 @@ function onFrameElementInView(cb) {
249253
250254/** @param {IntersectionObserverEntry[] } entries */
251255function onIntersect ( entries ) {
252- if ( ! chrome . runtime . id ) return selfDestruct ( ) ;
256+ if ( ! runtime . id )
257+ return selfDestruct ( ) ;
253258 for ( const e of entries ) {
254259 if ( e . intersectionRatio ) {
255260 xo . unobserve ( e . target ) ;
@@ -259,15 +264,30 @@ function onIntersect(entries) {
259264}
260265
261266function onBFCache ( e ) {
262- if ( ! chrome . runtime . id ) return selfDestruct ( ) ;
267+ if ( ! runtime . id )
268+ return selfDestruct ( ) ;
263269 if ( e . isTrusted && e . persisted ) {
264270 throttledCount = '' ;
265271 init ( ) ; // styles may have been toggled while we were in bfcache
266272 }
267273}
268274
275+ function onPortDisconnected ( ) {
276+ if ( __ . MV3 && own . cfg . wake )
277+ addEventListener ( 'mousedown' , wakeUpSW , true ) ;
278+ port = null ;
279+ }
280+
281+ function wakeUpSW ( e ) {
282+ if ( ! runtime . id )
283+ return selfDestruct ( ) ;
284+ if ( ! port && e . target . closest ( 'a' ) ?. href )
285+ updatePort ( ) ;
286+ }
287+
269288function onReified ( e ) {
270- if ( ! chrome . runtime . id ) return selfDestruct ( ) ;
289+ if ( ! runtime . id )
290+ return selfDestruct ( ) ;
271291 if ( e . isTrusted ) {
272292 updateTDM ( 2 ) ;
273293 document . onprerenderingchange = null ;
@@ -287,6 +307,7 @@ function selfDestruct() {
287307 if ( mqDark ) mqDark . onchange = null ;
288308 if ( offscreen ) for ( const fn of offscreen ) fn ( ) ;
289309 if ( TDM < 0 ) document . onprerenderingchange = null ;
310+ if ( __ . MV3 ) removeEventListener ( 'mousedown' , wakeUpSW , true ) ;
290311 navHubParent ?. removeEventListener ( NAV_ID , onUrlChanged , true ) ;
291312 offscreen = null ;
292313 styleInjector . shutdown ( ) ;
0 commit comments