Skip to content

Commit d4e9627

Browse files
author
Maxim Fomin
committed
advanced token recalcucaton on page navigation
1 parent 14216dd commit d4e9627

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

init.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ if (!window.__OCP_messageListenerRegistered_v2) {
4141

4242
// Lightweight UI refresh that preserves the floating panel state
4343
// Accepts an optional `origin` parameter to limit refreshing to the initiator (e.g. 'panel' or 'inline').
44-
window.__OCP_partialRefreshUI = function(optionalNewConfig, origin = null) {
44+
window.__OCP_partialRefreshUI = function (optionalNewConfig, origin = null) {
4545
try {
4646
if (optionalNewConfig) {
4747
window.globalMaxExtensionConfig = optionalNewConfig;
@@ -60,12 +60,12 @@ if (!window.__OCP_messageListenerRegistered_v2) {
6060

6161
// Expose a single entry to perform partial or full refresh depending on panel presence
6262
// Accepts optional `origin` so partial refresh can be targeted when appropriate.
63-
window.__OCP_nukeAndRefresh = function(optionalNewConfig, origin = null) {
63+
window.__OCP_nukeAndRefresh = function (optionalNewConfig, origin = null) {
6464
if (window.__OCP_nukeInProgress) return;
6565
window.__OCP_nukeInProgress = true;
6666
try {
6767
const hasPanel = !!(window.MaxExtensionFloatingPanel && window.MaxExtensionFloatingPanel.panelElement);
68-
68+
6969
if (hasPanel) {
7070
// Preserve panel DOM/state; only refresh buttons/inline
7171
window.__OCP_partialRefreshUI(optionalNewConfig, origin);
@@ -74,7 +74,7 @@ if (!window.__OCP_messageListenerRegistered_v2) {
7474
if (optionalNewConfig) {
7575
window.globalMaxExtensionConfig = optionalNewConfig;
7676
}
77-
77+
7878
// 1) Stop resiliency monitors and timers from previous run
7979
try {
8080
if (window.OneClickPropmts_currentResiliencyTimeout) {
@@ -85,21 +85,21 @@ if (!window.__OCP_messageListenerRegistered_v2) {
8585
window.OneClickPropmts_extendedMonitoringObserver.disconnect();
8686
window.OneClickPropmts_extendedMonitoringObserver = null;
8787
}
88-
} catch (e) {}
89-
88+
} catch (e) { }
89+
9090
// 2) Remove inline buttons container(s)
9191
try {
9292
const containerId = window?.InjectionTargetsOnWebsite?.selectors?.buttonsContainerId;
9393
if (containerId) {
9494
document.querySelectorAll('#' + CSS.escape(containerId)).forEach(node => node.remove());
9595
}
96-
} catch (e) {}
97-
96+
} catch (e) { }
97+
9898
// 3) Do NOT remove the panel here (it is already absent in this branch)
99-
99+
100100
// 4) Detach keyboard listener to avoid duplicates
101-
try { window.removeEventListener('keydown', manageKeyboardShortcutEvents); } catch (e) {}
102-
101+
try { window.removeEventListener('keydown', manageKeyboardShortcutEvents); } catch (e) { }
102+
103103
// 5) Re-run full initialization
104104
publicStaticVoidMain();
105105
}
@@ -238,7 +238,7 @@ async function commenceExtensionInitialization(configurationObject) {
238238
let modsExist = (() => {
239239
const containerId = window?.InjectionTargetsOnWebsite?.selectors?.buttonsContainerId;
240240
if (!containerId) return false;
241-
const el = document.getElementById(containerId);
241+
const el = document.getElementById(containerId);
242242
return !!(el && el.children && el.children.length > 0);
243243
})();
244244
// Only trigger fallback if no mods exist and the panel isn’t already visible.
@@ -367,13 +367,14 @@ function resilientStartAndRetryOnSPANavigation(callback) {
367367
window.__OCP_urlChangeObserver.disconnect();
368368
window.__OCP_urlChangeObserver = null;
369369
}
370-
} catch (e) {}
370+
} catch (e) { }
371371

372372
let previousUrl = location.href;
373373
const urlChangeObserver = new MutationObserver(() => {
374374
const currentUrl = location.href;
375375
if (currentUrl !== previousUrl) {
376376
previousUrl = currentUrl;
377+
document.dispatchEvent(new CustomEvent('ocp-page-navigated'));
377378
callback();
378379
}
379380
});
@@ -393,6 +394,7 @@ function patchHistoryMethods() {
393394
history[method] = function (...args) {
394395
const result = original.apply(this, args);
395396
logConCgp(`[init] ${method} called. URL:`, args[2]);
397+
document.dispatchEvent(new CustomEvent('ocp-page-navigated'));
396398
publicStaticVoidMain(); // Re-run the full initialization logic
397399
return result;
398400
};

modules/backend-tokenApproximator.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
if (window.__OCP_tokApprox_backend_initDone) return;
66
window.__OCP_tokApprox_backend_initDone = true;
77

8+
// A standard debounce utility function.
9+
function debounce(func, delay) {
10+
let timeout;
11+
return function(...args) {
12+
clearTimeout(timeout);
13+
timeout = setTimeout(() => func.apply(this, args), delay);
14+
};
15+
}
16+
817
// ---- Guards & site detection ----
918
const Site = (window.InjectionTargetsOnWebsite && window.InjectionTargetsOnWebsite.activeSite) || 'Unknown';
1019

@@ -1314,5 +1323,15 @@
13141323
}
13151324
});
13161325
} catch { /* noop */ }
1326+
1327+
// Create a debounced handler for page navigation events. This prevents
1328+
// excessive recalculations when a user navigates rapidly in a SPA.
1329+
const handlePageNavigation = debounce(() => {
1330+
log('Debounced navigation event triggered, forcing thread token update.');
1331+
threadScheduler.forceNow();
1332+
}, 2000);
1333+
1334+
// Listen for the custom event dispatched by init.js on SPA navigation.
1335+
document.addEventListener('ocp-page-navigated', handlePageNavigation);
13171336
})();
13181337
})();

0 commit comments

Comments
 (0)