javascript - Chrome extension events and nested callbacks -


i've seen problem few diffferent times, , think there's scope rule i've misunderstood.

in popup html have button:

<button id="submit">click me</button> 

and further down on page declare <script>s src files. 1 script contains:

document.getelementbyid("submit").onclick=function(){     var response = function(tab){         alert('success ' + tab.id);     };     chrome.tabs.create({url: determineurl()}, response); }; 

if open popup , click button, new tab opens determineurl(), alert never executes. however, if inspect popup, keeping open while new tab opens , loads, alert does execute.

the callback not called because popup page closes chronologically before callback can fire. consider these 2 assertions:

  • creating new tab chrome.tabs.create closes open extension popup

  • the chrome.tabs.create callback fires after tab created

  • it not possible page not loaded run scripts.

the script running in popup page therefore goes this:

  1. the script in popup create tab

  2. popup forced close, due new tab opening

  3. the script in popup causes alert

note step 3 impossible (according third assertion, above), long step 2 true. using inspection mode make popup persistent , prevents step 2 occurring.


Comments