无法将邮件从网页传递到Chrome扩展程序

时间:2016-07-28 23:24:09

标签: javascript html google-chrome google-chrome-extension

Want.html

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>

</head>
<body>
    <button onclick="myFunction()">Copy Text</button>
    <script>
        function myFunction() {
            alert("go");
            var id = "lmeobdocdijngimfmndbaljkejddkpbc"
            chrome.runtime.sendMessage(id, { data: "Todo" },
                function(response){
                    if(response == undefined){
                        console.log("Message didnt cross site");
                        console.log(chrome.runtime.lastError);
                        console.log(chrome.runtime.lastError.message);
                    }
                }

            )};
    </script>
</body>
</html>

我做了一个chrome扩展程序,想要向我的扩展程序后台脚本发送一条消息,只需点击一下按钮即可。扩展程序ID标记为id

这是我的background.js

chrome.runtime.onMessageExternal.addListener(
    function (request, sender, sendResponse) {
        if (request.data === "Todo") {
            alert("Listened");                
            sendResponse({ data: "money" });

        });

这是我的manifest.json

{
  "manifest_version": 2,

  "name": "E",
  "description": "P",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "background": {
    "scripts": [ "background.js" ]
  },

  "permissions": [
    "*://*/*",
    "cookies",
    "activeTab",
    "tabs",
    "https://ajax.googleapis.com/",

  ],
  "externally_connectable": {
      "matches": ["https://google.com/myindex/Want.html"]
  }
}

但是,当我尝试运行连接时,我得到Message didn't cross site并且错误Object {message: "Could not establish connection. Receiving end does not exist."}

任何关于为什么这样做有效的线索,因为我已经用尽了Chrome文档并且他们都说这是正确的。

2 个答案:

答案 0 :(得分:1)

根本原因如下:

"externally_connectable": {
  "matches": ["https://google.com/myindex/Want.html"]
}

"https://google.com/myindex/Want.html"与任何有效网址都不匹配,如果您确定https://*.google.com/myindex/Want.html位于Google托管主机下,则应使用*://*.google.com/myindex/Want.htmlWant.html之类的内容。

答案 1 :(得分:0)

找到解决方案。问题不是全部,而是更好地指定来自域的任何东西。 *//Stevie.dmnnt.msft/*工作了。