# LinkSyncExtension - Design Documentation ## Architecture Overview LinkSyncExtension is a Firefox browser extension that synchronizes bookmarks with LinkSyncServer. It runs as a background service worker with popup and settings interfaces. ### Component Architecture ``` ┌─────────────────────────────────────────────────────┐ │ LinkSyncExtension │ │ │ │ ┌─────────────────┐ ┌─────────────────────────┐ │ │ │ Background │ │ Popup UI │ │ │ │ Service │ │ (Add/Edit Bookmarks) │ │ │ │ Worker │ │ │ │ │ └─────────────────┘ └─────────────────────────┘ │ │ ┌─────────────────┐ ┌─────────────────────────┐ │ │ │ Storage │ │ Settings UI │ │ │ │ Manager │ │ (Configuration) │ │ │ └─────────────────┘ └─────────────────────────┘ │ │ ┌─────────────────┐ ┌─────────────────────────┐ │ │ │ Sync Engine │ │ Query Engine │ │ │ │ (3 modes) │ │ (Parser + Executor) │ │ │ └─────────────────┘ └─────────────────────────┘ │ │ ┌─────────────────┐ ┌─────────────────────────┐ │ │ │ Bookmark │ │ API Client │ │ │ │ Manipulator │ │ (REST calls) │ │ │ └─────────────────┘ └─────────────────────────┘ │ │ │ │ ┌───────────────────────────────────────────┐ │ │ │ Browser Bookmarks API │ │ │ └───────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────┘ ``` ## File Structure ``` LinkSyncExtension/ ├── manifest.json # Extension manifest v2 ├── popup.html # Bookmark add/edit UI ├── popup.css # Popup styling ├── popup.js # Popup logic ├── background.html # Settings page ├── background.js # Service worker ├── content/ │ └── content.js # Content script (optional) ├── utils/ │ ├── bookmark.js # Bookmark manipulation │ ├── collection.js # Collection management │ ├── query-engine.js # Query parsing/execution │ └── sync.js # Sync logic ├── icons/ │ ├── icon-48.png # 48x48 icon │ └── icon-96.png # 96x96 icon └── styles/ ├── base.css # Common styles └── theme.css # Theme variables ``` ## Manifest Design ### manifest.json ```json { "manifest_version": 2, "name": "LinkSync", "version": "1.0.0", "description": "Sync bookmarks with LinkSyncServer", "permissions": [ "bookmarks", "storage", "activeTab", "http://*/*", "https://*/*" ], "browser_action": { "default_icon": { "48": "icons/icon-48.png", "96": "icons/icon-96.png" }, "default_title": "LinkSync" }, "background": { "page": "background.html" }, "browser_specific_settings": { "gecko": { "id": "{linksync-id}", "strict_min_version": "109.0" } } } ``` ### Permissions - `bookmarks` - Read/write Firefox bookmarks - `storage` - Store settings, API keys, state - `activeTab` - Get current page data - HTTP/HTTPS - API communication ## Background Worker Design ### Responsibilities 1. **Sync Loop** - Check for pending syncs - Compare browser vs server bookmarks - Apply sync mode rules - Handle conflicts 2. **Event Handlers** - `onMessage` - UI requests - `onInstall` - Initialization - `onUpdate` - Handle version changes 3. **State Management** - Store collection mapping - Track sync timestamps - Monitor pending changes ### Code Structure ```javascript // background.js const Background = { // Constants SYNC_CHECK_INTERVAL: 60000, // 1 minute // Storage keys STORAGE: { API_KEY: 'linksync_api_key', COLLECTION: 'linksync_collection', MODE: 'linksync_sync_mode', DELETIONS: 'linksync_deletions', AUTO_SYNC: 'linksync_auto_sync' }, // Methods init(), // Initialize on install/update checkSync(), // Run sync loop handleSyncAction(), // Process sync actions handleEvent(), // Event handlers sendMessage(), // UI communication authenticate() // Handle auth }; ``` ### Sync Logic ```javascript async function handleSync() { const config = await loadConfig(); // Get browser bookmarks const browserBookmarks = await getBrowserBookmarks(); // Get server bookmarks via API const serverBookmarks = await fetchServerBookmarks(); // Apply sync mode const actions = applySyncMode(config.mode, browserBookmarks, serverBookmarks); // Process deletions if enabled if (config.deletions) { actions = applyDeletions(actions); } // Apply actions await applyActions(actions); // Update sync timestamp await saveSyncTimestamp(); } ``` ### Sync Modes | Mode | Browser→Server | Server→Browser | |------|---------------|---------------| | **Bi-directional** | Push | Push | | **Browser Authoritative** | Push | Overwrite | | **Server Authoritative** | Download | Overwrite | ## Popup Design ### Components 1. **Add/Edit Form** - URL (auto-filled) - Title (auto-filled) - Description (auto-filled) - Notes - Tags input - Folder path - Actions (Add, Edit, Delete) 2. **Bookmark List** - Paginated list of synced bookmarks - Search filter - Select for batch operations 3. **Collections Panel** - View all collections - Execute query - Create dynamic collection 4. **Settings Modal** - Server URL - API Key - Collection name - Sync mode - Auto-sync toggle ### HTML Structure ```html