Commit 29cb00fd authored by fbeaufort's avatar fbeaufort Committed by Commit bot

Fixed Screenshot extension example

BUG=

Review URL: https://codereview.chromium.org/490133002

Cr-Commit-Position: refs/heads/master@{#291874}
parent f44ecee4
...@@ -9,49 +9,41 @@ ...@@ -9,49 +9,41 @@
// open. // open.
var id = 100; var id = 100;
function takeScreenshot() { // Listen for a click on the camera icon. On that click, take a screenshot.
chrome.tabs.captureVisibleTab(null, function(img) { chrome.browserAction.onClicked.addListener(function() {
var screenshotUrl = img;
var viewTabUrl = chrome.extension.getURL('screenshot.html?id=' + id++)
chrome.tabs.create({url: viewTabUrl}, function(tab) { chrome.tabs.captureVisibleTab(function(screenshotUrl) {
var targetId = tab.id; var viewTabUrl = chrome.extension.getURL('screenshot.html?id=' + id++)
var targetId = null;
var addSnapshotImageToTab = function(tabId, changedProps) { chrome.tabs.onUpdated.addListener(function listener(tabId, changedProps) {
// We are waiting for the tab we opened to finish loading. // We are waiting for the tab we opened to finish loading.
// Check that the the tab's id matches the tab we opened, // Check that the tab's id matches the tab we opened,
// and that the tab is done loading. // and that the tab is done loading.
if (tabId != targetId || changedProps.status != "complete") if (tabId != targetId || changedProps.status != "complete")
return; return;
// Passing the above test means this is the event we were waiting for. // Passing the above test means this is the event we were waiting for.
// There is nothing we need to do for future onUpdated events, so we // There is nothing we need to do for future onUpdated events, so we
// use removeListner to stop geting called when onUpdated events fire. // use removeListner to stop getting called when onUpdated events fire.
chrome.tabs.onUpdated.removeListener(addSnapshotImageToTab); chrome.tabs.onUpdated.removeListener(listener);
// Look through all views to find the window which will display // Look through all views to find the window which will display
// the screenshot. The url of the tab which will display the // the screenshot. The url of the tab which will display the
// screenshot includes a query parameter with a unique id, which // screenshot includes a query parameter with a unique id, which
// ensures that exactly one view will have the matching URL. // ensures that exactly one view will have the matching URL.
var views = chrome.extension.getViews(); var views = chrome.extension.getViews();
for (var i = 0; i < views.length; i++) { for (var i = 0; i < views.length; i++) {
var view = views[i]; var view = views[i];
if (view.location.href == viewTabUrl) { if (view.location.href == viewTabUrl) {
view.setScreenshotUrl(screenshotUrl); view.setScreenshotUrl(screenshotUrl);
break; break;
}
} }
}; }
chrome.tabs.onUpdated.addListener(addSnapshotImageToTab);
}); });
});
}
// Listen for a click on the camera icon. On that click, take a screenshot. chrome.tabs.create({url: viewTabUrl}, function(tab) {
chrome.browserAction.onClicked.addListener(function(tab) { targetId = tab.id;
if (tab.url.match("^https?://code.google.com")) { });
takeScreenshot(); });
} else {
alert('This sample can only take screenshots of code.google.com pages');
}
}); });
{ {
"name": "Test Screenshot Extension", "name": "Test Screenshot Extension",
"version": "1.2", "version": "1.3",
"description": "Demonstrate screenshot functionality in the chrome.tabs api. Note: only works for code.google.com", "description": "Demonstrate screenshot functionality in the chrome.tabs api.",
"background": { "background": {
"persistent": false, "persistent": false,
"scripts": ["background.js"] "scripts": ["background.js"]
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
"default_title": "Take a screen shot!" "default_title": "Take a screen shot!"
}, },
"permissions": [ "permissions": [
"*://code.google.com/" "activeTab"
], ],
"manifest_version": 2 "manifest_version": 2
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<body> <body>
Image here: Image here:
<p> <p>
<img id="target" src="white.png" width="640" height="480"> <img id="target" src="white.png" height="480">
<p> <p>
End image End image
</body> </body>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment