OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 JSON.parse = function() { | 5 JSON.parse = function() { |
6 return "JSON.parse clobbered by content script."; | 6 return "JSON.parse clobbered by content script."; |
7 }; | 7 }; |
8 | 8 |
9 JSON.stringify = function() { | 9 JSON.stringify = function() { |
10 return "JSON.stringify clobbered by content script."; | 10 return "JSON.stringify clobbered by content script."; |
(...skipping 11 matching lines...) Expand all Loading... | |
22 chrome.runtime.onConnect.addListener(function(port) { | 22 chrome.runtime.onConnect.addListener(function(port) { |
23 console.log('connected'); | 23 console.log('connected'); |
24 port.onMessage.addListener(function(msg) { | 24 port.onMessage.addListener(function(msg) { |
25 console.log('got ' + msg); | 25 console.log('got ' + msg); |
26 if (msg.testPostMessage) { | 26 if (msg.testPostMessage) { |
27 port.postMessage({success: true}); | 27 port.postMessage({success: true}); |
28 } else if (msg.testPostMessageFromTab) { | 28 } else if (msg.testPostMessageFromTab) { |
29 testPostMessageFromTab(port); | 29 testPostMessageFromTab(port); |
30 } else if (msg.testSendMessageFromTab) { | 30 } else if (msg.testSendMessageFromTab) { |
31 testSendMessageFromTab(); | 31 testSendMessageFromTab(); |
32 } else if (msg.testSendMessageFromFrame) { | |
33 testSendMessageFromFrame(); | |
32 } else if (msg.testDisconnect) { | 34 } else if (msg.testDisconnect) { |
33 port.disconnect(); | 35 port.disconnect(); |
34 } else if (msg.testDisconnectOnClose) { | 36 } else if (msg.testDisconnectOnClose) { |
35 window.location = "about:blank"; | 37 window.location = "about:blank"; |
36 } else if (msg.testPortName) { | 38 } else if (msg.testPortName) { |
37 port.postMessage({portName:port.name}); | 39 port.postMessage({portName:port.name}); |
38 } else if (msg.testSendMessageFromTabError) { | 40 } else if (msg.testSendMessageFromTabError) { |
39 testSendMessageFromTabError(); | 41 testSendMessageFromTabError(); |
40 } else if (msg.testConnectFromTabError) { | 42 } else if (msg.testConnectFromTabError) { |
41 testConnectFromTabError(); | 43 testConnectFromTabError(); |
(...skipping 16 matching lines...) Expand all Loading... | |
58 // For test onMessage. | 60 // For test onMessage. |
59 function testSendMessageFromTab() { | 61 function testSendMessageFromTab() { |
60 chrome.runtime.sendMessage({step: 1}, function(response) { | 62 chrome.runtime.sendMessage({step: 1}, function(response) { |
61 if (response.nextStep) { | 63 if (response.nextStep) { |
62 console.log('testSendMessageFromTab sent'); | 64 console.log('testSendMessageFromTab sent'); |
63 chrome.runtime.sendMessage({step: 2}); | 65 chrome.runtime.sendMessage({step: 2}); |
64 } | 66 } |
65 }); | 67 }); |
66 } | 68 } |
67 | 69 |
70 function testSendMessageFromFrame() { | |
71 // Add two frames. The content script declared in manifest.json (frame.js) | |
72 // runs in frames whose URL matches ?testSendMessageFromFrame. | |
73 // frame.js sends a message to the background page, which checks that | |
74 // sender.frameId exists and is different for both frames. | |
75 var f = document.createElement('iframe'); | |
76 f.src = '?testSendMessageFromFrame'; | |
77 document.body.appendChild(f); | |
78 f = document.createElement('iframe'); | |
79 f.src = '?testSendMessageFromFrame'; | |
80 document.body.appendChild(f); | |
not at google - send to devlin
2014/11/11 01:00:56
Less duplication:
var createIframe(src) {
var f
robwu
2014/11/11 21:32:47
De-duplicated with a loop (which also gives unique
| |
81 } | |
82 | |
68 // Tests sendMessage to an invalid extension. | 83 // Tests sendMessage to an invalid extension. |
69 function testSendMessageFromTabError() { | 84 function testSendMessageFromTabError() { |
70 // try sending a request to a bad extension id | 85 // try sending a request to a bad extension id |
71 chrome.runtime.sendMessage("bad-extension-id", {m: 1}, function(response) { | 86 chrome.runtime.sendMessage("bad-extension-id", {m: 1}, function(response) { |
72 var success = (response === undefined && chrome.runtime.lastError); | 87 var success = (response === undefined && chrome.runtime.lastError); |
73 chrome.runtime.sendMessage({success: success}); | 88 chrome.runtime.sendMessage({success: success}); |
74 }); | 89 }); |
75 } | 90 } |
76 | 91 |
77 // Tests connecting to an invalid extension. | 92 // Tests connecting to an invalid extension. |
78 function testConnectFromTabError() { | 93 function testConnectFromTabError() { |
79 var port = chrome.runtime.connect("bad-extension-id"); | 94 var port = chrome.runtime.connect("bad-extension-id"); |
80 port.onDisconnect.addListener(function() { | 95 port.onDisconnect.addListener(function() { |
81 var success = (chrome.runtime.lastError ? true : false); | 96 var success = (chrome.runtime.lastError ? true : false); |
82 chrome.runtime.sendMessage({success: success}); | 97 chrome.runtime.sendMessage({success: success}); |
83 }); | 98 }); |
84 } | 99 } |
85 | 100 |
86 // For test sendMessage. | 101 // For test sendMessage. |
87 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { | 102 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { |
88 chrome.test.assertEq({id: chrome.runtime.id}, sender); | 103 chrome.test.assertEq({id: chrome.runtime.id}, sender); |
89 sendResponse({success: (request.step2 == 1)}); | 104 sendResponse({success: (request.step2 == 1)}); |
90 }); | 105 }); |
OLD | NEW |