Google Chrome Extension Http request

后端 未结 1 1242
旧时难觅i
旧时难觅i 2021-01-15 04:48

I\'d like to know if Google Chrome Extension can make a HTTP request and parse the body of the result (like Curl). For example, there is a server 1.2.3.4 that answers the q

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 05:19

    Yes, using Cross-Origin XMLHttpRequest. Set the permissions in the manifest and use it like

    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();
    

    0 讨论(0)
提交回复
热议问题