1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 if (!document
.body
.getAttribute('screenshot_extension_injected')) {
7 document
.body
.setAttribute('screenshot_extension_injected', true);
10 // Bounce message from webpage to background page.
12 // Expecting a message called with:
13 // window.postMessage({
14 // id: <a value that is passed back unchanged to the response for
16 // target: 'background'
19 // When the screenshot is captured, a message will be posted to the window.
20 // Listen for it like this:
22 // window.addEventListener('message', function(event) {
23 // if (event.source !== window)
26 // if (event.data.target !== 'page')
29 // // event.data is an object:
31 // // id: <the id passed to the request>,
33 // // data: <a data URI of MIMEtype image/png with the tab screenshot>
36 // // or if there is an error:
39 // // id: <the id passed to the request>,
41 // // error: <an error string>
45 window
.addEventListener('message', function(event
) {
46 if (event
.source
!== window
)
49 // Ignore messages not destined for the background page.
50 if (event
.data
.target
!== 'background')
53 var id
= event
.data
.id
;
54 console
.log('sending message: id=' + id
);
56 chrome
.runtime
.sendMessage(null, {},
57 function(responseData
) {
58 // Bounce response from background page back to webpage.
59 var lastError
= chrome
.runtime
.lastError
;
61 console
.log('lastError: ' + lastError
);
63 window
.postMessage({id
: id
, target
: 'page', error
: lastError
},
68 console
.log('received response: id=' + id
);
70 window
.postMessage({id
: id
, target
: 'page', data
: responseData
},