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