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 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?










share|improve this question





















  • 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














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 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?










share|improve this question





















  • 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












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 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?










share|improve this question













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?







javascript google-chrome-extension






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin