[英]Chrome onMessage listener always sends 'undefined' response
I have made the most basic chrome extension that could use message passing. 我做了可以使用消息传递的最基本的chrome扩展。 The extension should listen for messages from a website ( localhost:8080/*
here) and always respond "Bye". 该扩展程序应侦听来自网站的消息(此处为localhost:8080/*
),并始终响应“再见”。 In the test selenium opens a locally served page with chrome which tries to send a message to the extension and then throws the response on the console: 在测试中,硒打开一个带有chrome的本地服务页面,该页面尝试向扩展发送消息,然后在控制台上抛出响应:
$ tree -I node_modules
.
├── extension
│ ├── background.js
│ └── manifest.json
├── package.json
└── test
├── index.html
└── selenium-test.js
2 directories, 5 files
background.js background.js
chrome.runtime.onMessage.addListener(
function (req, sender, sendResp) {
sendResp('Bye');
});
manifest.json 的manifest.json
{
"name": "chrome-test",
"version": "0.1",
"manifest_version": 2,
"key": "pcoogjpilcclcmejpkmbifdbihomlgec",
"description": "Test extension.",
"app": {
"background": {
"scripts": ["background.js"],
"persistent": true
}
},
"externally_connectable": {
"matches": [
"http://localhost:8080/*"
],
"accepts_tls_channel_id": false
},
"permissions": [
"http://localhost:8080/*"
]
}
package.json 的package.json
{
"name": "chrome-test",
"version": "0.0.5",
"description": "Chrome API sucks.",
"keywords": [ "" ],
"author": "Chris Perivolaropoulos",
"contributors": [],
"dependencies": {
"selenium-webdriver": "*",
"mocha": "*",
"chai": "*"
},
"scripts": {
"test": "mocha test/selenium-test.js"
}
}
index.html 的index.html
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1>Test page</h1>
<div id="echo"></div>
<script type="text/javascript">
chrome.runtime.sendMessage('pcoogjpilcclcmejpkmbifdbihomlgec', 'hello',
function (msg) {
console.log("Received! " + msg);
});
</script>
</body>
</html>
selenium-test.js 硒test.js
var assert = require('chai').assert,
test = require('selenium-webdriver/testing'),
webdriver = require('selenium-webdriver'),
chromedriver = require('selenium-webdriver/chrome');
// @param extensions: string of unpacked extension path to install.
function chrome_driver(extension) {
var logperfs = new webdriver.logging.Preferences(),
opts = new chromedriver.Options().
addArguments("--load-extension=" + extension ||
'../extension');
logperfs.setLevel(webdriver.logging.Type.BROWSER,
webdriver.logging.Level.ALL);
var chrome = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
setChromeOptions(opts).
setLoggingPrefs(logperfs).
build();
chrome.manage().timeouts().pageLoadTimeout(5000);
return chrome;
}
function browser_logs(driver, callback) {
driver.manage().logs().
get(webdriver.logging.Type.BROWSER).then(callback);
}
test.describe('Test', function() {
var chrome;
this.timeout(10000);
test.before(function() {
chrome = chrome_driver("extension");
});
test.it("Test messages", function () {
chrome.get("http://localhost:8080/test/index.html").then(function () {
browser_logs(chrome, function (entries) {
entries.forEach(function (e) {console.log("BrowserLog: " + e.message);});
assert.equal(entries.pop().message,
"hello", "Bus not echoing.");
});
});
});
test.after(function() {
chrome.quit();
});
});
To run a test first run a local http server 要运行测试,请首先运行本地http服务器
$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 ...
and from another console run the tests 并从另一个控制台运行测试
$ npm test
> chrome-test@0.0.5 test /path/to/project
> mocha test/selenium-test.js
Test
BrowserLog: http://localhost:8080/test/index.html 11:15 Received! undefined
[2K[0G 1) Test messages
0 passing (1s)
1 failing
1) Test Test messages:
Bus not echoing.
+ expected - actual
+hello
-http://localhost:8080/test/index.html 11:15 Receviced! undefined
at /path/to/project/test/selenium-test.js:43:12
at /path/to/project/node_modules/selenium-webdriver/lib/goog/base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1640:20)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at Object.webdriver.promise.asap (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:758:5)
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1651:25)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at Object.webdriver.promise.asap (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:758:5)
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1651:25)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at /path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1507:10
at /path/to/project/node_modules/selenium-webdriver/lib/goog/base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1640:20)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at /path/to/project/node_modules/selenium-webdriver/lib/goog/base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1640:20)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at /path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:703:49
at /path/to/project/node_modules/selenium-webdriver/lib/webdriver/http/http.js:96:5
at IncomingMessage.<anonymous> (/path/to/project/node_modules/selenium-webdriver/http/index.js:131:7)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:943:16
at process._tickCallback (node.js:419:13)
==== async task ====
WebDriver.manage().logs().get(browser)
at webdriver.WebDriver.schedule (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:302:15)
at webdriver.WebDriver.Logs.get (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:1380:23)
at browser_logs (/path/to/project/test/selenium-test.js:28:3)
at /path/to/project/test/selenium-test.js:41:4
at /path/to/project/node_modules/selenium-webdriver/lib/goog/base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1640:20)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at Object.webdriver.promise.asap (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:758:5)
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1651:25)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at /path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1507:10
at /path/to/project/node_modules/selenium-webdriver/lib/goog/base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1640:20)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at /path/to/project/node_modules/selenium-webdriver/lib/goog/base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:1640:20)
at notify (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:444:12)
at notifyAll (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:422:7)
at resolve (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:400:7)
at fulfill (/path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:512:5)
at /path/to/project/node_modules/selenium-webdriver/lib/webdriver/promise.js:703:49
at /path/to/project/node_modules/selenium-webdriver/lib/webdriver/http/http.js:96:5
at IncomingMessage.<anonymous> (/path/to/project/node_modules/selenium-webdriver/http/index.js:131:7)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:943:16
at process._tickCallback (node.js:419:13)
==== async task ====
at Context.ret (/path/to/project/node_modules/selenium-webdriver/testing/index.js:132:12)
at Test.Runnable.run (/path/to/project/node_modules/mocha/lib/runnable.js:216:15)
at Runner.runTest (/path/to/project/node_modules/mocha/lib/runner.js:373:10)
at /path/to/project/node_modules/mocha/lib/runner.js:451:12
at next (/path/to/project/node_modules/mocha/lib/runner.js:298:14)
at /path/to/project/node_modules/mocha/lib/runner.js:308:7
at next (/path/to/project/node_modules/mocha/lib/runner.js:246:23)
at Object._onImmediate (/path/to/project/node_modules/mocha/lib/runner.js:275:5)
at processImmediate [as _immediateCallback] (timers.js:345:15)
npm ERR! Test failed. See above for more details.
The extension always responds undefined
instead of 'Bye'
as instructed in background.js . 该扩展始终响应undefined
而不是按照background.js中的指示'Bye'
。
Messages sent from webpages via externally_connectable
are considered external messages. 从网页发送的消息经由externally_connectable
被认为是外部的消息。 Therefore, your background script should look like this : 因此,您的背景脚本应如下所示 :
chrome.runtime.onMessageExternal.addListener(
function (req, sender, sendResp) {
sendResp('Bye');
}
);
You get the impression that the listener sends an undefined response, because the callback of sendMessage
is called in one of two cases: 您会感觉到侦听器发送了未定义的响应,因为在以下两种情况之一中调用了sendMessage
的回调:
sendResponse
. 侦听器实际上称为sendResponse
。 Then the argument is set to that response. 然后将参数设置为该响应。 undefined
and chrome.runtime.lastError
is set. 然后undefined
参数,并设置了chrome.runtime.lastError
。 You are hitting the second case - as there was no listener for the appropriate event. 您正在碰第二种情况-因为没有监听适当事件的人。
Now, a separate question is whether the webpage context even gets access to chrome.runtime.lastError
.. 现在,另一个问题是网页上下文是否甚至可以访问chrome.runtime.lastError
..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.