【问题标题】:Send message to Chrome App from Web Page从网页向 Chrome 应用程序发送消息
【发布时间】:2016-07-21 13:32:37
【问题描述】:

app.js

chrome.runtime.onMessageExternal.addListener(
  function (request, sender, sendResponse) {
      console.log("Message Recived");        
  });

Page.html

chrome.runtime.sendMessage("From WebPage", { openUrlInEditor: "http://localhost:54854/MainPage.html" },
          function (response) {             

          });

ma​​nifest.json

"externally_connectable": {
  "matches": ["*://*.example.com/*"]
}

但我仍然无法从网页向 Chrome APP 发送消息。 参考:https://developer.chrome.com/extensions/messaging#external

请帮助我:(

【问题讨论】:

    标签: google-chrome google-chrome-extension google-chrome-app messaging


    【解决方案1】:

    chrome.runtime.sendMessage 用于发送external消息的第一个参数不是任意的ID,而是将接收消息的扩展/应用的ID。

    对于已发布的应用,ID 在您首次提交到商店时就已固定。

    对于解压的应用程序,ID 由清单中的"key" 字段(如果有)或by the path to the folder 确定。 fix the ID for development 可能有用,这样它就不会因计算机而异。

    【讨论】:

    • 好的,我收到了Chrome APP的网页消息。但我可以反过来吗?也就是Chrome APP向网页发送消息?
    • 您无法从应用程序启动它。您只能使用sendResponse回复消息或打开一个与connect()进行双向通信的端口,但必须由页面发起。
    • 好吧,我想我可以使用以下流程进行通信:Chrome APP => Chrome Extension => My WebPage? :P
    • 这太过分了。为什么不立即通过页面打开一个端口,然后根据需要使用该端口从应用程序中发送消息?
    • 好吧,我将不得不从 Chrome APP 调用硬件事件的网页功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 2018-06-14
    • 2015-11-25
    • 1970-01-01
    相关资源
    最近更新 更多