从外部html发送消息到chrome扩展主机

时间:2019-06-17 09:05:58

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

我正在尝试使用chrome本地消息传递将消息从桌面目录上的虚拟html页面发送到chrome扩展主机。

这是主机的json文件:

char

此json已在Computer \ HKEY_CURRENT_USER \ Software \ Google \ Chrome \ NativeMessagingHosts \ com.myhost下注册

这是我的扩展程序的manifest.json文件:

{
  "name": "com.myhost",
  "description": "Chrome Native Messaging API Example Host",
  "path": "myhost.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
  ]
}

我能够从main.html页面发送和接收消息。但是,我无法使用以下命令从dummy.html文件向扩展主机发送任何消息:

{
  // Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
  "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
  "name": "Native Messaging Example",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Send a message to a native application.",
  "app": {
    "launch": {
      "local_path": "main.html"
    }
  },
  "icons": {
    "128": "icon-128.png"
  },
  "permissions": [
    "nativeMessaging","tabs","webNavigation","system.display","*://dummy.html/*"
  ],
"background": {
    "background_page": "background.html" 
  },

  "externally_connectable": {
    "matches": ["*://dummy.html/*"],
    "ids": ["*"]
    },
    "content_scripts" : [{
        "matches" : ["*://*/*"],
        "js" : ["content_script.js"],
        "run_at": "document_end"
    }]
}

尝试交流时,出现以下错误:

chrome.runtime.sendMessage('knldjmfmopnpolahpmmgbagdohdnhkik', { command: 'heyyyy' },
            function(response){

            });
window.postMessage("knldjmfmopnpolahpmmgbagdohdnhkik", { text: "heyyy" });

有什么建议吗? 谢谢

0 个答案:

没有答案