@@ -24,12 +24,12 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
2424 const docRewriteObserver = RewriteObserver ( updateRoot ) ;
2525 const docRootObserver = RootObserver ( restoreOrder ) ;
2626 const toSafeChar = c => String . fromCharCode ( 0xFF00 + c . charCodeAt ( 0 ) - 0x20 ) ;
27- const getAss = ( ) => Object . isExtensible ( ass ) ? ass : ass . slice ( ) ; // eslint-disable-line no-use-before-define
2827 /** @type {InjectedStyle[] } */
2928 const list = [ ] ;
3029 /** @type {Map<number,InjectedStyle> } */
3130 const table = new Map ( ) ;
32- let /** @type {CSSStyleSheet[] } */ ass ;
31+ let /** @type {CSSStyleSheet[] } */ ass ; // frozen array in old Chrome, the ref changes
32+ let /** @type {CSSStyleSheet[] } */ assV2 ; // mutable array, the ref doesn't change
3333 let root = document . documentElement ;
3434 let isEnabled = true ;
3535 let isTransitionPatched = chrome . app && CSS . supports ( 'accent-color' , 'red' ) ; // Chrome 93
@@ -87,13 +87,13 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
8787
8888 function addElement ( el , before ) {
8989 if ( ass ) {
90- const sheets = getAss ( ) ;
90+ const sheets = assV2 || wrappedDoc [ kAss ] ;
9191 let i = sheets . indexOf ( el ) ;
9292 if ( i >= 0 ) el = sheets . splice ( i , 1 ) [ 0 ] ;
9393 i = before ? sheets . indexOf ( before ) : - 1 ;
9494 if ( i >= 0 ) sheets . splice ( i , 0 , el ) ;
9595 else sheets . push ( el ) ;
96- if ( sheets !== ass ) wrappedDoc [ kAss ] = sheets ;
96+ if ( ! assV2 ) wrappedDoc [ kAss ] = sheets ;
9797 } else {
9898 updateRoot ( ) . insertBefore ( el , before ) ;
9999 }
@@ -112,11 +112,11 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
112112 if ( el . remove ) {
113113 el . remove ( ) ;
114114 } else if ( ass ) {
115- const sheets = getAss ( ) ;
115+ const sheets = assV2 || wrappedDoc [ kAss ] ;
116116 const i = sheets . indexOf ( el ) ;
117117 if ( i >= 0 ) {
118118 sheets . splice ( i , 1 ) ;
119- if ( sheets !== ass ) wrappedDoc [ kAss ] = sheets ;
119+ if ( ! assV2 ) wrappedDoc [ kAss ] = sheets ;
120120 }
121121 }
122122 }
@@ -129,9 +129,9 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
129129
130130 function replaceAss ( readd ) {
131131 const elems = list . map ( s => s . el ) ;
132- const res = ass . filter ( arrItemNotIn , new Set ( elems ) ) ;
132+ const res = ( assV2 || wrappedDoc [ kAss ] ) . filter ( arrItemNotIn , new Set ( elems ) ) ;
133133 if ( readd ) res . push ( ...elems ) ;
134- ass = wrappedDoc [ kAss ] = res ;
134+ wrappedDoc [ kAss ] = res ;
135135 }
136136
137137 function applyStyles ( isReplace , { cfg, sections} ) {
@@ -203,7 +203,7 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
203203 id = MEDIA + id ;
204204 el = new CSSStyleSheet ( { media : id } ) ;
205205 setTextAndName ( el , style ) ;
206- for ( const { media : m } of ass ) if ( m . mediaText === id ) m . mediaText += '-old' ;
206+ for ( const { media : m } of assV2 || wrappedDoc [ kAss ] ) if ( m . mediaText === id ) m . mediaText += '-old' ;
207207 return el ;
208208 }
209209 if ( ! creationDoc && ( el = initCreationDoc ( style ) ) ) {
@@ -296,9 +296,9 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
296296 if ( ok ) return ;
297297 } catch ( err ) { }
298298 }
299- if ( retry && ffCsp ) { // ffCsp bug got fixed
299+ if ( retry && ffCsp && ( ass = wrappedDoc [ kAss ] ) ) { // ffCsp bug got fixed
300300 console . debug ( 'Stylus switched to document.adoptedStyleSheets due to a strict CSP of the page' ) ;
301- ass = wrappedDoc [ kAss ] ;
301+ assV2 = Object . isExtensible ( ass ) && ass ;
302302 return createStyle ( style ) ;
303303 }
304304 creationDoc = document ;
@@ -319,6 +319,7 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
319319 if ( ! el ) {
320320 bad = false ;
321321 } else if ( ass ) {
322+ if ( ! assV2 ) ass = wrappedDoc [ kAss ] ;
322323 for ( let i = ass . length - list . length ; i < ass . length ; i ++ ) {
323324 if ( i < 0 || ass [ i ] !== list [ i ] . el ) {
324325 bad = true ;
@@ -363,6 +364,7 @@ window.StyleInjector = window.INJECTED === 1 ? window.StyleInjector : ({
363364 if ( ! ass !== ! cfg . ass ) {
364365 removeAllElements ( ) ;
365366 ass = ass ? null : wrappedDoc [ kAss ] ;
367+ assV2 = ass && Object . isExtensible ( ass ) && ass ;
366368 for ( const s of list ) s . el = createStyle ( s ) ;
367369 addAllElements ( ) ;
368370 }
0 commit comments