## Browser extension (Manifest V3) ### manifest.json ```json { "manifest_version": 3, "name": "PocketLink", "version": "1.0.0", "description": "Create shortlinks from right-click context menu", "permissions": [ "contextMenus", "storage", "activeTab", "scripting", "notifications", "offscreen" ], "background": { "service_worker": "background.js", "type": "module" }, "action": { "default_popup": "popup.html", "default_icon": { "16": "icons/icon16.png", "48": "icons/icon48.png", "128": "icons/icon128.png" } }, "options_page": "options.html", "icons": { "16": "icons/icon16.png", "48": "icons/icon48.png", "128": "icons/icon128.png" } } ``` ### Service worker (background.js) ```javascript // background.js - Service Worker // Create context menu on install chrome.runtime.onInstalled.addListener(() => { chrome.contextMenus.create({ id: 'create-shortlink', title: 'Create Shortlink', contexts: ['page', 'link'] }); }); // Handle context menu click chrome.contextMenus.onClicked.addListener(async (info, tab) => { if (info.menuItemId !== 'create-shortlink') return; const url = info.linkUrl || info.pageUrl; try { const shortUrl = await createShortlink(url); await copyToClipboard(shortUrl); showNotification('Shortlink Created', shortUrl); } catch (error) { showNotification('Error', error.message); } }); async function createShortlink(longUrl) { const { apiToken } = await chrome.storage.sync.get('apiToken'); if (!apiToken) throw new Error('API token not configured'); const response = await fetch('https://api-ssl.bitly.com/v4/shorten', { method: 'POST', headers: { 'Authorization': `Bearer ${apiToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ long_url: longUrl }) }); if (!response.ok) throw new Error('API request failed'); const data = await response.json(); return data.link; } // Clipboard methods (three fallback strategies) // Method 1: Offscreen API (preferred) async function copyToClipboard(text) { try { await copyViaOffscreen(text); } catch { try { await copyViaContentScript(text); } catch { await copyViaPopup(text); } } } async function copyViaOffscreen(text) { await chrome.offscreen.createDocument({ url: 'offscreen.html', reasons: ['CLIPBOARD'], justification: 'Copy shortlink to clipboard' }); await chrome.runtime.sendMessage({ type: 'copy', text }); await chrome.offscreen.closeDocument(); } async function copyViaContentScript(text) { const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); await chrome.scripting.executeScript({ target: { tabId: tab.id }, func: (text) => navigator.clipboard.writeText(text), args: [text] }); } function showNotification(title, message) { chrome.notifications.create({ type: 'basic', iconUrl: 'icons/icon48.png', title, message }); } ``` ### Options page ```html