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