Skip to content

Commit c788a98

Browse files
Delayed content collect (#1970)
* Delayed content collect * Check for hidden * Fix up clearing timers for delayed content collection * Clean up comments * Remove meaningful content check * Remove hidden to split out into another pr * Remove redundant clear timer * Move to check after invalidated cache with a number of retries * Clean up * Increment on recheck and clear timers always * Remove now outdated comments * Move where rescheudle is for hygine sake * Revert "Move where rescheudle is for hygine sake" This reverts commit b8ab753. * Simplify timer removal
1 parent fb990da commit c788a98

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

injected/src/features/page-context.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,23 @@ export default class PageContext extends ContentFeature {
7474
mutationObserver = null;
7575
lastSentContent = null;
7676
listenForUrlChanges = true;
77+
/** @type {ReturnType<typeof setTimeout> | null} */
78+
#delayedRecheckTimer = null;
79+
recheckCount = 0;
80+
recheckLimit = 0;
7781

7882
init() {
83+
this.recheckLimit = this.getFeatureSetting('recheckLimit') || 5;
7984
if (!this.shouldActivate()) {
8085
return;
8186
}
8287
this.setupListeners();
8388
}
8489

90+
resetRecheckCount() {
91+
this.recheckCount = 0;
92+
}
93+
8594
setupListeners() {
8695
this.observeContentChanges();
8796
if (this.getFeatureSettingEnabled('subscribeToCollect', 'enabled')) {
@@ -171,6 +180,16 @@ export default class PageContext extends ContentFeature {
171180
this.stopObserving();
172181
}
173182

183+
/**
184+
* Clear all pending timers
185+
*/
186+
clearTimers() {
187+
if (this.#delayedRecheckTimer) {
188+
clearTimeout(this.#delayedRecheckTimer);
189+
this.#delayedRecheckTimer = null;
190+
}
191+
}
192+
174193
set cachedContent(content) {
175194
if (content === undefined) {
176195
this.invalidateCache();
@@ -194,10 +213,34 @@ export default class PageContext extends ContentFeature {
194213
this.log.info('MutationObserver', _mutations);
195214
// Invalidate cache when content changes
196215
this.cachedContent = undefined;
216+
217+
this.scheduleDelayedRecheck();
197218
});
198219
}
199220
}
200221

222+
/**
223+
* Schedule a delayed recheck after navigation events
224+
*/
225+
scheduleDelayedRecheck() {
226+
// Clear any existing delayed recheck
227+
this.clearTimers();
228+
if (this.recheckLimit > 0 && this.recheckCount >= this.recheckLimit) {
229+
return;
230+
}
231+
232+
const delayMs = this.getFeatureSetting('navigationRecheckDelayMs') || 1500;
233+
234+
this.log.info('Scheduling delayed recheck', { delayMs });
235+
this.#delayedRecheckTimer = setTimeout(() => {
236+
this.log.info('Performing delayed recheck after navigation');
237+
this.recheckCount++;
238+
this.invalidateCache();
239+
240+
this.handleContentCollectionRequest(false);
241+
}, delayMs);
242+
}
243+
201244
startObserving() {
202245
this.log.info('Starting observing', this.mutationObserver, this.#cachedContent);
203246
if (this.mutationObserver && this.#cachedContent && !this.isObserving) {
@@ -217,8 +260,11 @@ export default class PageContext extends ContentFeature {
217260
}
218261
}
219262

220-
handleContentCollectionRequest() {
263+
handleContentCollectionRequest(resetRecheckCount = true) {
221264
this.log.info('Handling content collection request');
265+
if (resetRecheckCount) {
266+
this.resetRecheckCount();
267+
}
222268
try {
223269
const content = this.collectPageContent();
224270
this.sendContentResponse(content);
@@ -300,6 +346,7 @@ export default class PageContext extends ContentFeature {
300346

301347
this.log.info('Calling domToMarkdown', clone.innerHTML);
302348
content += domToMarkdown(clone, upperLimit);
349+
this.log.info('Content markdown', content, clone, contentRoot);
303350
}
304351
content = content.trim();
305352

0 commit comments

Comments
 (0)