How do I send an HTTP GET request from a Chrome extension?

前端 未结 1 927
粉色の甜心
粉色の甜心 2020-12-08 02:21

I\'m working on a chrome extension that sends an HTTP request using the method GET.

How do I send at www.example.com the parameter par with

相关标签:
1条回答
  • 2020-12-08 02:43

    First, you'll need to edit your manifest.json and add the permission for www.example.com:

    {
        "name": "My extension",
        ...
        "permissions": [
            "http://www.example.com/*"
        ],
        ...
    }
    

    Then in your background page (or somewhere else) you can do:

    fetch('http://www.example.com?par=0').then(r => r.text()).then(result => {
        // Result now contains the response text, do what you want...
    })
    

    Old (ES5) version using XMLHttpRequest:

    function callback() {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                result = xhr.responseText;
                // ...
            }
        }
    };
    
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://www.example.com?par=0", true);
    xhr.onreadystatechange = callback;
    xhr.send();
    

    For more information on this topic, see the relative documentation page.

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