Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion injected/src/features/page-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,23 @@ export default class PageContext extends ContentFeature {
mutationObserver = null;
lastSentContent = null;
listenForUrlChanges = true;
/** @type {ReturnType<typeof setTimeout> | null} */
#delayedRecheckTimer = null;
recheckCount = 0;
recheckLimit = 0;

init() {
this.recheckLimit = this.getFeatureSetting('recheckLimit') || 5;
if (!this.shouldActivate()) {
return;
}
this.setupListeners();
}

resetRecheckCount() {
this.recheckCount = 0;
}

setupListeners() {
this.observeContentChanges();
if (this.getFeatureSettingEnabled('subscribeToCollect', 'enabled')) {
Expand Down Expand Up @@ -171,6 +180,16 @@ export default class PageContext extends ContentFeature {
this.stopObserving();
}

/**
* Clear all pending timers
*/
clearTimers() {
if (this.#delayedRecheckTimer) {
clearTimeout(this.#delayedRecheckTimer);
this.#delayedRecheckTimer = null;
}
}

set cachedContent(content) {
if (content === undefined) {
this.invalidateCache();
Expand All @@ -194,10 +213,34 @@ export default class PageContext extends ContentFeature {
this.log.info('MutationObserver', _mutations);
// Invalidate cache when content changes
this.cachedContent = undefined;

this.scheduleDelayedRecheck();
});
}
}

/**
* Schedule a delayed recheck after navigation events
*/
scheduleDelayedRecheck() {
// Clear any existing delayed recheck
this.clearTimers();
if (this.recheckLimit > 0 && this.recheckCount >= this.recheckLimit) {
return;
}

const delayMs = this.getFeatureSetting('navigationRecheckDelayMs') || 1500;

this.log.info('Scheduling delayed recheck', { delayMs });
this.#delayedRecheckTimer = setTimeout(() => {
this.log.info('Performing delayed recheck after navigation');
this.recheckCount++;
this.invalidateCache();

this.handleContentCollectionRequest(false);
}, delayMs);
}

startObserving() {
this.log.info('Starting observing', this.mutationObserver, this.#cachedContent);
if (this.mutationObserver && this.#cachedContent && !this.isObserving) {
Expand All @@ -217,8 +260,11 @@ export default class PageContext extends ContentFeature {
}
}

handleContentCollectionRequest() {
handleContentCollectionRequest(resetRecheckCount = true) {
this.log.info('Handling content collection request');
if (resetRecheckCount) {
this.resetRecheckCount();
}
try {
const content = this.collectPageContent();
this.sendContentResponse(content);
Expand Down Expand Up @@ -300,6 +346,7 @@ export default class PageContext extends ContentFeature {

this.log.info('Calling domToMarkdown', clone.innerHTML);
content += domToMarkdown(clone, upperLimit);
this.log.info('Content markdown', content, clone, contentRoot);
}
content = content.trim();

Expand Down
Loading