0

Hi I am trying to automate a piece of code for Google extension to change Geolocation. I want it to work with Activate/Deactivate button as: When I remove the IF statement from "Content.js" it works fine but with IF statement it doesn't work. Please help.

Options.html

<button id="activate-it">Activate!</button>
<button id="deactivate-it">Deactivate!</button>

Background.Js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) 
     {      
           if (request.method == "getStatus") 
           {
            sendResponse({status: 'activate-it'});
           }
     });

Content.Js

chrome.runtime.sendMessage({method: "getStatus"}, function(response) 
{
 if (response.status == "activate-it") 
   {
      var lat = "28.22";
      var lon = "70.86";

      var script = document.createElement('script');
      script.innerHTML = 'navigator.geolocation.getCurrentPosition=function(a,b){a({coords:{latitude:'+lat+',longitude:'+ lon +'},timestamp:Date.now()})};var position={coords:{latitude:'+lat+',longitude:'+lon+'}};';
      document.head.insertBefore(script, document.head.firstChild);
   }

  else if (response.status!="activate-it")
  {
      var scripts = document.getElementsByTagName('script');

      for(var i = 0; i < scripts.length; i++)
      {
         if(scripts[i].text.indexOf("coords") !== -1)
         {
            scripts[i].text = scripts[i].text.replace(/position\.coords\.latitude/g, lat);
            scripts[i].text = scripts[i].text.replace(/position\.coords\.longitude/g, lon);
            scripts[i].text = scripts[i].text.replace(/coords\.latitude/g, lat);
            scripts[i].text = scripts[i].text.replace(/coords\.longitude/g, lon);
         }
      }

   }
});

1 Answers1

0

You have a SyntaxError in: sendResponse({status: 'activate-it']});

Change it to this:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) 
 {      
       if (request.method == "getStatus") 
       {
        sendResponse({status: 'activate-it'});
       }
 });
abraham
  • 46,583
  • 10
  • 100
  • 152
Sagi
  • 8,972
  • 3
  • 33
  • 41
  • doesn't work. I have figured the actual flow of the code but still stuck in the same thing. The "content.js" appears to be only working with "response.status" sent from "background.js". I am trying to put a switch on background.js but it isn't working for me. I have also updated the new changes. – John Bartz Sep 10 '15 at 19:54