0

Yes, I've searched and tried other answers.

I've been searching all over the place and found nothing for my problem, chrome.runtime.onMessage is undefined, a Mozilla bug report says that chrome.runtime.lastError existing prevents chrome.runtime.onMessage from existing, yet it doesn't.

Another question says to disable and reenable every extension? If this is the case, then that's going to be quite an inconvenient solution, as every time I'd run my code I would have to reload every extension I have.

This question, I'm not sure about. It seems they do not have an issue with chrome.runtime.onMessage, but they had an issue with getting the active tab.

Theres way more questions, but these are the ones which links/keywords I remember.

What I'm trying to do: Connect my popup.js file to my content.js file to the webpage.

Code

Manifest.json

{
    "name": "My Extension",
    "version": "1.0",
    "description": "First Extension",
    "permissions": [
      "tabs",
      "tabCapture",
      "downloads",
      "storage",
      "declarativeContent",
      "activeTab"
    ],
    "icons": { 
         ...
      },
    "content_scripts": [
      {
        "matches": ["https://example.com/*"],
        "js": ["content.js"]
      }
    ],
    "background": {
      "scripts": ["background.js"],
      "persistent": false
    },
    "options_page": "options.html",
    "page_action": {
      "default_popup": "popup.html",
      "default_icon": { 
         ...
      }
    },
    "manifest_version": 2
}

content.js

//...

    chrome.runtime.lastError = undefined;
    chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { //chrome.runtime.onMessage is undefined.
        console.log('something happened');
        myFunction(message.string);
    })

    window.onload = function() {
    myFunction(1);
    };

//...

popup.js (linked from popup.html

var button = document.createElement('a');
button.className = 'genericButton round noSelect';
button.value = 'hello';
button.innerText = `Send to the page`;
document.getElementById('buttongroup').appendChild(abtn);
button.onclick = function(e) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        if (tabs[0]) {
            chrome.tabs.sendMessage(tabs[0].id, {string: abtn.value});
        };
    });
}

If it helps:

Google Chrome   84.0.4147.105 (Official Build) (64-bit)
Revision    a6b12dfad6663f13a7e16e9a42a6a4975374096b-refs/branch-heads/4147@{#943}
JavaScript  V8 8.4.371.22
SomePerson
  • 1,171
  • 4
  • 16
  • 45
  • chrome.runtime.onMessage can't be undefined when used in a content script or any extension script. The only explanations for this error are that a) you run content.js as a page script e.g. via a DOM script element or b) you've reloaded the extension but didn't reload the web page tabs, c) it's a bug in the browser. – wOxxOm Aug 10 '20 at 11:39
  • Responses: a) I modify code with my content-script on the tab's document via document, and I'm not using this file to inject code with `chrome.tabs.executeScript(null, {file: 'content.js'})`. b) I've reloaded this extension PLENTY of times; but, I have 30~ Chrome tabs open, do I need to quit Chrome and reopen it every time I need to test this extension? And if I put this on the Chrome Web Store, will any users have the same issue? – SomePerson Aug 10 '20 at 17:23
  • You probably try to add onMessage listener in response to some user action in the web page so after reloading/updating the extension your old "orphaned" content script is no longer connected to the extension. See the [source code of Stylus extension](https://github.com/openstyles/stylus/tree/master/content/apply.js) for an example of handling this problem. – wOxxOm Aug 10 '20 at 17:41

0 Answers0