Next, I'll modify popup/popup.js to handle the new button's functionality and related UI updates. This includes adding an event listener for the "滚动截屏" button, sending a message to the background script to initiate the scrolling screenshot, and ensuring the button text updates to "截取中..." when clicked. I'll also assume that your getText and updateI18nTexts functions in utils/i18n.js will be updated to include the new internationalization keys like popup_scrollScreenshot: "滚动截屏" and capturing: "截取中...".
qrSuccess: "QR Code decodificado com sucesso, copiado para a área de transferência",
qrNoQRFound: "Nenhum QR Code válido detectado",
- qrError: "Falha ao decodificar QR Code: {0}"
+ qrError: "Falha ao decodificar QR Code: {0}",
+ processingScrollCapture: "Processando captura de tela com rolagem...", // 新增
+ processingScrollCaptureProgress: "{0} partes capturadas...", // 新增
+ scrollCaptureSaved: "Captura de tela com rolagem salva!", // 新增
+ scrollCaptureFailed: "Falha na captura de tela com rolagem: {0}" // 新增
},
ja: {
qrSuccess: "QR Code decodificado com sucesso, copiado para a área de transferência",
qrNoQRFound: "Nenhum QR Code válido detectado",
- qrError: "Falha ao decodificar QR Code: {0}"
+ qrError: "Falha ao decodificar QR Code: {0}",
+ processingScrollCapture: "Processando captura de tela com rolagem...", // 新增
+ processingScrollCaptureProgress: "{0} partes capturadas...", // 新增
+ scrollCaptureSaved: "Captura de tela com rolagem salva!", // 新增
+ scrollCaptureFailed: "Falha na captura de tela com rolagem: {0}" // 新增
},
ja: {
The scrolling screenshot feature has now been implemented across popup/popup.html, popup/popup.js, content/content.js, and background/background.js.
Summary of Changes:
Popup Interface: A "滚动截屏" (Scrolling Screenshot) button has been added to the popup. Clicking it initiates the scrolling capture process.
Content Script (content.js):
Handles new messages from the background script to display notifications about the scrolling capture progress (e.g., "正在滚动截取长图...", "已截取 N 部分..."), success ("长截图已保存!"), or failure ("长截图失败: {error}").
Appropriate i18n strings have been added.
Background Script (background/background.js):
The core logic for scrolling capture resides here.
It listens for the initiateScrollingScreenshotFromPopup message.
initiateScrollingScreenshot: Initializes the process by getting page dimensions, scrolling to the top.
scrollAndCaptureNextIteration: Recursively scrolls the page down by one viewport height (minus a small overlap), captures the visible area using chrome.tabs.captureVisibleTab, and repeats until the bottom of the page or a maximum scroll limit is reached.
captureCurrentViewForScrolling: Captures the current view and stores the dataUrl along with its scrollY position.
finalizeScrollingCapture: Once all parts are captured, it sorts the collected images by their scroll position, calculates the total dimensions, and uses an OffscreenCanvas to stitch them together into a single PNG image. The dpr (device pixel ratio) is considered for correct scaling. The final image is then saved using the existing saveScreenshot function.
Helper functions createImageBitmapFromUrl (to convert dataURLs to ImageBitmaps for canvas drawing) and restoreScrollPosition (to return the page to its original scroll state) have been added.
Error handling and notifications to the content script are included throughout the process.
Important Considerations and Potential Next Steps:
Fixed/Sticky Elements: As discussed, fixed or sticky headers/footers will currently be repeated in each captured segment. Addressing this would require more advanced techniques like attempting to hide them via script injection before each capture or post-processing the image to remove them.
Infinite Scrolling Pages: The current implementation has a maxScrolls limit to prevent getting stuck. True intelligent detection for "load more" buttons or infinite scroll mechanisms would be a significant enhancement.
Dynamic Content/Layout Shifts: Pages where content loads or shifts significantly during scrolling might lead to stitching artifacts. The delay between scroll and capture helps, but complex cases might still be problematic.
Performance for Very Long Pages: Capturing and stitching a very large number of images can be memory and CPU intensive. The OffscreenCanvas helps, but extremely long pages could still pose challenges.
User Options: You might consider adding options for:
Overlap amount.
Delay between scrolls.
Maximum height/number of scrolls.
Testing: Thorough testing on various websites with different layouts, scrolling behaviors, and fixed elements is crucial.
You should now be able to test the implemented scrolling screenshot functionality. Remember to update your utils/i18n.js file with the new translation keys mentioned in the code for popup.js and content.js to ensure all text displays correctly in different languages.