| OLD | NEW |
| 1 <script> | 1 <script> |
| 2 window.onload = function() { | 2 window.onload = function() { |
| 3 chrome.extension.onConnect.addListener(function(port) { | 3 chrome.extension.onConnect.addListener(function(port) { |
| 4 console.log('onConnect'); | 4 console.log('onConnect'); |
| 5 port.onMessage.addListener(function(msg) { | 5 port.onMessage.addListener(function(msg) { |
| 6 console.log('got message'); | 6 console.log('got message'); |
| 7 if (msg.testPostMessageFromTab) { | 7 if (msg.testPostMessageFromTab) { |
| 8 port.postMessage({success: true}); | 8 port.postMessage({success: true, portName: port.name}); |
| 9 console.log('sent success'); | 9 console.log('sent success'); |
| 10 } | 10 } |
| 11 // Ignore other messages since they are from us. | 11 // Ignore other messages since they are from us. |
| 12 }); | 12 }); |
| 13 }); | 13 }); |
| 14 }; | 14 }; |
| 15 | 15 |
| 16 // Tests that postMessage to the tab and its response works. | 16 // Tests that postMessage to the tab and its response works. |
| 17 function testPostMessage() { | 17 function testPostMessage() { |
| 18 chrome.tabs.getSelected(null, function(tab) { | 18 chrome.tabs.getSelected(null, function(tab) { |
| 19 var port = chrome.tabs.connect(tab.id); | 19 var port = chrome.tabs.connect(tab.id); |
| 20 console.log('connect to ' + tab.id); | 20 console.log('connect to ' + tab.id); |
| 21 port.postMessage({testPostMessage: true}); | 21 port.postMessage({testPostMessage: true}); |
| 22 port.onMessage.addListener(function(msg) { | 22 port.onMessage.addListener(function(msg) { |
| 23 window.domAutomationController.send(msg.success); | 23 window.domAutomationController.send(msg.success); |
| 24 port.disconnect(); | 24 port.disconnect(); |
| 25 }); | 25 }); |
| 26 }); | 26 }); |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Tests that port name is sent & received correctly. |
| 30 function testPortName() { |
| 31 chrome.tabs.getSelected(null, function(tab) { |
| 32 var portName = "lemonjello"; |
| 33 var port = chrome.tabs.connect(tab.id, {name: portName}); |
| 34 console.log('naming port ' + portName); |
| 35 port.postMessage({testPortName: true}); |
| 36 port.onMessage.addListener(function(msg) { |
| 37 console.log('got name ' + msg.portName); |
| 38 window.domAutomationController.send(msg.portName == portName); |
| 39 port.disconnect(); |
| 40 }); |
| 41 }); |
| 42 } |
| 43 |
| 29 // Tests that postMessage from the tab and its response works. | 44 // Tests that postMessage from the tab and its response works. |
| 30 function testPostMessageFromTab() { | 45 function testPostMessageFromTab() { |
| 31 chrome.tabs.getSelected(null, function(tab) { | 46 chrome.tabs.getSelected(null, function(tab) { |
| 32 var port = chrome.tabs.connect(tab.id); | 47 var port = chrome.tabs.connect(tab.id); |
| 33 console.log('connect to ' + tab.id); | 48 console.log('connect to ' + tab.id); |
| 34 port.postMessage({testPostMessageFromTab: true}); | 49 port.postMessage({testPostMessageFromTab: true}); |
| 35 port.onMessage.addListener(function(msg) { | 50 port.onMessage.addListener(function(msg) { |
| 36 window.domAutomationController.send(msg.success); | 51 window.domAutomationController.send(msg.success); |
| 37 port.disconnect(); | 52 port.disconnect(); |
| 38 }); | 53 }); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 56 chrome.tabs.getSelected(null, function(tab) { | 71 chrome.tabs.getSelected(null, function(tab) { |
| 57 var port = chrome.tabs.connect(tab.id); | 72 var port = chrome.tabs.connect(tab.id); |
| 58 console.log('connect to ' + tab.id); | 73 console.log('connect to ' + tab.id); |
| 59 port.postMessage({testDisconnectOnClose: true}); | 74 port.postMessage({testDisconnectOnClose: true}); |
| 60 port.onDisconnect.addListener(function() { | 75 port.onDisconnect.addListener(function() { |
| 61 window.domAutomationController.send(true); | 76 window.domAutomationController.send(true); |
| 62 }); | 77 }); |
| 63 }); | 78 }); |
| 64 } | 79 } |
| 65 </script> | 80 </script> |
| OLD | NEW |