| Index: chrome/common/extensions/docs/examples/api/tabs/screenshot/background.js
|
| diff --git a/chrome/common/extensions/docs/examples/api/tabs/screenshot/background.js b/chrome/common/extensions/docs/examples/api/tabs/screenshot/background.js
|
| index 4124857248261f40e234d7c8f49e94bfc92cd0b6..7f9b7bceb14a450f7304ffcecd8eae246c0f902b 100644
|
| --- a/chrome/common/extensions/docs/examples/api/tabs/screenshot/background.js
|
| +++ b/chrome/common/extensions/docs/examples/api/tabs/screenshot/background.js
|
| @@ -9,49 +9,41 @@
|
| // open.
|
| var id = 100;
|
|
|
| -function takeScreenshot() {
|
| - chrome.tabs.captureVisibleTab(null, function(img) {
|
| - var screenshotUrl = img;
|
| - var viewTabUrl = chrome.extension.getURL('screenshot.html?id=' + id++)
|
| +// Listen for a click on the camera icon. On that click, take a screenshot.
|
| +chrome.browserAction.onClicked.addListener(function() {
|
|
|
| - chrome.tabs.create({url: viewTabUrl}, function(tab) {
|
| - var targetId = tab.id;
|
| + chrome.tabs.captureVisibleTab(function(screenshotUrl) {
|
| + var viewTabUrl = chrome.extension.getURL('screenshot.html?id=' + id++)
|
| + var targetId = null;
|
|
|
| - var addSnapshotImageToTab = function(tabId, changedProps) {
|
| - // We are waiting for the tab we opened to finish loading.
|
| - // Check that the the tab's id matches the tab we opened,
|
| - // and that the tab is done loading.
|
| - if (tabId != targetId || changedProps.status != "complete")
|
| - return;
|
| + chrome.tabs.onUpdated.addListener(function listener(tabId, changedProps) {
|
| + // We are waiting for the tab we opened to finish loading.
|
| + // Check that the tab's id matches the tab we opened,
|
| + // and that the tab is done loading.
|
| + if (tabId != targetId || changedProps.status != "complete")
|
| + return;
|
|
|
| - // 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
|
| - // use removeListner to stop geting called when onUpdated events fire.
|
| - chrome.tabs.onUpdated.removeListener(addSnapshotImageToTab);
|
| + // 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
|
| + // use removeListner to stop getting called when onUpdated events fire.
|
| + chrome.tabs.onUpdated.removeListener(listener);
|
|
|
| - // Look through all views to find the window which will display
|
| - // the screenshot. The url of the tab which will display the
|
| - // screenshot includes a query parameter with a unique id, which
|
| - // ensures that exactly one view will have the matching URL.
|
| - var views = chrome.extension.getViews();
|
| - for (var i = 0; i < views.length; i++) {
|
| - var view = views[i];
|
| - if (view.location.href == viewTabUrl) {
|
| - view.setScreenshotUrl(screenshotUrl);
|
| - break;
|
| - }
|
| + // Look through all views to find the window which will display
|
| + // the screenshot. The url of the tab which will display the
|
| + // screenshot includes a query parameter with a unique id, which
|
| + // ensures that exactly one view will have the matching URL.
|
| + var views = chrome.extension.getViews();
|
| + for (var i = 0; i < views.length; i++) {
|
| + var view = views[i];
|
| + if (view.location.href == viewTabUrl) {
|
| + view.setScreenshotUrl(screenshotUrl);
|
| + break;
|
| }
|
| - };
|
| - chrome.tabs.onUpdated.addListener(addSnapshotImageToTab);
|
| + }
|
| });
|
| - });
|
| -}
|
|
|
| -// Listen for a click on the camera icon. On that click, take a screenshot.
|
| -chrome.browserAction.onClicked.addListener(function(tab) {
|
| - if (tab.url.match("^https?://code.google.com")) {
|
| - takeScreenshot();
|
| - } else {
|
| - alert('This sample can only take screenshots of code.google.com pages');
|
| - }
|
| + chrome.tabs.create({url: viewTabUrl}, function(tab) {
|
| + targetId = tab.id;
|
| + });
|
| + });
|
| });
|
|
|