5

I'm trying to create a very simple Chrome extension that will allow me to highlight a word on a webpage, right click to open the context menu, and then search it on a database called Whitaker's Words by simply appending the word to the search URL. I'm continuing to receive

"Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist."

as an error every time I run the code and attempt to use the context menu.

At the moment, I have already taken the steps to disable all other extensions and I attempted to use the port documentation on the Chrome Messaging Docs but I wasn't able to resolve the issue that way.

background.js

    chrome.contextMenus.create({
    title: "Search Whitaker's Words",
    contexts: ["selection"]
});


chrome.contextMenus.onClicked.addListener(function() {
    chrome.runtime.sendMessage({ method: "getSelection" }, function (response) {
        sendToWW(response.data);
    });
});

function sendToWW(selectedText) {
    var serviceCall = 'http://archives.nd.edu/cgi-bin/wordz.pl?keyword=' + selectedText;
    chrome.tabs.create({ url: serviceCall });
}

Here, I create a context menu and when the menu item is clicked, I send a message to the context script asking for the highlighted selection. I then return this to another function in background.js that will create a new tab with the search query.

content.js

chrome.runtime.onMessage.addListener(function (message) {
    if (message.method === "getSelection"){
        var word = window.getSelection().toString().trim();
        console.log(word);
        chrome.runtime.sendMessage({ data: word });
    }
    else
        chrome.runtime.sendMessage({}); // snub them.
});

I listen here for the message and then take a selection from the window, trim, and send it back.

manifest.json

{
  "manifest_version": 2,
  "name": "Latinate",
  "version": "0.1",
  "description": "Aid in Latin translation using Whitaker's Words",
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "jquery-3.4.1.min.js",
        "content.js"
      ]
    }
  ],
  "background": {
    "scripts": [
      "background.js"
    ]
  },
  "permissions": [
    "contextMenus",
    "tabs"
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

Any and all help would be appreciated! I've tried nearly everything I could find that seemed to apply.

vjawarani
  • 53
  • 1
  • 3

1 Answers1

4

The error message says there is no message listener on the other side. And indeed there is none because a message listener is a function registered with chrome.runtime.onMessage.addListener - in your extension only the content script has such a listener.

Instead of sending a new message back, send the response using sendResponse function which is passed as a parameter to the onMessage listener
(see also the messaging tutorial).

Another problem is that to send a message to a tab you need to use a different method: chrome.tabs.sendMessage with a tab id as the first parameter.

background script:

chrome.contextMenus.onClicked.addListener((info, tab) => {
  chrome.tabs.sendMessage(tab.id, {method: 'getSelection'}, response => {
    sendToWW(response.data);
  });
});

content script:

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.method === 'getSelection') {
    var word = window.getSelection().toString().trim();
    console.log(word);
    sendResponse({ data: word });
  } else {
    sendResponse({});
  }
});
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • 5
    Also when you edit a content/background script you need to reload the extension on `chrome://extensions` page and reload the web tab as well. – wOxxOm Dec 06 '19 at 13:48
  • WoxxOm -- this is the correct answer 100%. I was scratching my head trying to figure out why my nearly identical code wasn't working for one sendMessage vs. another. I literally had to just, close the tab, and reload the tab. For whatever reason, that fixed it. I think it was using old, messed up code on that tab, for whatever reason, and only refreshed the code after closing and re-opening a new tab. One additional point: Make sure you have the appropriate URL permissions in your content script manifest section. If you try to use it for a URL you don't have permission for, it won't work. – king_anton Dec 24 '22 at 21:24
  • @king_anton, if you reloaded the extension you need to [re-inject](https://stackoverflow.com/q/10994324) your content scripts. There's no need for additional permissions because `content_scripts` already sets them. If you see a different behavior please open a new question, don't hijack a completed topic. – wOxxOm Dec 25 '22 at 07:40