How to capture a VisibleTab and save it in a server side png file?

In the Google Chrome extension, how can I grab a VisibleTab and save it in a server side png file?

+4
source share
1 answer

Here is a simple example showing how you can do this:

manifest.json:

{ "name": "TabCapture", "version": "0.0.1", "description": "Capture a tab", "background_page" : "background.html", "browser_action": { "default_icon": "icon.png", "default_title": "Capture tab" }, "permissions" : ["tabs", "<all_urls>"] } 

background.html:

 <!DOCTYPE html> <html> <script type="text/javascript" src="background.js"></script> </html> 

background.js:

  chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.captureVisibleTab(null, function(img) { var xhr = new XMLHttpRequest(), formData = new FormData(); formData.append("img", img); xhr.open("POST", "http://myserver.com/submitImage", true); xhr.send(formData); }); }); 

This extension adds a browser button to Chrome. When the user clicks the button, the POST request containing the base64 encoded image (in the FormData object ) is sent to http://myserver.com/submitImage .

This code does not show how to manage server errors and response.

+10
source

Source: https://habr.com/ru/post/1389280/


All Articles