Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 // Only inject once. | |
| 6 if (!document.body.getAttribute('screenshot_extension_injected')) { | |
| 7 document.body.setAttribute('screenshot_extension_injected', true); | |
| 8 (function() { | |
| 9 | |
| 10 // Bounce message from webpage to background page. | |
| 11 // | |
| 12 // Expecting a message called with: | |
| 13 // window.postMessage({ | |
| 14 // id: <a value that is passed back unchanged to the response for | |
| 15 // identification>, | |
| 16 // target: 'background' | |
| 17 // }, '*'); | |
| 18 // | |
| 19 // When the screenshot is captured, a message will be posted to the window. | |
| 20 // Listen for it like this: | |
| 21 // | |
| 22 // window.addEventListener('message', function(event) { | |
| 23 // if (event.source !== window) | |
| 24 // return; | |
| 25 // | |
| 26 // if (event.data.target !== 'page') | |
| 27 // return; | |
| 28 // | |
| 29 // // event.data is an object: | |
| 30 // // { | |
| 31 // // id: <the id passed to the request>, | |
| 32 // // target: 'page', | |
| 33 // // data: <a data URI of MIMEtype image/png with the tab screenshot> | |
| 34 // // } | |
| 35 // // | |
| 36 // // or if there is an error: | |
| 37 // // | |
| 38 // // { | |
| 39 // // id: <the id passed to the request>, | |
| 40 // // target: 'page', | |
| 41 // // error: <an error string> | |
| 42 // // } | |
| 43 // }, false); | |
| 44 // | |
| 45 window.addEventListener('message', function(event) { | |
| 46 if (event.source !== window) | |
| 47 return; | |
| 48 | |
| 49 // Ignore messages coming from the background page. | |
| 50 if (event.data.target !== 'background') | |
|
Sam Clegg
2013/09/05 00:05:31
There seems to be mismatch between the commend and
binji
2013/09/06 20:59:48
Done.
| |
| 51 return; | |
| 52 | |
| 53 var id = event.data.id; | |
| 54 console.log('sending message: id=' + id); | |
| 55 | |
| 56 chrome.runtime.sendMessage(null, {}, | |
| 57 function(responseData) { | |
| 58 // Bounce response from background page back to webpage. | |
| 59 var lastError = chrome.runtime.lastError; | |
| 60 if (lastError) { | |
| 61 console.log('lastError: ' + lastError); | |
| 62 | |
| 63 window.postMessage({id: id, target: 'page', error: lastError}, | |
| 64 '*'); | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 console.log('received response: id=' + id); | |
| 69 | |
| 70 window.postMessage({id: id, target: 'page', data: responseData}, | |
| 71 '*'); | |
| 72 }); | |
| 73 }, false); | |
| 74 })(); | |
| 75 } | |
| OLD | NEW |