我知道這裏有captureVisibleTab,但是如何剪切製表符的結果截圖,以便只剩下一個HTML元素?如何在Chrome擴展中捕獲單個HTML元素的屏幕截圖?
5
A
回答
4
爲此,您需要onMessage,sendMessage和captureVisibleTab。 onMessage
是chrome.runtime,sendMessage
和captureVisibleTab
的方法都是tabs的方法。
清單
在manifest文件中,您必須對tabs
,並且<all_urls>
權限添加到您的manifest文件。沒有權限,這將無法正常工作。
"permissions": [
"tabs",
"<all_urls>"
],
內容腳本
在你的內容的腳本文件,你需要添加以下。這使您可以與您的背景頁面進行交流,以獲取活動選項卡的屏幕截圖。
chrome.runtime.sendMessage({ chromeAction: "screenshot" }, function (imageString) {
console.log(imageString);
});
背景腳本/頁
這裏就是奇蹟發生。
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.chromeAction === "screenshot") {
createScreenshot(function (dataURL) {
createImage(dataURL);
});
return true;
}
});
// here we create a new image
function createImage(dataURL) {
// create a canvas
var canvas = createCanvas(500, 500);
// get the context of your canvas
var context = canvas.getContext('2d');
// create a new image object
var croppedImage = new Image();
croppedImage.onload = function() {
// this is where you manipulate the screenshot (cropping)
// parameter 1: source image (screenshot)
// parameter 2: source image x coordinate
// parameter 3: source image y coordinate
// parameter 4: source image width
// parameter 5: source image height
// parameter 6: destination x coordinate
// parameter 7: destination y coordinate
// parameter 8: destination width
// parameter 9: destination height
context.drawImage(croppedImage, 10, 10, 300, 300, 0, 0, 250, 250);
// canvas.toDataURL() contains your cropped image
console.log(canvas.toDataURL());
};
croppedImage.src = dataURL; // screenshot (full image)
}
// creates a canvas element
function createCanvas(canvasWidth, canvasHeight) {
var canvas = document.createElement("canvas");
// size of canvas in pixels
canvas.width = canvasWidth;
canvas.height = canvasHeight;
return canvas;
}
// calling the captureVisibleTab method takes a screenhot
function createScreenshot(callback) {
// you can have two image formats (jpeg and png)
// for jpeg use { format: "jpeg", quality: 100 } (you can adjust the jpeg image quality from 0-100)
// for png use { format: "png" }
chrome.tabs.captureVisibleTab(null, { format: "png" }, callback);
}
裁剪
爲了更好地理解drawImage
帆布方法的閱讀完整documentation。
+0
@BrunoLM下面是一些讓你開始的代碼。 – Daniel
相關問題
- 1. 捕獲HorizontalScrollView的屏幕截圖,包括離屏元素
- 2. Google Chrome擴展程序(如Aviary)如何捕獲屏幕(網頁)?
- 3. 捕獲屏幕截圖
- 4. 捕獲MKMapView屏幕截圖
- 5. ASP.NET - 捕獲屏幕截圖
- 6. 捕獲屏幕截圖
- 7. Chrome屏幕外標籤捕獲API和擴展白名單
- 8. 在硒中捕獲屏幕截圖
- 9. 使用ScreenCapture捕獲屏幕截圖並捕獲屏幕截圖.CaptureScreenshot
- 10. 如何在Chrome插件中本地保存捕獲的屏幕截圖?
- 11. 開發屏幕截圖鍍鉻擴展
- 12. Chrome瀏覽器擴展程序 - 在新標籤頁中打開捕獲的屏幕截圖
- 13. 捕獲屏幕截圖的iframe
- 14. Android如何在屏幕截圖中捕獲陰影
- 15. 如何調用Chrome擴展(Full Page Screen Capture)或360browser的屏幕截圖
- 16. 從Chrome擴展打開的窗口在活動面板中的捕獲屏幕
- 17. 捕獲屏幕截圖 - html2canvas不工作
- 18. 用ImageMagick ++捕獲X11屏幕截圖
- 19. 如何在Xcode屏幕上捕獲指定的區域屏幕截圖
- 20. 擷取元素的屏幕截圖
- 21. 使用Chrome擴展程序獲取完整頁面屏幕截圖
- 22. 在iPhone中捕捉屏幕截圖?
- 23. 如何使用Selenium在Chrome中捕捉完整的屏幕截圖?
- 24. 如何在iPhone上使用UI自動捕獲屏幕截圖?
- 25. 在Chrome擴展中反覆捕獲DOM
- 26. 多個屏幕截圖捕獲相同的圖像
- 27. 如何自動捕獲iOS應用程序的屏幕截圖
- 28. 如何使用phantomcss捕獲iframe內組件的屏幕截圖
- 29. 如何使用objective-c捕獲iOS屏幕截圖的子集?
- 30. 如何捕獲轉換的屏幕截圖
使用內容腳本獲取元素的相對位置和尺寸,使用此信息在畫布上繪製屏幕截圖,然後從畫布中讀取最終(裁剪後)的圖像。 –
@RobW,謝謝,我沒有考慮使用畫布。我會接受你的評論作爲答案,如果這是可能的! :) – spektom
編寫我的評論的實施,發佈它作爲答案並接受它。編寫正確的實現可能需要5-20分鐘(請記住,captureVisibleTab只捕獲標籤的可見部分,而不是整個頁面,因此您可能需要滾動到正確的位置)。 –