【问题标题】:Google Chrome Extension Http request谷歌浏览器扩展 Http 请求
【发布时间】:2012-07-15 10:35:12
【问题描述】:

我想知道 Google Chrome 扩展程序是否可以发出 HTTP 请求并解析结果的正文(如 Curl)。例如,有一个服务器 1.2.3.4 通过汇总 URL 参数来回答问题?a=1&b=2。查询"http://1.2.3.4?a=1&b=2" 将返回一个包含3 的正文,而我的扩展想要提交这样的查询并解析结果。

任何帮助将不胜感激。

【问题讨论】:

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

是的,使用Cross-Origin XMLHttpRequest

  1. manifest.json中设置permissions

  2. 然后在你的扩展页面中这样使用它:

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://api.example.com/data.json", true);
    xhr.onreadystatechange = function() {
      if (xhr.readyState == 4) {
        // WARNING! Might be injecting a malicious script!
        document.getElementById("resp").innerHTML = xhr.responseText;
        ...
      }
    }
    xhr.send();
    

注意事项:

  • 内容脚本无法在现代 Chrome 中发出跨域请求,more info
  • ManifestV3 后台脚本没有 XMLHttpRequest,因此您将在那里使用fetch

【讨论】:

  • 谢谢,你帮了我很多!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 2014-07-24
  • 2016-06-08
  • 2012-04-21
相关资源
最近更新 更多