Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // To make sure we can uniquely identify each screenshot tab, add an id as a | 5 // To make sure we can uniquely identify each screenshot tab, add an id as a |
| 6 // query param to the url that displays the screenshot. | 6 // query param to the url that displays the screenshot. |
| 7 // Note: It's OK that this is a global variable (and not in localStorage), | 7 // Note: It's OK that this is a global variable (and not in localStorage), |
| 8 // because the event page will stay open as long as any screenshot tabs are | 8 // because the event page will stay open as long as any screenshot tabs are |
| 9 // open. | 9 // open. |
| 10 var id = 100; | 10 var id = 100; |
| 11 | 11 |
| 12 function takeScreenshot() { | 12 // Listen for a click on the camera icon. On that click, take a screenshot. |
| 13 chrome.tabs.captureVisibleTab(null, function(img) { | 13 chrome.browserAction.onClicked.addListener(function() { |
| 14 | |
| 15 chrome.tabs.captureVisibleTab(function(img) { | |
| 14 var screenshotUrl = img; | 16 var screenshotUrl = img; |
| 15 var viewTabUrl = chrome.extension.getURL('screenshot.html?id=' + id++) | 17 var viewTabUrl = chrome.extension.getURL('screenshot.html?id=' + id++) |
| 18 var targetId = null; | |
| 16 | 19 |
| 20 var addSnapshotImageToTab = function(tabId, changedProps) { | |
| 21 // We are waiting for the tab we opened to finish loading. | |
| 22 // Check that the tab's id matches the tab we opened, | |
| 23 // and that the tab is done loading. | |
| 24 if (tabId != targetId || changedProps.status != "complete") | |
| 25 return; | |
| 26 | |
| 27 // Passing the above test means this is the event we were waiting for. | |
| 28 // There is nothing we need to do for future onUpdated events, so we | |
| 29 // use removeListner to stop getting called when onUpdated events fire. | |
| 30 chrome.tabs.onUpdated.removeListener(addSnapshotImageToTab); | |
| 31 | |
| 32 // Look through all views to find the window which will display | |
| 33 // the screenshot. The url of the tab which will display the | |
| 34 // screenshot includes a query parameter with a unique id, which | |
| 35 // ensures that exactly one view will have the matching URL. | |
| 36 var views = chrome.extension.getViews(); | |
| 37 for (var i = 0; i < views.length; i++) { | |
| 38 var view = views[i]; | |
| 39 if (view.location.href == viewTabUrl) { | |
| 40 view.setScreenshotUrl(screenshotUrl); | |
| 41 break; | |
| 42 } | |
| 43 } | |
| 44 }; | |
| 45 | |
| 46 chrome.tabs.onUpdated.addListener(addSnapshotImageToTab); | |
|
not at google - send to devlin
2014/08/22 14:20:51
Feel free not to do this, but I've seen a nice pat
François Beaufort
2014/08/25 07:35:57
I love this pattern.
Thanks!
| |
| 17 chrome.tabs.create({url: viewTabUrl}, function(tab) { | 47 chrome.tabs.create({url: viewTabUrl}, function(tab) { |
|
not at google - send to devlin
2014/08/22 14:20:51
And also - you can avoid |targetId| if you nest th
François Beaufort
2014/08/25 07:35:57
Sadly, adding the listener chrome.tabs.onUpdated a
not at google - send to devlin
2014/08/25 16:32:06
Ah ok. Bummer. I can see why.
| |
| 18 var targetId = tab.id; | 48 targetId = tab.id; |
| 19 | |
| 20 var addSnapshotImageToTab = function(tabId, changedProps) { | |
| 21 // We are waiting for the tab we opened to finish loading. | |
| 22 // Check that the the tab's id matches the tab we opened, | |
| 23 // and that the tab is done loading. | |
| 24 if (tabId != targetId || changedProps.status != "complete") | |
| 25 return; | |
| 26 | |
| 27 // Passing the above test means this is the event we were waiting for. | |
| 28 // There is nothing we need to do for future onUpdated events, so we | |
| 29 // use removeListner to stop geting called when onUpdated events fire. | |
| 30 chrome.tabs.onUpdated.removeListener(addSnapshotImageToTab); | |
| 31 | |
| 32 // Look through all views to find the window which will display | |
| 33 // the screenshot. The url of the tab which will display the | |
| 34 // screenshot includes a query parameter with a unique id, which | |
| 35 // ensures that exactly one view will have the matching URL. | |
| 36 var views = chrome.extension.getViews(); | |
| 37 for (var i = 0; i < views.length; i++) { | |
| 38 var view = views[i]; | |
| 39 if (view.location.href == viewTabUrl) { | |
| 40 view.setScreenshotUrl(screenshotUrl); | |
| 41 break; | |
| 42 } | |
| 43 } | |
| 44 }; | |
| 45 chrome.tabs.onUpdated.addListener(addSnapshotImageToTab); | |
| 46 }); | 49 }); |
| 47 }); | 50 }); |
| 48 } | |
| 49 | |
| 50 // Listen for a click on the camera icon. On that click, take a screenshot. | |
| 51 chrome.browserAction.onClicked.addListener(function(tab) { | |
| 52 if (tab.url.match("^https?://code.google.com")) { | |
| 53 takeScreenshot(); | |
| 54 } else { | |
| 55 alert('This sample can only take screenshots of code.google.com pages'); | |
| 56 } | |
| 57 }); | 51 }); |
| OLD | NEW |