1 // Copyright (c) 2011 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.
6 if (typeof(contentWindow
) != 'undefined') {
10 chrome
.runtime
.onConnect
.addListener(function(port
) {
11 console
.log('connected');
12 port
.onMessage
.addListener(function(msg
) {
13 console
.log('got ' + msg
);
14 if (msg
.testPostMessage
) {
15 port
.postMessage({success
: true});
16 } else if (msg
.testPostMessageFromTab
) {
17 testPostMessageFromTab(port
);
18 } else if (msg
.testDisconnect
) {
20 } else if (msg
.testDisconnectOnClose
) {
21 win
.location
= "about:blank";
22 } else if (msg
.testPortName
) {
23 port
.postMessage({portName
:port
.name
});
25 // Ignore other messages since they are from us.
29 // Tests that postMessage to the extension and its response works.
30 function testPostMessageFromTab(origPort
) {
31 console
.log('testPostMessageFromTab');
32 var portName
= "peter";
33 var port
= chrome
.runtime
.connect({name
: portName
});
34 port
.postMessage({testPostMessageFromTab
: true});
35 port
.onMessage
.addListener(function(msg
) {
36 origPort
.postMessage({success
: (msg
.success
&& (msg
.portName
== portName
))});
37 console
.log('sent ' + msg
.success
);
40 console
.log('posted message');
43 // Workaround two bugs: shutdown crash if we hook 'unload', and content script
44 // GC if we don't register any event handlers.
45 // http://code.google.com/p/chromium/issues/detail?id=17410
46 // http://code.google.com/p/chromium/issues/detail?id=17582
48 win
.addEventListener('error', foo
);