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
23 changes: 19 additions & 4 deletions src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* Complexity:
* - Org detection is O(1) DOM querying.
* - URL change detection uses a MutationObserver; callback work is O(1) per DOM mutation batch.
* - URL change detection uses history.pushState/replaceState monkey-patching and popstate; fires only on actual navigations.
*/

import { MessageBus } from '../services/messaging';
Expand Down Expand Up @@ -159,15 +159,30 @@ messageBus.on('ORG_INFO', async (message) => {
reportOrgDetection();

// Re-detect on URL changes (SPA navigation in Lightning)
// Uses history API monkey-patching + popstate instead of a document-wide
// MutationObserver, which fired on every DOM mutation in Salesforce pages.
let lastUrl = window.location.href;
const urlObserver = new MutationObserver(() => {

function onUrlChange(): void {
if (window.location.href !== lastUrl) {
lastUrl = window.location.href;
reportOrgDetection();
}
});
}

window.addEventListener('popstate', onUrlChange);

const originalPushState = history.pushState.bind(history);
history.pushState = function (...args: Parameters<typeof history.pushState>): void {
originalPushState(...args);
onUrlChange();
Comment on lines +176 to +178

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Observe navigation from the page's execution world

Because public/manifest.json does not specify "world": "MAIN", this content script runs in Chrome's isolated world. Reassigning history.pushState and replaceState here therefore does not wrap the functions invoked by Salesforce's page scripts; moreover, those calls do not emit popstate. During ordinary Lightning SPA navigation, onUrlChange() consequently never runs and reportOrgDetection() is not repeated, unlike with the removed DOM observer. Inject a page-world hook that relays navigation events, or retain another observer visible to the content script.

Useful? React with 👍 / 👎.

};

urlObserver.observe(document.body, { childList: true, subtree: true });
const originalReplaceState = history.replaceState.bind(history);
history.replaceState = function (...args: Parameters<typeof history.replaceState>): void {
originalReplaceState(...args);
onUrlChange();
};

// Content script initialized

Expand Down
Loading