diff --git a/frontend/extension/README.md b/frontend/extension/README.md index 7b5f62d..6c226b3 100644 --- a/frontend/extension/README.md +++ b/frontend/extension/README.md @@ -10,11 +10,18 @@ These screenshots show the actual popup running in Chromium with a local demo ba | --- | --- | |  |  | +Right-click capture opens a separate window with the clicked target and destination library ready for confirmation: + +| Light mode | Dark mode | +| --- | --- | +|  |  | + ## Capabilities | Capability | Current behavior | | --- | --- | | Capture the current page | Prefills from the active tab's URL, selected identifier, or explicit scholarly metadata. | +| Capture with a right-click | Opens the same capture UI in a small window for the clicked page, link, or selected identifier; waits for you to confirm Save. | | Paste an identifier | Accepts DOIs, arXiv IDs, and HTTP/HTTPS URLs independently of the current page. | | Choose an item type | Automatically detects papers, websites, and GitHub repositories, with manual type overrides. | | Choose a destination | Saves new items to Inbox in the selected ResearchOS library. | @@ -48,6 +55,9 @@ Rebuild and click **Reload** on `chrome://extensions` after changing the source. ## Capturing research - Click the toolbar icon or press **Alt+Shift+S** to open the popup. Customize the shortcut at `chrome://extensions/shortcuts`. +- Right-click a web page, link, or selected DOI/arXiv identifier/URL and choose **Save to ResearchOS**. A small capture window shows that target and your last saved library. Review it and click **Save to Inbox**; opening the window does not import anything automatically. +- Right-clicked links and selections take precedence over the containing page. Invalid selections stay in the input with an explanation so you can correct them. Page captures can use scholarly metadata, including metadata inside a clicked frame where Chrome allows access. +- Each right-click capture has its own temporary target, so opening another capture does not replace one you are reviewing. Closing the capture window clears that target; browser restart or extension reload clears all temporary targets. A capture window restored after that asks you to capture again. - The extension uses the current page's URL and explicit scholarly metadata such as `citation_doi`. A selected DOI, arXiv identifier, or URL takes precedence. - Paste an identifier or URL to save something other than the current page. - **Auto** recognizes DOI/arXiv, OpenReview, Zenodo, PDF and GitHub links. Use **Paper** for publisher pages without scholarly metadata, **Website** to keep a page as a website, or **GitHub** for a repository. Chrome-internal pages and local files cannot be captured; paste a public identifier instead. @@ -61,7 +71,7 @@ The backend writes to the same Supabase database configured for your ResearchOS The extension follows this repository's current unauthenticated backend API. Deployments behind an additional sign-in proxy or custom token authentication need a corresponding authentication integration; this extension does not copy browser sessions or store database keys. -Required permissions are `activeTab`, `scripting`, and `storage`. Page metadata is read only when opening the popup. Server access is requested for the configured hostname on connection; Chrome host permissions cover that host's ports, but fetches use the exact configured URL. No permanent access to every page, browsing history, third-party fonts, or analytics is requested. Only the identifier/URL and library ID are sent to ResearchOS. The backend's usual metadata and AI behavior still applies. +Required permissions are `activeTab`, `scripting`, `storage`, and `contextMenus`. Page metadata is read only when you open the toolbar popup or choose to capture a page from the context menu. Link/selection captures use the explicit target without reading the containing page's metadata. Temporary right-click targets live in extension session storage and are removed when their capture window closes. Server access is requested for the configured hostname on connection; Chrome host permissions cover that host's ports, but fetches use the exact configured URL. No permanent access to every page, browsing history, third-party fonts, or analytics is requested. Only the identifier/URL and library ID are sent to ResearchOS. The backend's usual metadata and AI behavior still applies. If a request loses its connection, times out, or the browser stops during a save, the result may be uncertain even if ResearchOS completed the import. Check Inbox before retrying. The extension never automatically replays a write. Requests time out after 25 seconds to respect Chrome's service-worker fetch limit. @@ -71,7 +81,6 @@ These are ideas for later work, not capabilities included in this release. The o | Addition | What it would enable | Implementation considerations | | --- | --- | --- | -| Right-click capture | Save a page, link, or selected DOI from Chrome's context menu. | Add the context-menu permission and reuse detection and import handling. | | Collections, tags, and capture notes | File an item and record why it matters while saving. | Load library-scoped collections and define how annotations apply to existing duplicates. | | Batch capture | Import selected tabs, bookmarks, or a pasted list of identifiers. | Show per-item progress, cancellation, duplicate review, and rate limits. | | PDF attachment upload | Upload a local or already-open PDF when a DOI or public paper link is unavailable. | Use the app's PDF metadata and upload flow, with explicit file selection and size limits. | @@ -93,6 +102,6 @@ npx playwright install chromium npm run test:extension ``` -The browser test loads a disposable copy of the built Manifest V3 extension in Chromium and uses a local mock API, without contacting your database. It grants only the mock server's host in that disposable copy, since headless Chromium cannot display Chrome's native optional-permission prompt. It verifies service-worker imports for all three item types, library selection, duplicate handling, real script injection, theme persistence, and light/dark screenshots. The shipped manifest retains optional server access only. Browser output is under `frontend/test-results/extension`. +The browser test loads a disposable copy of the built Manifest V3 extension in Chromium and uses a local mock API, without contacting your database. It grants only the mock server's host in that disposable copy, since headless Chromium cannot display Chrome's native optional-permission prompt. It verifies service-worker imports for all three item types, library selection, duplicate handling, real script injection, theme persistence, and light/dark screenshots. It also checks menu registration and capture-window/session handoff for page, link, and selection intents, including explicit save confirmation and cleanup. Context-menu event handling and target extraction are covered by unit tests rather than automating Chrome's native menu. The shipped manifest retains optional server access only. Browser output is under `frontend/test-results/extension`. Chrome references: [host permissions and network requests](https://developer.chrome.com/docs/extensions/develop/concepts/network-requests), [runtime permission requests](https://developer.chrome.com/docs/extensions/reference/api/permissions), and [service worker lifecycle](https://developer.chrome.com/docs/extensions/develop/concepts/service-workers/lifecycle). diff --git a/frontend/extension/e2e/extension.spec.js b/frontend/extension/e2e/extension.spec.js index 99ef8b3..2828b6c 100644 --- a/frontend/extension/e2e/extension.spec.js +++ b/frontend/extension/e2e/extension.spec.js @@ -100,6 +100,52 @@ test('unpacked extension connects, imports all item types, handles duplicates, a await expect(popup.getByRole('heading', { name: 'Save to Inbox' })).toBeVisible() await expect(popup.getByLabel('Save to library')).toHaveValue('lib_2') await expect(popup.getByLabel('Color theme')).toHaveValue('dark') + + // Context-menu event handling and target extraction are covered in contextMenu.test. + // Here exercise the browser/session/window handoff with each resulting intent. + await worker.evaluate(async () => { + await chrome.contextMenus.update('researchos-save-to-inbox', { title: 'Save to ResearchOS' }) + }) + for (const [contextType, identifier, type, reason] of [ + ['selection', '10.1000/context-selection', 'paper', 'Selected text'], + ['link', 'https://example.org/linked-article', 'website', 'Right-clicked link'], + ['page', '10.1000/context-page', 'paper', 'Current page'], + ]) { + const previousImports = imported.length + const opened = context.waitForEvent('page') + const captureId = await worker.evaluate(async capture => { + const id = crypto.randomUUID() + const key = `researchos.contextCapture.${id}` + await chrome.storage.session.set({ [key]: capture }) + const window = await chrome.windows.create({ url: chrome.runtime.getURL(`popup.html?capture=${id}`), type: 'popup', width: 420, height: 680 }) + await chrome.storage.session.set({ [key]: { ...capture, windowId: window.id } }) + return id + }, { context: contextType, identifier, page: { title: identifier }, source: { + type, identifier, reason, + buttonLabel: { page: 'Use this page', link: 'Use this link', selection: 'Use this selection' }[contextType], + }, error: '' }) + const captureWindow = await opened + await expect(captureWindow.getByRole('heading', { name: 'Save to Inbox' })).toBeVisible() + await expect(captureWindow.getByLabel('DOI, arXiv ID, or URL')).toHaveValue(identifier) + await expect(captureWindow.getByLabel('Save to library')).toHaveValue('lib_2') + await expect(captureWindow.getByText(reason)).toBeVisible() + await expect(captureWindow.getByRole('button', { name: { page: 'Use this page', link: 'Use this link', selection: 'Use this selection' }[contextType] })).toBeVisible() + expect(imported.length).toBe(previousImports) + if (contextType === 'link') { + for (const theme of ['light', 'dark']) { + await captureWindow.getByLabel('Color theme').selectOption(theme) + await captureWindow.locator('#root').screenshot({ path: testInfo.outputPath(`context-capture-${theme}.png`), animations: 'disabled' }) + } + } + await captureWindow.getByRole('button', { name: 'Save to Inbox', exact: true }).click() + await expect(captureWindow.getByText('Saved to Inbox', { exact: true })).toBeVisible() + expect(imported.at(-1)).toEqual({ + path: type === 'paper' ? '/api/papers/import' : '/api/websites/import', + body: { [type === 'paper' ? 'identifier' : 'url']: identifier, library_id: 'lib_2' }, + }) + await captureWindow.close() + await expect.poll(() => worker.evaluate(async id => (await chrome.storage.session.get(`researchos.contextCapture.${id}`))[`researchos.contextCapture.${id}`], captureId)).toBeUndefined() + } expect(errors).toEqual([]) } finally { await context?.close() diff --git a/frontend/extension/public/manifest.json b/frontend/extension/public/manifest.json index 67bee31..1911d95 100644 --- a/frontend/extension/public/manifest.json +++ b/frontend/extension/public/manifest.json @@ -4,7 +4,7 @@ "description": "Save papers, DOIs, websites, and GitHub repositories to your ResearchOS Inbox.", "version": "0.1.0", "minimum_chrome_version": "109", - "permissions": ["activeTab", "scripting", "storage"], + "permissions": ["activeTab", "scripting", "storage", "contextMenus"], "optional_host_permissions": ["http://*/*", "https://*/*"], "action": { "default_popup": "popup.html", diff --git a/frontend/extension/screenshots/context-capture-dark.png b/frontend/extension/screenshots/context-capture-dark.png new file mode 100644 index 0000000..9e1ab0b Binary files /dev/null and b/frontend/extension/screenshots/context-capture-dark.png differ diff --git a/frontend/extension/screenshots/context-capture-light.png b/frontend/extension/screenshots/context-capture-light.png new file mode 100644 index 0000000..1f7e0ad Binary files /dev/null and b/frontend/extension/screenshots/context-capture-light.png differ diff --git a/frontend/src/extension/App.jsx b/frontend/src/extension/App.jsx index 74d239d..48cc9a6 100644 --- a/frontend/src/extension/App.jsx +++ b/frontend/src/extension/App.jsx @@ -101,21 +101,30 @@ export default function App() { let alive = true async function start() { try { - const [state, currentPage] = await Promise.all([command('GET_STATE'), captureActivePage().catch(() => null)]) + const captureId = new URLSearchParams(window.location.search).get('capture') + const captureRequest = captureId !== null + ? command('GET_CONTEXT_CAPTURE', { id: captureId }).catch(err => ({ identifier: '', error: err.message })) + : captureActivePage().then(currentPage => ({ + page: currentPage, + source: currentPage ? sourceFromPage(currentPage) : null, + })).catch(() => ({})) + const [state, capture] = await Promise.all([command('GET_STATE'), captureRequest]) if (!alive) return setSettings(state.settings) setLibraryId(state.settings.libraryId) setRecent(state.recent || []) - const source = currentPage ? sourceFromPage(currentPage) : null - setPage(currentPage) + const source = capture.source + setPage(capture.page) setPageSource(source) - if (state.job?.status === 'saving') { + if (capture.error) setError(capture.error) + if (state.job?.status === 'saving' && captureId === null) { setIdentifier(state.job.identifier) setLibraryId(state.job.libraryId) setJob(state.job) } else { - setIdentifier(source?.identifier || '') - if (state.job && (['error', 'uncertain'].includes(state.job.status) || state.job.identifier === source?.identifier)) setJob(state.job) + setIdentifier(capture.identifier ?? source?.identifier ?? '') + if (state.job && (state.job.status === 'saving' || state.job.identifier === (capture.identifier ?? source?.identifier) || + (captureId === null && ['error', 'uncertain'].includes(state.job.status)))) setJob(state.job) } if (state.settings.connected) { try { @@ -183,7 +192,7 @@ export default function App() {
Keep a paper, a page, or your next idea.
{pageSource.reason}
{page?.title || pageSource.identifier}
{pageSource.reason}
{page?.title || pageSource.identifier}