Google chrome 使用chrome.runtime.sendmessage从网页与打包应用程序通信

Google chrome 使用chrome.runtime.sendmessage从网页与打包应用程序通信,google-chrome,google-chrome-app,Google Chrome,Google Chrome App,我正在尝试从网页到打包应用程序进行通信。其想法是让网页从串行设备读取一个数字。因为我想访问串行设备,我需要一个打包的应用程序,不能使用扩展。这很类似于,似乎说这是可能的 如何从常规网页执行chrome.runtime.sendMessage?当我这样做时,我得到*UncaughtTypeError:无法读取未定义的属性“sendMessage”。我的简单功能是: function doFunction(){ chrome.runtime.sendMessage(editorExtensi

我正在尝试从网页到打包应用程序进行通信。其想法是让网页从串行设备读取一个数字。因为我想访问串行设备,我需要一个打包的应用程序,不能使用扩展。这很类似于,似乎说这是可能的

如何从常规网页执行chrome.runtime.sendMessage?当我这样做时,我得到*UncaughtTypeError:无法读取未定义的属性“sendMessage”。我的简单功能是:

function doFunction(){
    chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
      function(response) {
        if (!response.success)
          handleError(url);
      });
}
我的打包应用程序加载并可以访问串行端口。但我怀疑清单没有“启用”常规网页的chrome.runtime。Manifest.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/*"]
}
}
{
  "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/*"
    ]
  }
}
可能是我用来测试的://localhost/。但Chrome并没有抱怨


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

Xan的评论起到了作用

虽然Chrome没有抱怨
*://localhost/*
,但它不起作用。Chrome确实抱怨过其他组合,比如
file://localhost/

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

请注意,浏览到file://www.foo.com/hostpage.html 不起作用。但是,浏览一下就知道了。(我使用Rails,因此使用3000端口)

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

这是我的manifest.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/*"]
}
}
{
  "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/*"
    ]
  }
}

错误,
//用于测试。
不是有效的JSON,此文件中不能有注释。试试把它拿出来。谢谢你的主意。我把它拿了出来,没有零钱。Chrome加载打包的应用程序并正常运行。请注意,它没有包装。不幸的是,我的常规网页(来自file://localhost/)仍然未定义chrome.runtime。从文档中可以看出:“URL模式必须至少包含一个二级域,即主机名模式,如
“*”
“*.com”
“*.co.uk”
,和
“*.appspot.com”
被禁止。请尝试在主机文件中为127.0.0.1定义一个二级别名并使用它。您可以要求我将我的评论转换为答案。。但是哦,好吧。我在评论中的回答是我自己的错。对不起。我不太懂堆栈溢出。如果你想把它转换成一个答案,然后我接受它,那对我来说没关系。我按照建议做了,但没有运气。你的舱单结果如何@对于那些对主机文件不熟悉的人,只需将此条目添加到mac上的
/etc/hosts
127.0.0.1 your test domain.com
。如果它不能立即工作,请尝试清除缓存。它可能与浏览器相关。在“外部可连接”中添加
“*://localhost/*”
作为“匹配项”对我也很有用(Chrome 86.0.4240.111)