From 71604bd767d9a2266daead947459bbd67c2a09b7 Mon Sep 17 00:00:00 2001 From: Pouri Date: Sun, 1 Nov 2026 09:44:04 +0330 Subject: [PATCH] feat(capture): capture a whole scrolling area When the element picker's selection is, or sits inside, a box that scrolls vertically and is fully on screen, the picker offers "Capture whole scrolling area". Choosing it tags that box data-oss-scroll-target and runs the full-page pipeline with scrollTarget on: getMetrics uses the tagged box as the scroller, so each tile is cropped to it and the stitched image holds the box's full content. restoreCapture removes the tag, and a stale one is cleared by the next picker or full-page capture. The capture is stored with the element mode. No new permissions. The element picker smoke gains a scrolling box fixture covering the offer, the tag, the measurement, and the cleanup. --- public/_locales/ar/messages.json | 4 ++ public/_locales/de/messages.json | 4 ++ public/_locales/en/messages.json | 4 ++ public/_locales/es/messages.json | 4 ++ public/_locales/es_419/messages.json | 4 ++ public/_locales/fr/messages.json | 4 ++ public/_locales/hi/messages.json | 4 ++ public/_locales/id/messages.json | 4 ++ public/_locales/it/messages.json | 4 ++ public/_locales/ja/messages.json | 4 ++ public/_locales/ko/messages.json | 4 ++ public/_locales/nl/messages.json | 4 ++ public/_locales/pl/messages.json | 4 ++ public/_locales/pt_BR/messages.json | 4 ++ public/_locales/ru/messages.json | 4 ++ public/_locales/tr/messages.json | 4 ++ public/_locales/vi/messages.json | 4 ++ public/_locales/zh_CN/messages.json | 4 ++ public/_locales/zh_TW/messages.json | 4 ++ src/background/index.ts | 19 +++++-- src/content/element-select.ts | 38 ++++++++++++-- src/content/scroll-capture.ts | 12 +++-- tests/browser/element-select-smoke.mjs | 68 +++++++++++++++++++++++++- 23 files changed, 202 insertions(+), 11 deletions(-) diff --git a/public/_locales/ar/messages.json b/public/_locales/ar/messages.json index faa99024..fc9d39b9 100644 --- a/public/_locales/ar/messages.json +++ b/public/_locales/ar/messages.json @@ -67,6 +67,10 @@ "message": "التقاط الصفحة الكاملة", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "التقاط منطقة التمرير بالكامل", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "إلغاء", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/de/messages.json b/public/_locales/de/messages.json index 33e437b8..5d1c2c36 100644 --- a/public/_locales/de/messages.json +++ b/public/_locales/de/messages.json @@ -67,6 +67,10 @@ "message": "Ganze Seite erfassen", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Ganzen Scrollbereich erfassen", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Abbrechen", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/en/messages.json b/public/_locales/en/messages.json index c0b6f4b2..721cfe01 100644 --- a/public/_locales/en/messages.json +++ b/public/_locales/en/messages.json @@ -67,6 +67,10 @@ "message": "Capture full page", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Capture whole scrolling area", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Cancel", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/es/messages.json b/public/_locales/es/messages.json index 2ff96d0d..dff59372 100644 --- a/public/_locales/es/messages.json +++ b/public/_locales/es/messages.json @@ -67,6 +67,10 @@ "message": "Capturar página completa", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Capturar toda el área desplazable", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Cancelar", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/es_419/messages.json b/public/_locales/es_419/messages.json index 3321cdac..5ec2bb51 100644 --- a/public/_locales/es_419/messages.json +++ b/public/_locales/es_419/messages.json @@ -67,6 +67,10 @@ "message": "Capturar página completa", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Capturar toda el área desplazable", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Cancelar", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/fr/messages.json b/public/_locales/fr/messages.json index 7e74cdb6..d1b1140a 100644 --- a/public/_locales/fr/messages.json +++ b/public/_locales/fr/messages.json @@ -67,6 +67,10 @@ "message": "Capturer la page entière", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Capturer toute la zone défilante", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Annuler", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/hi/messages.json b/public/_locales/hi/messages.json index 6eb6fa0f..2cc9c752 100644 --- a/public/_locales/hi/messages.json +++ b/public/_locales/hi/messages.json @@ -67,6 +67,10 @@ "message": "पूरा पेज कैप्चर करें", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "पूरा स्क्रॉल क्षेत्र कैप्चर करें", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "रद्द करें", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/id/messages.json b/public/_locales/id/messages.json index 771c694f..0f8d460b 100644 --- a/public/_locales/id/messages.json +++ b/public/_locales/id/messages.json @@ -67,6 +67,10 @@ "message": "Tangkap halaman penuh", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Tangkap seluruh area gulir", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Batal", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/it/messages.json b/public/_locales/it/messages.json index ab702c56..dc8779fb 100644 --- a/public/_locales/it/messages.json +++ b/public/_locales/it/messages.json @@ -67,6 +67,10 @@ "message": "Cattura pagina intera", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Cattura l'intera area scorrevole", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Annulla", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/ja/messages.json b/public/_locales/ja/messages.json index 65a4df66..f459a52a 100644 --- a/public/_locales/ja/messages.json +++ b/public/_locales/ja/messages.json @@ -67,6 +67,10 @@ "message": "ページ全体をキャプチャ", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "スクロール領域全体をキャプチャ", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "キャンセル", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/ko/messages.json b/public/_locales/ko/messages.json index c02f8514..0567b855 100644 --- a/public/_locales/ko/messages.json +++ b/public/_locales/ko/messages.json @@ -67,6 +67,10 @@ "message": "전체 페이지 캡처", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "스크롤 영역 전체 캡처", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "취소", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/nl/messages.json b/public/_locales/nl/messages.json index 7d34da20..52f8d0cd 100644 --- a/public/_locales/nl/messages.json +++ b/public/_locales/nl/messages.json @@ -67,6 +67,10 @@ "message": "Volledige pagina vastleggen", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Volledig scrollgebied vastleggen", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Annuleren", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/pl/messages.json b/public/_locales/pl/messages.json index 669a8167..9965eb22 100644 --- a/public/_locales/pl/messages.json +++ b/public/_locales/pl/messages.json @@ -67,6 +67,10 @@ "message": "Przechwyć całą stronę", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Przechwyć cały przewijany obszar", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Anuluj", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/pt_BR/messages.json b/public/_locales/pt_BR/messages.json index 10998e75..3f49904c 100644 --- a/public/_locales/pt_BR/messages.json +++ b/public/_locales/pt_BR/messages.json @@ -67,6 +67,10 @@ "message": "Capturar página inteira", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Capturar toda a área rolável", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Cancelar", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/ru/messages.json b/public/_locales/ru/messages.json index fd0cb648..2843e7a0 100644 --- a/public/_locales/ru/messages.json +++ b/public/_locales/ru/messages.json @@ -67,6 +67,10 @@ "message": "Снимок всей страницы", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Снимок всей прокручиваемой области", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Отмена", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/tr/messages.json b/public/_locales/tr/messages.json index 915f156b..37a0210c 100644 --- a/public/_locales/tr/messages.json +++ b/public/_locales/tr/messages.json @@ -67,6 +67,10 @@ "message": "Tam sayfayı yakala", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Tüm kaydırılabilir alanı yakala", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "İptal", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/vi/messages.json b/public/_locales/vi/messages.json index 16b6283a..a0e4a7e5 100644 --- a/public/_locales/vi/messages.json +++ b/public/_locales/vi/messages.json @@ -67,6 +67,10 @@ "message": "Chụp toàn trang", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "Chụp toàn bộ vùng cuộn", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "Hủy", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/zh_CN/messages.json b/public/_locales/zh_CN/messages.json index 9681685f..2054b7e4 100644 --- a/public/_locales/zh_CN/messages.json +++ b/public/_locales/zh_CN/messages.json @@ -67,6 +67,10 @@ "message": "截取整页", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "截取整个滚动区域", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "取消", "description": "Element picker: button to cancel selection." diff --git a/public/_locales/zh_TW/messages.json b/public/_locales/zh_TW/messages.json index e6ee6c9a..7a301aab 100644 --- a/public/_locales/zh_TW/messages.json +++ b/public/_locales/zh_TW/messages.json @@ -67,6 +67,10 @@ "message": "擷取整個網頁", "description": "Element picker: button to capture the full page instead." }, + "elementPickerScrollArea": { + "message": "擷取整個捲動區域", + "description": "Element picker: button shown when the selection is inside a box that scrolls; it captures the box's whole scrolling content, not just the visible part." + }, "elementPickerCancel": { "message": "取消", "description": "Element picker: button to cancel selection." diff --git a/src/background/index.ts b/src/background/index.ts index 53a2283e..727fa609 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -569,6 +569,7 @@ async function captureElement(tab: chrome.tabs.Tab): Promise { cancel: chrome.i18n.getMessage('elementPickerCancel'), larger: chrome.i18n.getMessage('elementPickerLarger'), smaller: chrome.i18n.getMessage('elementPickerSmaller'), + scrollArea: chrome.i18n.getMessage('elementPickerScrollArea'), }, ]); if (!selection) return; @@ -582,6 +583,12 @@ async function captureElement(tab: chrome.tabs.Tab): Promise { await captureFullPage(tab); return; } + if (selection.kind === 'scroll-area') { + // The whole content of a scrolling box: the full-page pipeline, scrolling + // the box the picker tagged and cropping to it. + await captureFullPage(tab, { mode: 'element', scrollTarget: true }); + return; + } // Element rectangles are viewport-relative, independent of inner scroll panels. // Read fresh dimensions: the user may resize or zoom while choosing. @@ -630,9 +637,13 @@ async function captureElement(tab: chrome.tabs.Tab): Promise { } } -async function captureFullPage(tab: chrome.tabs.Tab): Promise { +async function captureFullPage( + tab: chrome.tabs.Tab, + { mode = 'full-page', scrollTarget = false }: { mode?: CaptureMode; scrollTarget?: boolean } = {}, +): Promise { const tabId = tab.id as number; - const metrics = await execInTab(tabId, getMetrics, [true]); + // scrollTarget: scroll the box the element picker tagged instead of the page. + const metrics = await execInTab(tabId, getMetrics, [true, scrollTarget]); if (metrics.viewportHeight <= 0 || metrics.scrollHeight <= 0) { await runInTab(tabId, restoreCapture, []); broadcast({ @@ -704,7 +715,7 @@ async function captureFullPage(tab: chrome.tabs.Tab): Promise { dataUrl, width: canvasWidth, height: part.height, - mode: 'full-page', + mode, title: tab.title ?? '', url: tab.url ?? '', capturedAt: Date.now(), @@ -759,7 +770,7 @@ async function captureFullPage(tab: chrome.tabs.Tab): Promise { firstPartUrl, canvasWidth, canvasHeight, - 'full-page', + mode, tab.title ?? '', tab.url ?? '', windowId, diff --git a/src/content/element-select.ts b/src/content/element-select.ts index c5031b6f..561e8f91 100644 --- a/src/content/element-select.ts +++ b/src/content/element-select.ts @@ -10,15 +10,26 @@ export interface ElementPickerLabels { cancel: string; larger: string; smaller: string; + /** Button that captures the whole content of a scrolling box. */ + scrollArea: string; } -export type ElementSelection = { kind: 'element'; rect: PageRect } | { kind: 'full-page' }; +/** + * What the picker chose. `scroll-area` means the whole content of a scrolling + * box: the picker tags that box `data-oss-scroll-target`, and the full-page + * pipeline scrolls it instead of the page (getMetrics in scroll-capture.ts). + */ +export type ElementSelection = + { kind: 'element'; rect: PageRect } | { kind: 'full-page' } | { kind: 'scroll-area' }; /** Injected with executeScript: all runtime helpers must stay inside this function. */ export function selectElement(labels: ElementPickerLabels): Promise { return new Promise((resolve) => { const doc = document; const marker = 'data-openscreenshot-element-picker'; + const scrollTag = 'data-oss-scroll-target'; + // A tag left by a capture that never ran must not steer this one. + for (const el of doc.querySelectorAll(`[${scrollTag}]`)) el.removeAttribute(scrollTag); // A DOM event also works across separately serialized invocations. doc.querySelector(`[${marker}]`)?.dispatchEvent(new Event('openscreenshot-cancel')); let active = doc.activeElement; @@ -105,6 +116,8 @@ export function selectElement(labels: ElementPickerLabels): Promise { + for (let el = from; el && el !== doc.body && el !== doc.documentElement; el = parent(el)) { + if (!(el instanceof HTMLElement) || !usable(el)) continue; + const css = getComputedStyle(el); + if (!/^(auto|scroll)$/.test(css.overflowY) || el.scrollHeight <= el.clientHeight + 1) + continue; + return fullyVisible(el, el.getBoundingClientRect()) ? el : null; + } + return null; + }; const render = () => { if (current && !usable(current)) { current = null; @@ -267,6 +292,7 @@ export function selectElement(labels: ElementPickerLabels): Promise !el.disabled && !el.hidden, ); const index = available.indexOf(shadow.activeElement as HTMLButtonElement); diff --git a/src/content/scroll-capture.ts b/src/content/scroll-capture.ts index a0ed4859..6773022e 100644 --- a/src/content/scroll-capture.ts +++ b/src/content/scroll-capture.ts @@ -35,8 +35,13 @@ export function getWindowViewport() { * `data-oss-expanded`, so the capture holds them whole. {@link restoreCapture} * puts them back. */ -export function getMetrics(excludeScrollbars = false): Metrics { +export function getMetrics(excludeScrollbars = false, scrollTarget = false): Metrics { const de = document.documentElement; + // The element picker's "Capture whole scrolling area" tags the box to scroll. + // Only that capture uses the tag; any other capture clears a stale one. + const tagged = document.querySelector('[data-oss-scroll-target]'); + if (tagged && !scrollTarget) tagged.removeAttribute('data-oss-scroll-target'); + const forced = scrollTarget ? tagged : null; const dpr = window.devicePixelRatio || 1; // client dimensions exclude classic scrollbar gutters without reflowing // the page. Overlay scrollbars are made transparent during preparation. @@ -63,9 +68,9 @@ export function getMetrics(excludeScrollbars = false): Metrics { // When the document can grow, a panel that overflows by less than half a // viewport is expanded in place instead, which keeps the page around it. // ponytail: linear DOM scan, fine for a one-shot capture; index by overflow if it ever matters. - let scroller: HTMLElement | null = null; + let scroller: HTMLElement | null = forced; let bestOverflow = Math.max(documentScrolls ? vh * 0.5 : 4, de.scrollHeight - vh); - for (const el of document.querySelectorAll('*')) { + for (const el of forced ? [] : document.querySelectorAll('*')) { const cs = getComputedStyle(el); if (cs.overflowY !== 'auto' && cs.overflowY !== 'scroll') continue; const overflow = el.scrollHeight - el.clientHeight; @@ -324,6 +329,7 @@ export function restoreCapture(): void { } const scroller = document.querySelector('[data-oss-scroller="1"]'); if (scroller) delete scroller.dataset.ossScroller; + document.querySelector('[data-oss-scroll-target]')?.removeAttribute('data-oss-scroll-target'); } /** diff --git a/tests/browser/element-select-smoke.mjs b/tests/browser/element-select-smoke.mjs index d80b603c..dfd181ef 100644 --- a/tests/browser/element-select-smoke.mjs +++ b/tests/browser/element-select-smoke.mjs @@ -8,6 +8,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'; const ROOT = resolve(fileURLToPath(new URL('../..', import.meta.url))); const SOURCE = join(ROOT, 'src/content/element-select.ts'); +const SCROLL_SOURCE = join(ROOT, 'src/content/scroll-capture.ts'); const CHROME = process.env.CHROME_BIN ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; const labels = { @@ -20,6 +21,7 @@ const labels = { cancel: 'Cancel', larger: 'Larger', smaller: 'Smaller', + scrollArea: 'Capture whole scrolling area', }; async function loadPuppeteer() { @@ -52,7 +54,9 @@ html,body{margin:0;background:white;min-height:1600px;font:16px Arial} #overflow{width:200px;height:120px;background:white} #shadow{position:absolute;left:80px;top:440px;width:240px;height:100px} iframe{position:absolute;left:360px;top:440px;width:180px;height:100px;border:0} -
Image
`; +#feed{position:absolute;left:800px;top:80px;width:160px;height:200px;overflow-y:auto;background:#ddd} +#feed article{height:110px;margin:8px;background:white;border-radius:6px} +
Image
`; let browser; let work; @@ -410,6 +414,68 @@ try { 'restores focus inside open shadow roots', ); console.log('PASS shadow DOM, iframe boundaries and duplicate cleanup'); + + // A box that scrolls offers its whole content; one that does not, doesn't. + await setup(); + await page.mouse.move(850, 120); + assert.equal( + (await control(labels.scrollArea)).hidden, + false, + 'a card inside a scrolling box offers the whole scrolling area', + ); + await page.mouse.move(130, 130); + assert.equal( + (await control(labels.scrollArea)).hidden, + true, + 'an element with no scrolling box around it does not', + ); + await page.mouse.move(850, 120); + await clickControl(labels.scrollArea); + assert.deepEqual((await result()).value, { kind: 'scroll-area' }); + assert.equal( + await page.evaluate(() => document.querySelector('[data-oss-scroll-target]')?.id), + 'feed', + 'the picker tags the scrolling box for the capture', + ); + // The capture's measurement uses the tagged box, and restoring clears the tag. + const { code: scrollCode } = await transformWithEsbuild( + await readFile(SCROLL_SOURCE, 'utf8'), + SCROLL_SOURCE, + { loader: 'ts', format: 'iife', globalName: '__sc' }, + ); + await page.addScriptTag({ content: scrollCode }); + const measured = await page.evaluate(() => { + const m = globalThis.__sc.getMetrics(true, true); + const tagged = document.querySelector('[data-oss-scroller="1"]')?.id; + globalThis.__sc.restoreCapture(); + return { + tagged, + scrollHeight: m.scrollHeight, + container: m.container, + left: document.querySelectorAll('[data-oss-scroll-target],[data-oss-scroller]').length, + }; + }); + assert.equal(measured.tagged, 'feed', 'getMetrics scrolls the tagged box'); + assert.equal(measured.scrollHeight, 5 * 118 + 8, 'its full content height'); + assert.deepEqual( + measured.container, + { x: 800, y: 80, width: 160, height: 200 }, + 'tiles crop to the box', + ); + assert.equal(measured.left, 0, 'restoreCapture removes both tags'); + // Starting the picker again clears a tag a capture never used. + await page.evaluate(() => + document.querySelector('#feed').setAttribute('data-oss-scroll-target', ''), + ); + await start(); + assert.equal( + await page.evaluate(() => document.querySelectorAll('[data-oss-scroll-target]').length), + 0, + 'a stale scroll target is cleared when the picker opens', + ); + await page.keyboard.press('Escape'); + await result(); + console.log('PASS whole scrolling area: offer, tag, measure, and cleanup'); console.log('Element picker smoke passed.'); } finally { if (browser) await browser.close();