0

I want to make a chrome extension. I need to get the response JSON what can be seen in chrome's developer tools. It's a post by using fetch. Which API or javascript way can I use?

Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
天气君
  • 87
  • 2
  • 9
  • You can use the jquery AJAX for fetching the data from API – Rishab Nov 06 '19 at 06:59
  • Assuming you want to intercept the request made by a site in the tab, you can use chrome.debugger API ([example](/a/29845219)) or hook XMLHttpRequest in the page context ([example](https://medium.com/better-programming/chrome-extension-intercepting-and-reading-the-body-of-http-requests-dd9ebdf2348b)). – wOxxOm Nov 06 '19 at 07:04
  • What have you tried so far? Shouldn't a usual extension be written in JS, such that you can use the usual JS stuff? – Nico Haase Nov 06 '19 at 07:13

1 Answers1

0

Just use Fetch API (another good alternative is axios)

example:

async function getData() {
    try {
        const response = await fetch('https://your-res-website.com');
        const resJson = await response.json();

        return resJson;
    } catch (error) {
        console.warn('getData error', error);
    }

    return null;
}

getData().then(data => console.log(data));

More info about Fetch

zemil
  • 3,235
  • 2
  • 24
  • 33
  • Thank you,but I already tried this way to fetch straightly.It doesn't work.It's a big company's website,when i tried this way,it always response'status 500'.But in chrome, it's normal,so i try to get the message in chrome. – 天气君 Nov 06 '19 at 08:22
  • Try to copy success response as fetch in chrome and update according success fetch https://i.imgur.com/GSVSiPp.png – zemil Nov 06 '19 at 10:18
  • fetch is the standard way to make requests in js env so it doesn't matter web, chrome or node (in node js you can find isomorphic fetch or axios) – zemil Nov 06 '19 at 10:20
  • well,I tested that the request payload could only be used two times,it means whether I can straightly get the chrome 's response,or after the chrome's response I get the first time 's payload – 天气君 Nov 07 '19 at 02:51