gpt4 book ai didi

google-chrome - 使用 chrome.runtime.sendmessage 从网页与打包应用程序进行通信

转载 作者:行者123 更新时间:2023-12-05 08:21:06 28 4
gpt4 key购买 nike

我正在尝试从网页与打包应用进行通信。这个想法是让网页从串行设备读取一个数字。因为要访问串口设备,需要打包好的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);
});
}

我的打包应用加载并可以访问串行端口。但我怀疑 list 并没有“启用”常规网页的 chrome.runtime。 list .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 不会提示。

有什么想法吗?提前致谢。

最佳答案

Xan 的评论起到了作用。

虽然 Chrome 没有提示 *://localhost/*,但它不起作用。 Chrome 确实提示其他组合,例如 file://localhost/

我将 foo.com 添加到主机文件中,然后通过 Web 服务器提供我的网页,并且成功了!我可以从我的网页与我打包的应用程序进行通信。

请注意,浏览到 file://www.foo.com/hostpage.html 无效。但正在浏览 http://www.foo.com:3000/hostpage.html做过。 (我正在使用 Rails,因此使用 3000 端口)。

故事的寓意:在本地进行测试时,您需要在主机文件中添加一个带有虚假二级域的条目。

这是我的 manifest.json:

{
"name": "RFID Tag Reader",
"description": "Reads RFID Tags connected via USB reader",
"version": "0.0.0.1",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"icons": {
"16": "rfid-16.png",
"128": "rfid-128.png"
},
"permissions": [
"serial",
"*://www.foo.com/*",
"*://arealsite.net/*"
],
"externally_connectable": {
"matches": [
"*://www.foo.com/*",
"*://arealsite.net/*"
]
}
}

关于google-chrome - 使用 chrome.runtime.sendmessage 从网页与打包应用程序进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23531671/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com