I have a tab change listener in background script which will check for URL and accordingly send a message to the content script for DOM action. What is the issue in this code?
I have tried chrome.runtime. as well as chrome.tabs. API. Also, console.log( ) is not working in content.js.
background.js
chrome.tabs.onActivated.addListener(function(activeInfo) {
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
let arr = tabs[0].url.split('/');
let url = arr[0] + "//" + arr[2];
if(url=="https://youtube.com"){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(
tabs[0].id, {action: "open_dialog_box"}, function(response) {console.log(response)}
);
});
}
});
});
content.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log(request);
if (request.msg === "youtube_action") {
//console.log(request.data.subject)
alert("kdjfnodf");
}
return true;
});
manifest.json
{
"manifest_version" : 3,
"name" : "Play",
"description" : "Play",
"version": "2",
"browser_action" : {
"default_title" : "Play-Pause",
"default_popup" : "popup.html"
},
"permissions" : [
"activeTab",
"tabs",
"background"
],
"content_scripts": [{
"matches": ["http://*/*"],
"js": [
"content.js"
]
}],
"background" : {
"scripts": ["background.js"],
"persistent": false
}
}