Home | History | Annotate | Download | only in screenshot
      1 // To make sure we can uniquely identify each screenshot tab, add an id as a
      2 // query param to the url that displays the screenshot.
      3 var id = 100;
      4 
      5 function takeScreenshot() {
      6   chrome.tabs.captureVisibleTab(null, function(img) {
      7     var screenshotUrl = img;
      8     var viewTabUrl = [chrome.extension.getURL('screenshot.html'),
      9                       '?id=', id++].join('');
     10 
     11     chrome.tabs.create({url: viewTabUrl}, function(tab) {
     12       var targetId = tab.id;
     13 
     14       var addSnapshotImageToTab = function(tabId, changedProps) {
     15         // We are waiting for the tab we opened to finish loading.
     16         // Check that the the tab's id matches the tab we opened,
     17         // and that the tab is done loading.
     18         if (tabId != targetId || changedProps.status != "complete")
     19           return;
     20 
     21         // Passing the above test means this is the event we were waiting for.
     22         // There is nothing we need to do for future onUpdated events, so we
     23         // use removeListner to stop geting called when onUpdated events fire.
     24         chrome.tabs.onUpdated.removeListener(addSnapshotImageToTab);
     25 
     26         // Look through all views to find the window which will display
     27         // the screenshot.  The url of the tab which will display the
     28         // screenshot includes a query parameter with a unique id, which
     29         // ensures that exactly one view will have the matching URL.
     30         var views = chrome.extension.getViews();
     31         for (var i = 0; i < views.length; i++) {
     32           var view = views[i];
     33           if (view.location.href == viewTabUrl) {
     34             view.setScreenshotUrl(screenshotUrl);
     35             break;
     36           }
     37         }
     38       };
     39       chrome.tabs.onUpdated.addListener(addSnapshotImageToTab);
     40 
     41     });
     42   });
     43 }
     44 
     45 // Listen for a click on the camera icon.  On that click, take a screenshot.
     46 chrome.browserAction.onClicked.addListener(function(tab) {
     47   takeScreenshot();
     48 });
     49