Exactly when does the background script in a chrome extension get run?
up vote
0
down vote
favorite
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 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?
javascript google-chrome-extension
add a comment |
up vote
0
down vote
favorite
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 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?
javascript google-chrome-extension
You're talking about the event page ("persistent":false in manifest.json) and it'll run every time any of its listeners is triggered (like chrome.runtime.onMessage etc) and on the start of the browser profile as well as on manual reloading of the extension which replicates the same.
– wOxxOm
Nov 10 at 13:15
@wOxxOm When one of the listeners is triggered, do you mean the listener will be run, or the whole script (global scope) will be run?
– Sweeper
Nov 10 at 13:17
The whole page will run and then the listener will run. You can observe this easily via console.log.
– wOxxOm
Nov 10 at 13:19
@wOxxOm I added aconsole.log("xxx")
in the global scope. I reloaded the extension and saw that "xxx" is logged. However, when my listeners trigger, I only observe them being run, not "xxx"
– Sweeper
Nov 10 at 13:54
Well I guess you did something wrong then - like didn't actually wait for the page to unload - because no code runs in vacuum, it runs in the page context which is created by running the page script(s) as a whole.
– wOxxOm
Nov 10 at 14:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 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?
javascript google-chrome-extension
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 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?
javascript google-chrome-extension
javascript google-chrome-extension
asked Nov 10 at 12:36
Sweeper
60.1k967134
60.1k967134
You're talking about the event page ("persistent":false in manifest.json) and it'll run every time any of its listeners is triggered (like chrome.runtime.onMessage etc) and on the start of the browser profile as well as on manual reloading of the extension which replicates the same.
– wOxxOm
Nov 10 at 13:15
@wOxxOm When one of the listeners is triggered, do you mean the listener will be run, or the whole script (global scope) will be run?
– Sweeper
Nov 10 at 13:17
The whole page will run and then the listener will run. You can observe this easily via console.log.
– wOxxOm
Nov 10 at 13:19
@wOxxOm I added aconsole.log("xxx")
in the global scope. I reloaded the extension and saw that "xxx" is logged. However, when my listeners trigger, I only observe them being run, not "xxx"
– Sweeper
Nov 10 at 13:54
Well I guess you did something wrong then - like didn't actually wait for the page to unload - because no code runs in vacuum, it runs in the page context which is created by running the page script(s) as a whole.
– wOxxOm
Nov 10 at 14:08
add a comment |
You're talking about the event page ("persistent":false in manifest.json) and it'll run every time any of its listeners is triggered (like chrome.runtime.onMessage etc) and on the start of the browser profile as well as on manual reloading of the extension which replicates the same.
– wOxxOm
Nov 10 at 13:15
@wOxxOm When one of the listeners is triggered, do you mean the listener will be run, or the whole script (global scope) will be run?
– Sweeper
Nov 10 at 13:17
The whole page will run and then the listener will run. You can observe this easily via console.log.
– wOxxOm
Nov 10 at 13:19
@wOxxOm I added aconsole.log("xxx")
in the global scope. I reloaded the extension and saw that "xxx" is logged. However, when my listeners trigger, I only observe them being run, not "xxx"
– Sweeper
Nov 10 at 13:54
Well I guess you did something wrong then - like didn't actually wait for the page to unload - because no code runs in vacuum, it runs in the page context which is created by running the page script(s) as a whole.
– wOxxOm
Nov 10 at 14:08
You're talking about the event page ("persistent":false in manifest.json) and it'll run every time any of its listeners is triggered (like chrome.runtime.onMessage etc) and on the start of the browser profile as well as on manual reloading of the extension which replicates the same.
– wOxxOm
Nov 10 at 13:15
You're talking about the event page ("persistent":false in manifest.json) and it'll run every time any of its listeners is triggered (like chrome.runtime.onMessage etc) and on the start of the browser profile as well as on manual reloading of the extension which replicates the same.
– wOxxOm
Nov 10 at 13:15
@wOxxOm When one of the listeners is triggered, do you mean the listener will be run, or the whole script (global scope) will be run?
– Sweeper
Nov 10 at 13:17
@wOxxOm When one of the listeners is triggered, do you mean the listener will be run, or the whole script (global scope) will be run?
– Sweeper
Nov 10 at 13:17
The whole page will run and then the listener will run. You can observe this easily via console.log.
– wOxxOm
Nov 10 at 13:19
The whole page will run and then the listener will run. You can observe this easily via console.log.
– wOxxOm
Nov 10 at 13:19
@wOxxOm I added a
console.log("xxx")
in the global scope. I reloaded the extension and saw that "xxx" is logged. However, when my listeners trigger, I only observe them being run, not "xxx"– Sweeper
Nov 10 at 13:54
@wOxxOm I added a
console.log("xxx")
in the global scope. I reloaded the extension and saw that "xxx" is logged. However, when my listeners trigger, I only observe them being run, not "xxx"– Sweeper
Nov 10 at 13:54
Well I guess you did something wrong then - like didn't actually wait for the page to unload - because no code runs in vacuum, it runs in the page context which is created by running the page script(s) as a whole.
– wOxxOm
Nov 10 at 14:08
Well I guess you did something wrong then - like didn't actually wait for the page to unload - because no code runs in vacuum, it runs in the page context which is created by running the page script(s) as a whole.
– wOxxOm
Nov 10 at 14:08
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239022%2fexactly-when-does-the-background-script-in-a-chrome-extension-get-run%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You're talking about the event page ("persistent":false in manifest.json) and it'll run every time any of its listeners is triggered (like chrome.runtime.onMessage etc) and on the start of the browser profile as well as on manual reloading of the extension which replicates the same.
– wOxxOm
Nov 10 at 13:15
@wOxxOm When one of the listeners is triggered, do you mean the listener will be run, or the whole script (global scope) will be run?
– Sweeper
Nov 10 at 13:17
The whole page will run and then the listener will run. You can observe this easily via console.log.
– wOxxOm
Nov 10 at 13:19
@wOxxOm I added a
console.log("xxx")
in the global scope. I reloaded the extension and saw that "xxx" is logged. However, when my listeners trigger, I only observe them being run, not "xxx"– Sweeper
Nov 10 at 13:54
Well I guess you did something wrong then - like didn't actually wait for the page to unload - because no code runs in vacuum, it runs in the page context which is created by running the page script(s) as a whole.
– wOxxOm
Nov 10 at 14:08