title: Background Script is not sending message to document script - Chrome Extension
body: <p>I am working with chrome extension and trying to send a message from background script to the content script. But I do not know why the message from background is not passing to the content script. </p>
  
  <blockquote>
    <p>First of all I check the cookies in the background script, if the cookies are available then it passes a message  <code>{type:"allow_access"}</code> if no cookies are available then it passes <code>{type:"not_logged_in"}</code>. This object has been passed to the content scripts, and the <code>switch(sent.type)</code> decide about what has been passed to the content script. And after that a function had been called in the first case and in the other case only <code>alert()</code> method. </p>
    
    <blockquote>
      <p>But Unfortunately there might be some problem with my code that is preventing that message from passing to the content script.</p>
    </blockquote>
  </blockquote>
  
  <p><strong>Note</strong> - The <code>chrome.runtime.sendMessage({type:"open_login"});</code> in the very beginning is simply sending a message to background script to open a tab. And is a separate message.</p>
  
  <p><strong>Document Script</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var bact_btn = document.getElementById("openLogin");
  bact_btn.addEventListener("click",function(){
   chrome.runtime.sendMessage({type:"open_login"});
  });
  
  chrome.runtime.onMessage.addListener(function(sent,sender,res){
      console.log("Document Script is called  !");
   switch(sent.type){
    case "allow_access":{
        alert("You are logged in !")
        populate();
        break;
       }
       case "not_logged_in":{
        alert("You are not logged in to your account to use the extension ! Please login.");
        break;
       }
   }
   
  });
  
  function populate(){
      alert("Populate has been called ! ");
   var fbEl = document.getElementsByClassName("_42nr");
   var fbPost = document.getElementsByClassName("_5jmm _5pat _3lb4 s_1qgkos469b");
   var fbPostUrl = document.getElementsByClassName("_5pb8 _8o _8s lfloat _ohe");
   var h = '&lt;a href="" &gt;&lt;div style="float:left;font-weight:bold;cursor:pointer;height:14px;color:#4b4f56;" class="myprivateLikeButton"&gt;&lt;img src="http://bitbaysolutions.com/Icons/icon_like.png" style="width:17px;height:17px;margin-right:7px;float:left;margin-top:-4px;"&gt;Private Like&lt;/div&gt;&lt;/a&gt;';
   var currCont = 0;
  
   window.onload = function(){
       currCont = fbPost.length;
       console.log(currCont);
   }
  
  
   for (var i = 0; i &lt; fbEl.length ; i++) {
    if((fbEl[i].children.length&lt;=3) &amp;&amp; !(fbEl[i].children[0].innerText.toLowerCase().trim()=="private like")){
     fbEl[i].insertAdjacentHTML('afterbegin',h);
    }
   }
  
   window.onscroll = function(){
    if(fbPost.length&gt;currCont){
     for (var i = 0; i &lt; fbEl.length ; i++) {
      if((fbEl[i].children.length&lt;=3) &amp;&amp; !(fbEl[i].children[0].innerText.toLowerCase().trim()=="private like")){
       fbEl[i].insertAdjacentHTML('afterbegin',h);
      }
     }
    }
   }
  
   document.addEventListener("click",function(e){
    e.preventDefault();
    var ev = e.target; 
    if(ev.innerText=="Private Like"){
  
     var liked = "http://bitbaysolutions.com/Icons/icon_liked.png";
     ev.firstChild.setAttribute("src",liked) ;
     ev.style.color="#e20505";
  
     getIndex(fbEl,ev.parentElement.parentElement);
  
    };
   });
   function getIndex(el,pr){
  
    //Find the index value of the fb Element clicked
    var index=-1;
    for(var i=0;i&lt;el.length;i++){
        if(el[i]===pr){
             index=i;
             break;
        }
    }
    getUrl(index);
   }
  
   function getUrl(ind){
      var postInd = -1;
      for(var i=0;i&lt;fbPost.length;i++){
        if(i==ind){
         postInd=i;
           var url = fbPost[postInd].getElementsByClassName("_5pb8 _8o _8s lfloat _ohe")[0].getAttribute("href");
           chrome.runtime.sendMessage({type:"postUrl",mess:url});
        }
      }
   }
  
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Background Script</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(window).on('load', function(){
     
     if (document.cookie.indexOf("_session") &gt;= 0) {
  
          chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ 
        chrome.tabs.sendMessage(tabs[0].id, {type:"allow_access"}, function(response) {
         
        }); 
       });
         
   } else{ 
    
          chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ 
        chrome.tabs.sendMessage(tabs[0].id, {type:"not_logged_in"}, function(response) {
         
        }); 
       });
   
  
   }
  });
  
  chrome.tabs.create({url:"myFrame.html"});
  
  chrome.runtime.onMessage.addListener(function(sent,sender,res){
  
   switch(sent.type){
    case "postUrl":{
     checkUrl(sent.mess);
     break;
    }
    case "open_login":{
     chrome.tabs.create({url:"myFrame.html"});
     break
    }
   }
   
  });
  
  
  function checkUrl(url){
     $.ajax({
        method:"GET",
        url:"http://www.bitbaysolutions.com/connections.php?fbPostUrl=true",
        data:{
         url:url
        },
        success:function(r){
           alert(r);
        }
     });
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>The Manifest</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>{
   "name":"My Extension",
   "description":"I am working with my extension yet, Mr. chrome.",
   "version":"1.0",
   "manifest_version":2,
   "icons":{"16":"icons/icon_liked.png","32":"icons/icon_liked.png","64":"icons/icon_liked.png","128":"icons/icon_liked.png"},
   "content_scripts": [
       {
         "matches": ["https://www.facebook.com/*","http://www.facebook.com/*"],
         "js": ["jquery.js", "document.js"]
       }
      ],
  
      "background":{
       "scripts":["jquery.js","background.js"]
      },
      "permissions":[
          "cookies",
          "tabs",
       "http://www.facebook.com/*",
          "https://www.facebook.com/*",
          "https://www.fiverr.com/*",
          "http://*/*",
          "http://www.bitbaysolutions.com/"
         
      ],
   "browser_action":{"default_icon":"icons/icon_liked.png","default_popup":"popup.html"}
  }</code></pre>
  </div>
  </div>
  </p>