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