简体   繁体   English

无法从Chrome扩展程序发送http请求

[英]unable to send an http request from chrome extension

I am trying to create a manual Google search from chrome extension. 我正在尝试从chrome扩展名创建手动Google搜索。 The html contains a form field. html包含一个表单字段。 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. 然后,我将该数字添加到url参数中,并基于该参数发起GET请求。

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. 单击扩展按钮时,这是在新标签页中打开的html。

<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 最后是我的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. 没有错误消息记录到控制台。

Google does not tolerate being programmatically accessed. Google不允许以编程方式访问Google。

XHR'ing it is pointless and will not work. XHR'ing毫无意义,将无法正常工作。 It is also protecting itself against framing, so loading it in an iframe will not work either. 它还可以保护自己免受成帧的影响,因此将其加载到iframe中也不起作用。

Unless there is an API for Google Now, you can't do this without opening a regular tab. 除非有适用于Google即时的API,否则您必须先打开常规标签页才能执行此操作。 And there does not seem to be one . 而且似乎没有一个

As Xan mentioned, XHR'ing was pointless in my problem. 正如Xan所说,XHR'ing对我的问题毫无意义。 I was fairly new to JS and chrome extensions. 我对JS和chrome扩展还很陌生。

Now I found this api chrome.tabs.update . 现在,我找到了这个api chrome.tabs.update I simply had to pass the url to that api. 我只需要将URL传递给该api。 I removed the XMLHttpRequest(). 我删除了XMLHttpRequest()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM