|
| 1 | +const DB_NAME = "brain-vault-capture"; |
| 2 | +const STORE_NAME = "handles"; |
| 3 | +const RAW_HANDLE_KEY = "raw-folder"; |
| 4 | + |
| 5 | +const MENU_PAGE = "brain-capture-page"; |
| 6 | +const MENU_SELECTION = "brain-capture-selection"; |
| 7 | + |
| 8 | +chrome.runtime.onInstalled.addListener(createContextMenus); |
| 9 | +chrome.runtime.onStartup.addListener(createContextMenus); |
| 10 | + |
| 11 | +chrome.contextMenus.onClicked.addListener(async (info, tab) => { |
| 12 | + try { |
| 13 | + if (!tab?.id || isBlockedUrl(tab.url)) { |
| 14 | + await showNeedsPopup("This page cannot be captured from the context menu."); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const rawDirectoryHandle = await loadRawHandle(); |
| 19 | + if (!rawDirectoryHandle || !(await hasWritePermission(rawDirectoryHandle))) { |
| 20 | + await showNeedsPopup("Open Brain Vault Capture and choose vault/_raw first."); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + const page = info.menuItemId === MENU_SELECTION |
| 25 | + ? buildSelectionPage(info, tab) |
| 26 | + : await extractPageFromTab(tab.id); |
| 27 | + |
| 28 | + const filename = buildFilename(page.title || tab.title || "web-capture"); |
| 29 | + const markdown = buildMarkdown(page, ""); |
| 30 | + const savedFilename = await writeMarkdown(rawDirectoryHandle, filename, markdown); |
| 31 | + await flashBadge("ok", "#9be2b5", savedFilename); |
| 32 | + } catch (error) { |
| 33 | + await flashBadge("!", "#e58f85", error.message); |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +function createContextMenus() { |
| 38 | + chrome.contextMenus.removeAll(() => { |
| 39 | + chrome.contextMenus.create({ |
| 40 | + id: MENU_PAGE, |
| 41 | + title: "Capture page to brain raw", |
| 42 | + contexts: ["page"] |
| 43 | + }); |
| 44 | + |
| 45 | + chrome.contextMenus.create({ |
| 46 | + id: MENU_SELECTION, |
| 47 | + title: "Capture selection to brain raw", |
| 48 | + contexts: ["selection"] |
| 49 | + }); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +function isBlockedUrl(url = "") { |
| 54 | + return url.startsWith("chrome://") || url.startsWith("chrome-extension://"); |
| 55 | +} |
| 56 | + |
| 57 | +async function showNeedsPopup(title) { |
| 58 | + await chrome.action.setTitle({ title }); |
| 59 | + await flashBadge("set", "#8ee7d2", title); |
| 60 | + |
| 61 | + if (chrome.action.openPopup) { |
| 62 | + await chrome.action.openPopup(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +async function flashBadge(text, color, title) { |
| 67 | + await chrome.action.setBadgeText({ text }); |
| 68 | + await chrome.action.setBadgeBackgroundColor({ color }); |
| 69 | + if (title) { |
| 70 | + await chrome.action.setTitle({ title }); |
| 71 | + } |
| 72 | + |
| 73 | + setTimeout(() => { |
| 74 | + chrome.action.setBadgeText({ text: "" }); |
| 75 | + }, 2400); |
| 76 | +} |
| 77 | + |
| 78 | +async function hasWritePermission(directoryHandle) { |
| 79 | + const options = { mode: "readwrite" }; |
| 80 | + return directoryHandle.queryPermission |
| 81 | + && await directoryHandle.queryPermission(options) === "granted"; |
| 82 | +} |
| 83 | + |
| 84 | +async function extractPageFromTab(tabId) { |
| 85 | + const [{ result }] = await chrome.scripting.executeScript({ |
| 86 | + target: { tabId }, |
| 87 | + func: extractPage |
| 88 | + }); |
| 89 | + |
| 90 | + return result; |
| 91 | +} |
| 92 | + |
| 93 | +function buildSelectionPage(info, tab) { |
| 94 | + const selectedText = (info.selectionText || "").trim(); |
| 95 | + |
| 96 | + return { |
| 97 | + title: tab.title || "Selected web text", |
| 98 | + url: tab.url || info.pageUrl || info.frameUrl || "", |
| 99 | + description: "", |
| 100 | + selection: selectedText, |
| 101 | + text: "" |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +async function writeMarkdown(directoryHandle, filename, markdown) { |
| 106 | + const fileHandle = await getUniqueFileHandle(directoryHandle, filename); |
| 107 | + const writable = await fileHandle.createWritable(); |
| 108 | + await writable.write(markdown); |
| 109 | + await writable.close(); |
| 110 | + return fileHandle.name; |
| 111 | +} |
| 112 | + |
| 113 | +async function getUniqueFileHandle(directoryHandle, filename) { |
| 114 | + const extension = ".md"; |
| 115 | + const basename = filename.endsWith(extension) ? filename.slice(0, -extension.length) : filename; |
| 116 | + |
| 117 | + for (let attempt = 0; attempt < 100; attempt += 1) { |
| 118 | + const candidate = attempt === 0 ? filename : `${basename}-${attempt + 1}${extension}`; |
| 119 | + try { |
| 120 | + await directoryHandle.getFileHandle(candidate); |
| 121 | + } catch (error) { |
| 122 | + if (error.name === "NotFoundError") { |
| 123 | + return directoryHandle.getFileHandle(candidate, { create: true }); |
| 124 | + } |
| 125 | + |
| 126 | + throw error; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return directoryHandle.getFileHandle(`${basename}-${Date.now()}${extension}`, { create: true }); |
| 131 | +} |
| 132 | + |
| 133 | +function buildFilename(title) { |
| 134 | + const date = new Date().toISOString().slice(0, 10); |
| 135 | + const slug = title |
| 136 | + .toLowerCase() |
| 137 | + .normalize("NFKD") |
| 138 | + .replace(/[\u0300-\u036f]/g, "") |
| 139 | + .replace(/[^a-z0-9]+/g, "-") |
| 140 | + .replace(/^-+|-+$/g, "") |
| 141 | + .slice(0, 72) || "web-capture"; |
| 142 | + |
| 143 | + return `${date}-${slug}.md`; |
| 144 | +} |
| 145 | + |
| 146 | +function buildMarkdown(page, note) { |
| 147 | + const now = new Date().toISOString(); |
| 148 | + const safeTitle = escapeYaml(page.title || "Untitled page"); |
| 149 | + const cleanNote = note.trim(); |
| 150 | + const selectedText = page.selection ? `\n## Selection\n\n${page.selection}\n` : ""; |
| 151 | + const noteText = cleanNote ? `\n## Capture Note\n\n${cleanNote}\n` : ""; |
| 152 | + const description = page.description ? `\n> ${page.description}\n` : ""; |
| 153 | + const pageContent = page.text ? `\n## Page Content\n\n${page.text}\n` : ""; |
| 154 | + |
| 155 | + return `---\ntitle: ${safeTitle}\ntags: [web-capture, raw-ingest]\nsources:\n - ${page.url}\ncreated: ${now}\ncaptured: chrome-extension\n---\n\n# ${page.title || "Untitled page"}\n\n${description}\n- Source: ${page.url}\n- Captured: ${now}\n${noteText}${selectedText}${pageContent}`; |
| 156 | +} |
| 157 | + |
| 158 | +function escapeYaml(value) { |
| 159 | + return JSON.stringify(String(value)); |
| 160 | +} |
| 161 | + |
| 162 | +function openDb() { |
| 163 | + return new Promise((resolve, reject) => { |
| 164 | + const request = indexedDB.open(DB_NAME, 1); |
| 165 | + request.onupgradeneeded = () => request.result.createObjectStore(STORE_NAME); |
| 166 | + request.onsuccess = () => resolve(request.result); |
| 167 | + request.onerror = () => reject(request.error); |
| 168 | + }); |
| 169 | +} |
| 170 | + |
| 171 | +async function loadRawHandle() { |
| 172 | + const db = await openDb(); |
| 173 | + return new Promise((resolve, reject) => { |
| 174 | + const transaction = db.transaction(STORE_NAME, "readonly"); |
| 175 | + const request = transaction.objectStore(STORE_NAME).get(RAW_HANDLE_KEY); |
| 176 | + request.onsuccess = () => resolve(request.result); |
| 177 | + request.onerror = () => reject(request.error); |
| 178 | + transaction.oncomplete = () => db.close(); |
| 179 | + }); |
| 180 | +} |
| 181 | + |
| 182 | +function extractPage() { |
| 183 | + const selectorsToRemove = "script, style, noscript, svg, canvas, iframe, nav, footer, aside, form"; |
| 184 | + const clone = document.body ? document.body.cloneNode(true) : document.documentElement.cloneNode(true); |
| 185 | + clone.querySelectorAll(selectorsToRemove).forEach((node) => node.remove()); |
| 186 | + |
| 187 | + const article = clone.querySelector("article, main, [role='main']") || clone; |
| 188 | + const rawText = article.innerText || clone.innerText || document.body?.innerText || ""; |
| 189 | + const text = rawText |
| 190 | + .replace(/\u00a0/g, " ") |
| 191 | + .replace(/[ \t]+\n/g, "\n") |
| 192 | + .replace(/\n{3,}/g, "\n\n") |
| 193 | + .trim() |
| 194 | + .slice(0, 140000); |
| 195 | + |
| 196 | + const selection = String(window.getSelection?.() || "") |
| 197 | + .replace(/\n{3,}/g, "\n\n") |
| 198 | + .trim() |
| 199 | + .slice(0, 30000); |
| 200 | + |
| 201 | + const description = document.querySelector("meta[name='description'], meta[property='og:description']") |
| 202 | + ?.getAttribute("content") |
| 203 | + ?.trim() || ""; |
| 204 | + |
| 205 | + return { |
| 206 | + title: document.title, |
| 207 | + url: location.href, |
| 208 | + description, |
| 209 | + selection, |
| 210 | + text |
| 211 | + }; |
| 212 | +} |
0 commit comments