0

I m working on extension that changes the background color of all divs on page to red but the control doesnot pass to the content script. Using the developers tools i m able to set breakpoints in background.js and popup.js but not in content.js so i used console.log but it is not executing plz help

popup.html

<!DOCTYPE html>
<html>
<head>
<script src="popup.js"></script>
</head>
<body>

<button id="change-btn">Change color of Divs</button>

</body>    
</html>

popup.js

window.onload=init;

function init(){

    var btn=document.getElementById("change-btn");
    btn.onclick = function(){

        chrome.runtime.sendMessage({type:"color-div" });
     }

}; 

background.js

chrome.runtime.onMessage.addListener(function(request, sender, response){

     if(request.type==="color-div"){

         colordiv();

     }
});



function colordiv(){
    chrome.tabs.query({ active:true, currentWindow:true} , function(tab){
        chrome.tabs.sendMessage( tab[0].id, {type: "colors-div", color: "#F00"});

    });
};

content.js

chrome.runtime.onMessage.addListener(function(request, sender, response){
        if(request.type==="colors-div"){
            var divs= document.querySelectorAll("div");
            if(divs.length===0)
                alert("There r no divs on this page ");
            else{
                console.log("Before for loop");
                for(var i=0; i<divs.length; i++)
                {
                    divs[i].style.backgroundColor=request.color;
                }
                console.log("After for loop");
            }
        }
    });

manifest.json

{
    "name": "BrowserExtension",
    "version": "0.0.1",
    "manifest_version": 2,
    "description" : "Description ...",
     "content_scripts": [{
        "matches": ["http://*/*", "https://*/*"],
        "js": ["content.js"]
    }],
    "browser_action": {
        "default_icon": "icon.png",
        "default_title": "That's the tool tip",
        "default_popup": "popup.html"
    },
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "permissions":["tabs"]

}
Casey
  • 87
  • 1
  • 1
  • 13
  • Are you aware that you need to choose the console specific to the extension? Just above the console on the left is a drop down menu for which you want. – Teepeemm Feb 17 '15 at 12:59
  • @Teepeemm It's needed to execute statements, but not needed for reading logs (they are shown from all contexts) – Xan Feb 17 '15 at 13:21
  • If you say you're not able to set breakpoints - are you looking at the right dev tools? If should be the page's own dev tools. – Xan Feb 17 '15 at 13:21
  • Your code looks correct to me at first glance. However, if you're running a debugger in a separate window, your active tab query may return the DEBUGGER window (to which your content script is not injected) – Xan Feb 17 '15 at 13:29

1 Answers1

1

This code works for me and the console.log statements show up in the Javascript console for the page when I open developer tools. Make sure that you refresh the tab you are targeting so that the content script is injected and be aware that this script will not be injected to local HTML documents because "file:///*" is not one of the matching rules specified for the content script.

Reilly Grant
  • 5,590
  • 1
  • 13
  • 23
  • Thanks tried my code now it worked. IDK y it was not working before – Casey Feb 18 '15 at 06:19
  • @Casey I have a hunch. Take a look at [this answer](http://stackoverflow.com/a/23895822/934239). Is that your situation? – Xan Feb 18 '15 at 12:54