@@ -101,15 +101,6 @@ function replaceNodeTextFn(
101101 const reExcludes = extraArgs . excludes
102102 ? safe . patternToRegex ( extraArgs . excludes , 'ms' )
103103 : null ;
104- const stop = ( takeRecord = true ) => {
105- if ( takeRecord ) {
106- handleMutations ( observer . takeRecords ( ) ) ;
107- }
108- observer . disconnect ( ) ;
109- if ( safe . logLevel > 1 ) {
110- safe . uboLog ( logPrefix , 'Quitting' ) ;
111- }
112- } ;
113104 const textContentFactory = ( ( ) => {
114105 const out = { createScript : s => s } ;
115106 const { trustedTypes : tt } = self ;
@@ -122,19 +113,19 @@ function replaceNodeTextFn(
122113 }
123114 return out ;
124115 } ) ( ) ;
125- let sedCount = extraArgs . sedCount || 0 ;
116+ let sedCount = extraArgs . sedCount ?? Number . MAX_SAFE_INTEGER ;
126117 const handleNode = node => {
127118 const before = node . textContent ;
128119 if ( reIncludes ) {
129120 reIncludes . lastIndex = 0 ;
130- if ( safe . RegExp_test ( reIncludes , before ) === false ) { return true ; }
121+ if ( safe . RegExp_test ( reIncludes , before ) === false ) { return ; }
131122 }
132123 if ( reExcludes ) {
133124 reExcludes . lastIndex = 0 ;
134- if ( safe . RegExp_test ( reExcludes , before ) ) { return true ; }
125+ if ( safe . RegExp_test ( reExcludes , before ) ) { return ; }
135126 }
136127 rePattern . lastIndex = 0 ;
137- if ( safe . RegExp_test ( rePattern , before ) === false ) { return true ; }
128+ if ( safe . RegExp_test ( rePattern , before ) === false ) { return ; }
138129 rePattern . lastIndex = 0 ;
139130 const after = pattern !== ''
140131 ? before . replace ( rePattern , replacement )
@@ -146,44 +137,65 @@ function replaceNodeTextFn(
146137 safe . uboLog ( logPrefix , `Text before:\n${ before . trim ( ) } ` ) ;
147138 }
148139 safe . uboLog ( logPrefix , `Text after:\n${ after . trim ( ) } ` ) ;
149- return sedCount === 0 || ( sedCount -= 1 ) !== 0 ;
140+ sedCount -= 1 ;
150141 } ;
151- const handleMutations = mutations => {
152- for ( const mutation of mutations ) {
153- for ( const node of mutation . addedNodes ) {
154- if ( reNodeName . test ( node . nodeName ) === false ) { continue ; }
155- if ( handleNode ( node ) ) { continue ; }
156- stop ( false ) ; return ;
157- }
158- }
159- } ;
160- const observer = new MutationObserver ( handleMutations ) ;
161- observer . observe ( document , { childList : true , subtree : true } ) ;
162- if ( document . documentElement ) {
163- const treeWalker = document . createTreeWalker (
164- document . documentElement ,
142+ const handleTree = root => {
143+ const treeWalker = document . createTreeWalker ( root ,
165144 NodeFilter . SHOW_ELEMENT | NodeFilter . SHOW_TEXT
166145 ) ;
146+ const { currentScript } = document ;
167147 let count = 0 ;
168148 for ( ; ; ) {
169149 const node = treeWalker . nextNode ( ) ;
170- count += 1 ;
171150 if ( node === null ) { break ; }
172- if ( reNodeName . test ( node . nodeName ) === false ) { continue ; }
173- if ( node === document . currentScript ) { continue ; }
174- if ( handleNode ( node ) ) { continue ; }
175- stop ( ) ; break ;
151+ count += 1 ;
152+ if ( node === currentScript ) { continue ; }
153+ if ( reNodeName . test ( node . nodeName ) ) {
154+ handleNode ( node ) ;
155+ } else if ( node . nodeName === 'TEMPLATE' ) {
156+ count += handleTree ( node . content ) ;
157+ } else {
158+ continue ;
159+ }
160+ if ( sedCount === 0 ) { break ; }
176161 }
162+ return count ;
163+ } ;
164+ if ( document . documentElement ) {
165+ const count = handleTree ( document . documentElement ) ;
177166 safe . uboLog ( logPrefix , `${ count } nodes present before installing mutation observer` ) ;
178167 }
179- if ( extraArgs . stay ) { return ; }
180- runAt ( ( ) => {
181- const quitAfter = extraArgs . quitAfter || 0 ;
182- if ( quitAfter !== 0 ) {
183- setTimeout ( ( ) => { stop ( ) ; } , quitAfter ) ;
184- } else {
185- stop ( ) ;
168+ const stay = Boolean ( extraArgs . stay ) ;
169+ if ( sedCount === 0 && stay === false ) { return ; }
170+ const stop = ( takeRecord = true ) => {
171+ const mutations = takeRecord ? observer . takeRecords ( ) : [ ] ;
172+ observer . disconnect ( ) ;
173+ handleMutations ( mutations ) ;
174+ if ( safe . logLevel > 1 ) {
175+ safe . uboLog ( logPrefix , 'Quitting' ) ;
176+ }
177+ } ;
178+ const handleMutations = mutations => {
179+ for ( const mutation of mutations ) {
180+ for ( const node of mutation . addedNodes ) {
181+ if ( reNodeName . test ( node . nodeName ) ) {
182+ handleNode ( node ) ;
183+ } else if ( node . nodeName === 'TEMPLATE' ) {
184+ handleTree ( node . content ) ;
185+ } else {
186+ continue ;
187+ }
188+ if ( sedCount === 0 ) { return stop ( false ) ; }
189+ }
186190 }
191+ } ;
192+ const observer = new MutationObserver ( handleMutations ) ;
193+ observer . observe ( document , { childList : true , subtree : true } ) ;
194+ if ( stay ) { return ; }
195+ runAt ( ( ) => {
196+ const quitAfter = extraArgs . quitAfter ?? 0 ;
197+ if ( quitAfter === 0 ) { return stop ( ) ; }
198+ setTimeout ( ( ) => { stop ( ) ; } , quitAfter ) ;
187199 } , 'interactive' ) ;
188200}
189201
0 commit comments