【发布时间】:2014-05-08 02:22:37
【问题描述】:
我正在尝试从网页与打包的应用程序进行通信。这个想法是让网页从串行设备中读取一个数字。因为要访问串口设备,所以需要一个打包好的app,不能使用扩展。这与Keep Chrome Packaged App running in background? 非常相似,似乎Chrome documentation 表示这是可能的。
如何从常规网页执行 chrome.runtime.sendMessage?当我这样做时,我得到 *Uncaught TypeError: Cannot read property 'sendMessage' of undefined。我的简单功能是:
function doFunction(){
chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
function(response) {
if (!response.success)
handleError(url);
});
}
我的打包应用程序加载并可以访问串行端口。但我怀疑清单没有“启用”常规网页的 chrome.runtime 。清单.json:
{
"name": "Hello World!",
"description": "My first Chrome App.",
"version": "0.1",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"icons": { "16": "calculator-16.png", "128": "calculator-128.png" },
"permissions": [
"serial",
"*://localhost/*"
],
"externally_connectable": {
"matches": [
"*://localhost/*"]
}
}
也许是我用于测试的 ://localhost/。但 Chrome 没有抱怨。
有什么想法吗?提前致谢。
【问题讨论】:
-
Err,
// For tests.不是有效的 JSON,您不能在此文件中包含 cmets。试着把它拿出来。 -
感谢您的想法。我把它拿出来,没有任何变化。 Chrome 会加载打包的应用程序并正常运行。请注意,它没有打包。不幸的是,我的常规网页(来自文件://localhost/)仍然未定义 chrome.runtime。
-
来自文档:'URL 模式必须至少包含一个二级域 - 即,禁止使用诸如
"*"、"*.com"、"*.co.uk"和"*.appspot.com"之类的主机名模式。尝试在您的 hosts 文件中为 127.0.0.1 定义一个二级别名并使用它。
标签: google-chrome google-chrome-app