0

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
    }
}
Community
  • 1
  • 1
Saurav Bhagat
  • 55
  • 4
  • 15
  • YouTube URL should have www, Chrome just doesn't show it in the address box, but you can see it if you actually debug the code in devtools, which is the first thing to do, actually. Note, the background script runs in a hidden background page which has its [own devtools](https://stackoverflow.com/questions/10257301/where-to-read-console-messages-from-background-js-in-a-chrome-extension). – wOxxOm Mar 25 '19 at 04:19
  • Another problem is that your manifest.json declares the content script only on http URLs, but YouTube runs on https. Actually it's unclear why you declare an automatically injected content script anyway - judging by the code in the question you can use chrome.tabs.executeScript or specify the individual URL patterns e.g. "matches": [`"https://youtube.com/*"`, `"https://www.youtube.com/*"`] – wOxxOm Mar 25 '19 at 04:49
  • Yes, I have included www. also it's just not here for making it short, the main problem is in the message passing. – Saurav Bhagat Mar 25 '19 at 05:21
  • @wOxxOm I included *:// instead of http and inspecting in background page devtools. – Saurav Bhagat Mar 25 '19 at 05:24
  • The question doesn't make it clear what exactly you're doing, what exactly you're seeing, what exactly you were expecting to see instead. I can only guess onActivated isn't what you need here. Maybe chrome.tabs.onUpdated is what you want. – wOxxOm Mar 25 '19 at 05:49

0 Answers0