繁体   English   中英

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

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

我正在尝试从chrome扩展名创建手动Google搜索。 html包含一个表单字段。 用户可以在此字段中输入数字。 然后,我将该数字添加到url参数中,并基于该参数发起GET请求。

这是我的清单文件

{
  "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"
  }
}

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

最后是我的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;
};

这似乎不起作用。 可能是什么原因? 没有错误消息记录到控制台。

Google不允许以编程方式访问Google。

XHR'ing毫无意义,将无法正常工作。 它还可以保护自己免受成帧的影响,因此将其加载到iframe中也不起作用。

除非有适用于Google即时的API,否则您必须先打开常规标签页才能执行此操作。 而且似乎没有一个

正如Xan所说,XHR'ing对我的问题毫无意义。 我对JS和chrome扩展还很陌生。

现在,我找到了这个api chrome.tabs.update 我只需要将URL传递给该api。 我删除了XMLHttpRequest()。

暂无
暂无

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

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