Stevie
Stevie

Reputation: 89

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. The extension id is labeled as id

This is my 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_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."}

Any clue as to why this is working as I have been exhausting the chrome docs and they all say this is proper.

Upvotes: 0

Views: 133

Answers (2)

Stevie
Stevie

Reputation: 89

Found a Solution. Problem is instead of all that, its better just to specify anything coming from a domain. *//Stevie.dmnnt.msft/* worked.

Upvotes: 0

Haibara Ai
Haibara Ai

Reputation: 10897

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.

Upvotes: 1

Related Questions