OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 function setScreenshotUrl(url) { |
6 // query param to the url that displays the screenshot. | 6 document.getElementById('target').src = url; |
7 var id = 100; | |
8 | |
9 function takeScreenshot() { | |
10 chrome.tabs.captureVisibleTab(null, function(img) { | |
11 var screenshotUrl = img; | |
12 var viewTabUrl = [chrome.extension.getURL('screenshot.html'), | |
13 '?id=', id++].join(''); | |
14 | |
15 chrome.tabs.create({url: viewTabUrl}, function(tab) { | |
16 var targetId = tab.id; | |
17 | |
18 var addSnapshotImageToTab = function(tabId, changedProps) { | |
19 // We are waiting for the tab we opened to finish loading. | |
20 // Check that the the tab's id matches the tab we opened, | |
21 // and that the tab is done loading. | |
22 if (tabId != targetId || changedProps.status != "complete") | |
23 return; | |
24 | |
25 // Passing the above test means this is the event we were waiting for. | |
26 // There is nothing we need to do for future onUpdated events, so we | |
27 // use removeListner to stop geting called when onUpdated events fire. | |
28 chrome.tabs.onUpdated.removeListener(addSnapshotImageToTab); | |
29 | |
30 // Look through all views to find the window which will display | |
31 // the screenshot. The url of the tab which will display the | |
32 // screenshot includes a query parameter with a unique id, which | |
33 // ensures that exactly one view will have the matching URL. | |
34 var views = chrome.extension.getViews(); | |
35 for (var i = 0; i < views.length; i++) { | |
36 var view = views[i]; | |
37 if (view.location.href == viewTabUrl) { | |
38 view.setScreenshotUrl(screenshotUrl); | |
39 break; | |
40 } | |
41 } | |
42 }; | |
43 chrome.tabs.onUpdated.addListener(addSnapshotImageToTab); | |
44 | |
45 }); | |
46 }); | |
47 } | 7 } |
48 | |
49 // Listen for a click on the camera icon. On that click, take a screenshot. | |
50 chrome.browserAction.onClicked.addListener(function(tab) { | |
51 if (tab.url.match(/code.google.com/)) { | |
52 takeScreenshot(); | |
53 } else { | |
54 alert('This sample can only take screenshots of code.google.com pages'); | |
55 } | |
56 }); | |
OLD | NEW |