I'm new to chrome extensions and keep getting stuck with the basics so I'm clearly missing something. Similar questions have been asked but nothing seems to be work. I am trying to send a basic message to the content script but cannot console.log the message.
manifest.json
{
"name": "Ext_Test",
"version": "1.0",
"description": "testing",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"run_at": "document_start",
"js": ["content.js"]
}
],
"browser_action": {
"default_popup": "popup.html",
"default_title": "Testing"
},
"permissions": [
"tabs",
"*://*/*"
]
}
background.js
chrome.tabs.onUpdated.addListener(function(tabId,info, tab) {
if (info.status == "complete") {
chrome.tabs.sendMessage(tabId, {action: "test", message: tab.url})
}
});
content.js
console.log("before method");
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.action == "test"){
console.log(message);
}
});
console.log("after method");
From the console log I can see "before method" and "after method" being printed but cannot see the message inside the function.
Update: popup.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script type="module" src="content.js"></script>
</body>
</html>