简体   繁体   English

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

[英]Cannot pass messages from Webpage to 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>

I made a chrome extention and want to send a message to my extensions background script to do stuff with a click of a button as seen. 我进行了chrome扩展,并希望向扩展程序后台脚本发送一条消息,以便单击所显示的按钮即可完成操作。 The extension id is labeled as id 扩展ID被标记为id

This is my background.js 这是我的background.js

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

        });

Here is my manifest.json 这是我的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"]
  }
}

However, when i try to run the connect, I get Message didn't cross site and the error Object {message: "Could not establish connection. Receiving end does not exist."} 但是,当我尝试运行连接时,我得到Message didn't cross site并且错误Object {message: "Could not establish connection. Receiving end does not exist."}

Any clue as to why this is working as I have been exhausting the chrome docs and they all say this is proper. 关于这为什么起作用的任何线索,因为我一直在用chrome文档,他们都说这是正确的。

The root cause is the following line: 根本原因是以下几行:

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

"https://google.com/myindex/Want.html" doesn't match any valid url, you should use something like https://*.google.com/myindex/Want.html or *://*.google.com/myindex/Want.html if you are sure that Want.html is under google host. "https://google.com/myindex/Want.html"与任何有效的网址均不匹配,您应使用https://*.google.com/myindex/Want.html*://*.google.com/myindex/Want.html如果您确定Want.html在Google托管下)。

Found a Solution. 找到了解决方案。 Problem is instead of all that, its better just to specify anything coming from a domain. 问题不是全部,而是最好指定来自域的任何内容。 *//Stevie.dmnnt.msft/* worked. *//Stevie.dmnnt.msft/*有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM