0

I wanted to build a Google Chrome extension that would display domain popularity, the way Alexa domain stats did;

I am able to display any data inside popup.html when user clicks my extension button (to show domain rank, backlinks, etc) but in background I need to track popular urls visited by users.

Using manifest v3, I was able (inside background.js) to detect and trigger code on each url change, but I cannot find a function that would allow the extension to ping my servers in order to tell what url/domain is visited by user.

I assume best way to do this is to make an URL get request in background. How can I do something like that ? Basically I want the extension to send a text message to an url of mine.

adrianTNT
  • 3,671
  • 5
  • 29
  • 35

1 Answers1

1

As wOxxOm mentioned you can use built in fetch API. As mentioned in this documentation, you can use fetch like this -

fetch(your_url, fetchParams) //fetch returns promise, that resolves to a response object
.then(response => response.json()) //assuming JSON response
.then(data => console.log(data))
.catch(err => console.error(err));

where fetchParam is typically a javascript object that follows the documentation mentioned above. you can add your custom data in the fetchParam's body key. like this

fetchParam = {
                method: your_metod,
                headers: your_header_object,
                body: your_data
            };

Hope I understood the question correctly.

  • Yes, that seems to do what I need. Can you please tell me how to use the variable `data` outside of that "arrow function" ? If I try to access `data` after this code, it is undefined. – adrianTNT Sep 06 '22 at 23:15
  • got, it, it can be passed to a user defined function with `.then(data => process_fetch_data(data))` – adrianTNT Sep 07 '22 at 08:51