side-notes/background.js

39 lines
1.3 KiB
JavaScript
Raw Normal View History

// Side Notes — Background Service Worker
//
// Responsibilities:
// - Set up side panel behavior on install.
// - Detect in-page navigation (SPA pushState/replaceState, hash changes)
// and notify any open side panel for the affected tab to reload its note.
import { buildNoteKey } from './shared/key-builder.js';
// ---------------------------------------------------------------------------
// Install
// ---------------------------------------------------------------------------
chrome.runtime.onInstalled.addListener(() => {
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
});
// ---------------------------------------------------------------------------
// In-page navigation detection (SPA support)
// ---------------------------------------------------------------------------
const bc = new BroadcastChannel('side-notes');
function notifyTab(tabId, url) {
const key = buildNoteKey(url);
bc.postMessage({ type: 'navigation', tabId, key });
}
chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
// details.frameId === 0 => main frame only (ignore iframes)
if (details.frameId === 0) {
notifyTab(details.tabId, details.url);
}
});
chrome.webNavigation.onReferenceFragmentUpdated.addListener((details) => {
if (details.frameId === 0) {
notifyTab(details.tabId, details.url);
}
});