I've got a problem: the chrome.experimental.offscreenTabs.create
worked well, but the toDataUrl
method produced an image with a height of 1 pixel. I've tried my best, but the image produced by toDataUrl
does not show the size as I specified. How can this problem be solved?
Here is my code:
chrome.experimental.offscreenTabs.create({ url: "http:/www.baidu.com" }, function(offscreenTab) {
// console.log(offscreenTab);
chrome.experimental.offscreenTabs.toDataUrl(offscreenTab.id, { format: "png" }, function(imgUrl) {
$("#container").html("<img src='" + imgUrl + "' />");
});
});
The
offscreenTabs
API is experimental. The solution below has successfully been tested in Chromium 20.0.1132.57 (Linux), but that doesn't mean that the code still works in later versions.This answer is a follow-up to How to use the experimental offscreenTab API?
The callback function of
chrome.experimental.offscreenTabs.create
is called when the tab is created. Tests using thechrome.webRequest
API and the UNIX netcat command showed that the callback can be fired before the server responds. Hence, it's unlikely that a callback is triggered after the page is rendered.My demo extension consists of 5 files. Their role is briefly explained:
manifest.json
- The required glue for the extension (see documentation).sayhello.js
- A content script which notifies the background page. This helper is necessary, because thechrome.tabs
andchrome.webRequest
are useless: The event listeners ofchrome.tabs
are never triggered for offscreen tabs, and the event listeners ofchrome.webRequest
are triggered before the page is rendered.background.js
- A background page to receive the message from the content script.chrome.extension.getViews()
is used to locate the view which launches the offscreen tab.options.html
- The visible view which launches offscreen tabs. (The background page cannot launch an offscreen tab).options.js
- When manifest version 2 is active, inline scripts are not executed. The script must be placed in an external file, and loaded using<script src>
.I've uploaded the extension to http://rob.lekensteyn.nl/offscreentabs.zip same file, crx extension: CRX. Don't forget to enable the experimental permission at
chrome://flags
, if you want to test it.manifest.json
sayhello.js
background.js
options.html
options.js