google-chromegoogle-chrome-extensiongoogle-chrome-devtoolsfirefox-addongoogle-chrome-app

Chrome extension, how to track each visited page?


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.


Solution

  • 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.