0

I am trying to create a manual Google search from chrome extension. The html contains a form field. The user can enter a number to this field. Then I add that number into a url parameter and initiate a GET request based on that.

Here is my manifest file

{
  "manifest_version": 2,

  "name": "Qoogle",
  "description": "Customized google search for your daily needs",
  "version": "1.0",
  "permissions": [
          "tabs",
          "http://www.google.co.in/",
          "https://www.google.co.in/"
        ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

This is my html that is opened in a new tab when clicked on the extension button.

<form action="" name="timer">
            <input type="number" name="quantity" id="time">
            <input type="radio" name="timertype" id="t1" value="seconds" checked>Seconds
            <input type="radio" name="timertype" id="t2" value="minutes">Minutes
            <br>
            <input type="submit" id="timer" value="Set Timer">
            *<b>Enter any positive number</b>
        </form>
<script src="validate.js"></script>

And finally this is my validate.js

//Method for new http request
function httpGet(theUrl)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false );0
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        // do stuff here
        xmlHttp.send(null);
        return xmlHttp.responseText;
        };
    }
}

//Funtion to validate and send timer request
function timer() {

    var value = document.getElementById('time').value;
    var url;

    if(document.getElementById('t1').checked) {
        url = "https://www.google.co.in/#q=set+timer+for+" + value + "+seconds";
        httpGet(url)
    }
    else{
        url = "https://www.google.co.in/#q=set+timer+for+" + value + "+minutes";
        httpGet(url)
    }
}

window.onload = function(){
    document.getElementById('timer').onclick = timer;
};

This doesn't seem to be working. What could be the reason? No error messages are logged to the console.

Anonymous Platypus
  • 1,242
  • 4
  • 18
  • 47

2 Answers2

2

Google does not tolerate being programmatically accessed.

XHR'ing it is pointless and will not work. It is also protecting itself against framing, so loading it in an iframe will not work either.

Unless there is an API for Google Now, you can't do this without opening a regular tab. And there does not seem to be one.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
2

As Xan mentioned, XHR'ing was pointless in my problem. I was fairly new to JS and chrome extensions.

Now I found this api chrome.tabs.update. I simply had to pass the url to that api. I removed the XMLHttpRequest().

Anonymous Platypus
  • 1,242
  • 4
  • 18
  • 47