@@ -116,14 +116,23 @@ export function useFogOFWarDiscovery(
116
116
if ( ! path ) {
117
117
return ;
118
118
}
119
- let url = new URL ( path , window . location . origin ) ;
120
- if ( ! discoveredPaths . has ( url . pathname ) ) {
121
- nextPaths . add ( url . pathname ) ;
119
+ // optimization: use the already-parsed pathname from links
120
+ let pathname =
121
+ el . tagName === "A"
122
+ ? ( el as HTMLAnchorElement ) . pathname
123
+ : new URL ( path , window . location . origin ) . pathname ;
124
+ if ( ! discoveredPaths . has ( pathname ) ) {
125
+ nextPaths . add ( pathname ) ;
122
126
}
123
127
}
124
128
125
129
// Register and fetch patches for all initially-rendered links/forms
126
130
async function fetchPatches ( ) {
131
+ // re-check/update registered links
132
+ document
133
+ . querySelectorAll ( "a[data-discover], form[data-discover]" )
134
+ . forEach ( registerElement ) ;
135
+
127
136
let lazyPaths = Array . from ( nextPaths . keys ( ) ) . filter ( ( path ) => {
128
137
if ( discoveredPaths . has ( path ) ) {
129
138
nextPaths . delete ( path ) ;
@@ -150,43 +159,14 @@ export function useFogOFWarDiscovery(
150
159
}
151
160
}
152
161
153
- // Register and fetch patches for all initially-rendered links
154
- document . body
155
- . querySelectorAll ( "a[data-discover], form[data-discover]" )
156
- . forEach ( ( el ) => registerElement ( el ) ) ;
162
+ let debouncedFetchPatches = debounce ( fetchPatches , 100 ) ;
157
163
164
+ // scan and fetch initial links
158
165
fetchPatches ( ) ;
159
166
160
167
// Setup a MutationObserver to fetch all subsequently rendered links/form
161
- let debouncedFetchPatches = debounce ( fetchPatches , 100 ) ;
162
-
163
- function isElement ( node : Node ) : node is Element {
164
- return node . nodeType === Node . ELEMENT_NODE ;
165
- }
166
-
167
- let observer = new MutationObserver ( ( records ) => {
168
- let elements = new Set < Element > ( ) ;
169
- records . forEach ( ( r ) => {
170
- [ r . target , ...r . addedNodes ] . forEach ( ( node ) => {
171
- if ( ! isElement ( node ) ) return ;
172
- if ( node . tagName === "A" && node . getAttribute ( "data-discover" ) ) {
173
- elements . add ( node ) ;
174
- } else if (
175
- node . tagName === "FORM" &&
176
- node . getAttribute ( "data-discover" )
177
- ) {
178
- elements . add ( node ) ;
179
- }
180
- if ( node . tagName !== "A" ) {
181
- node
182
- . querySelectorAll ( "a[data-discover], form[data-discover]" )
183
- . forEach ( ( el ) => elements . add ( el ) ) ;
184
- }
185
- } ) ;
186
- } ) ;
187
- elements . forEach ( ( el ) => registerElement ( el ) ) ;
188
- debouncedFetchPatches ( ) ;
189
- } ) ;
168
+ // It just schedules a full scan since that's faster than checking subtrees
169
+ let observer = new MutationObserver ( ( ) => debouncedFetchPatches ( ) ) ;
190
170
191
171
observer . observe ( document . documentElement , {
192
172
subtree : true ,
0 commit comments