Friday, 16 November 2018

Exactly when does the background script in a chrome extension get run?

In my chrome extension, I have a background script that will fetch some data that it will need using a XMLHttpRequest.

// note that this code is in the global scope i.e. outside of any function
// also note that I got this code from the page talking about XMLHttpRequest
var myData = []

var xhr = new XMLHttpRequest()
xhr.onreadystatechange = handleStateChange
xhr.open("GET", "...", true)
xhr.send()

function handleStateChange() {
    myData = Object.values(JSON.parse(xhr.responseText))
}

I want to know when will xor.send() be run.

I observed that every time I reload the extension by pressing on the enter image description here button, xhr.send() will be called. I also observed that opening a new tab/window doesn't cause the background script to be run again.

I also found this page, that the background page gets "loaded" and "unloaded", but it says very little about when the code in the global scope of the background script is run.

Does it only run when the extension is installed/reloaded?



from Exactly when does the background script in a chrome extension get run?

No comments:

Post a Comment