Why is chrome.runtime undefined in script content?
I have a very simple chrome extension where I am trying to pass a message from a background script to a content script. But chrome.runtime
- undefined.
This is literally all the code here, as you can see almost nothing there. In the content script, the execution time is undefined.
Script background:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.runtime.sendMessage({action: 'someAction'},
function(response) {
console.log('Response: ', response);
});
});
Script content:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
sendResponse({
user: 'user'
});
});
manifest.json
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"description": "Some stupid extension",
"browser_action": {
"default_icon": "icons/MyExtensionIcon.png"
},
"icons": {
"48": "icons/MyExtensionIcon.png"
},
"permissions": [
"tabs",
"storage",
"https://*/",
"http://*/"
],
"content_scripts": [
{
"matches": ["*://*.twitter.com/*", "https://twitter.com/*"],
"js": ["js/content.js"]
}
],
"background": {
"scripts": ["js/background.js"],
"persistent": true
},
"web_accessible_resources": [
"js/*",
"css/*"
]
}
Additional Information:
- Chrome version 58.0.3029.110 (64-bit)
- Installing my extension as "Unpacked extension" with developer mode
+3
source to share
1 answer
Ok I figured it out. This is absolutely silly, but it looks like it's just Heisenbug . When adding a breakpoint or debugger statement, this value must be undefined. Maybe a chrome bug?
I swear every day Chrome feels more and more like Internet Explorer.
+7
source to share