| OLD | NEW |
| 1 var win = window; | 1 var win = window; |
| 2 if (typeof(contentWindow) != 'undefined') { | 2 if (typeof(contentWindow) != 'undefined') { |
| 3 win = contentWindow; | 3 win = contentWindow; |
| 4 } | 4 } |
| 5 | 5 |
| 6 win.onload = function() { | 6 win.onload = function() { |
| 7 // Do this in an onload handler because I'm not sure if chrome.extension | 7 // Do this in an onload handler because I'm not sure if chrome.extension |
| 8 // is available before then. | 8 // is available before then. |
| 9 chrome.extension.onConnect.addListener(function(port) { | 9 chrome.extension.onConnect.addListener(function(port) { |
| 10 console.log('connected'); | 10 console.log('connected'); |
| 11 port.onMessage.addListener(function(msg) { | 11 port.onMessage.addListener(function(msg) { |
| 12 console.log('got ' + msg); | 12 console.log('got ' + msg); |
| 13 if (msg.testPostMessage) { | 13 if (msg.testPostMessage) { |
| 14 port.postMessage({success: true}); | 14 port.postMessage({success: true}); |
| 15 } else if (msg.testPostMessageFromTab) { | 15 } else if (msg.testPostMessageFromTab) { |
| 16 testPostMessageFromTab(port); | 16 testPostMessageFromTab(port); |
| 17 } else if (msg.testDisconnect) { | 17 } else if (msg.testDisconnect) { |
| 18 port.disconnect(); | 18 port.disconnect(); |
| 19 } else if (msg.testDisconnectOnClose) { | 19 } else if (msg.testDisconnectOnClose) { |
| 20 win.location = "about:blank"; | 20 win.location = "about:blank"; |
| 21 } else if (msg.testPortName) { |
| 22 port.postMessage({portName:port.name}); |
| 21 } | 23 } |
| 22 // Ignore other messages since they are from us. | 24 // Ignore other messages since they are from us. |
| 23 }); | 25 }); |
| 24 }); | 26 }); |
| 25 }; | 27 }; |
| 26 | 28 |
| 27 // Tests that postMessage to the extension and its response works. | 29 // Tests that postMessage to the extension and its response works. |
| 28 function testPostMessageFromTab(origPort) { | 30 function testPostMessageFromTab(origPort) { |
| 29 console.log('testPostMessageFromTab'); | 31 console.log('testPostMessageFromTab'); |
| 30 var port = chrome.extension.connect(); | 32 var portName = "peter"; |
| 33 var port = chrome.extension.connect({name: portName}); |
| 31 port.postMessage({testPostMessageFromTab: true}); | 34 port.postMessage({testPostMessageFromTab: true}); |
| 32 port.onMessage.addListener(function(msg) { | 35 port.onMessage.addListener(function(msg) { |
| 33 origPort.postMessage({success: msg.success}); | 36 origPort.postMessage({success: (msg.success && (msg.portName == portName))})
; |
| 34 console.log('sent ' + msg.success); | 37 console.log('sent ' + msg.success); |
| 35 port.disconnect(); | 38 port.disconnect(); |
| 36 }); | 39 }); |
| 37 console.log('posted message'); | 40 console.log('posted message'); |
| 38 } | 41 } |
| 39 | 42 |
| 40 // Workaround two bugs: shutdown crash if we hook 'unload', and content script | 43 // Workaround two bugs: shutdown crash if we hook 'unload', and content script |
| 41 // GC if we don't register any event handlers. | 44 // GC if we don't register any event handlers. |
| 42 // http://code.google.com/p/chromium/issues/detail?id=17410 | 45 // http://code.google.com/p/chromium/issues/detail?id=17410 |
| 43 // http://code.google.com/p/chromium/issues/detail?id=17582 | 46 // http://code.google.com/p/chromium/issues/detail?id=17582 |
| 44 function foo() {} | 47 function foo() {} |
| 45 win.addEventListener('error', foo); | 48 win.addEventListener('error', foo); |
| OLD | NEW |