side-notes/background.js
Fabio Montefuscolo 4365b7687b feat: implement Side Notes Chrome extension
- Manifest V3 with sidePanel, tabs, webNavigation, unlimitedStorage permissions
- Background service worker: sidePanel setup + SPA navigation detection
- Shared modules: URL key derivation (buildNoteKey) and IndexedDB storage
- Side panel: per-tab markdown editor with debounced autosave, in-panel find
- Cross-panel consistency via BroadcastChannel with focus-aware refresh
- Notes-list page: day-grouped entries, click-to-focus/navigate behavior
- Dark theme by default
2026-08-03 19:51:34 +02:00

38 lines
1.3 KiB
JavaScript

// 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);
}
});