executeScript "runAt" order ?

219 views
Skip to first unread message

patrick clancey

unread,
Nov 7, 2016, 6:36:04 AM11/7/16
to Chromium-Extensions-Announce
I have an extension that loads Zepto into the page and uses it in the content script. e.g. 
chrome.tabs.executeScript(tabId, {file: "lib/js/vendor/zepto.js", runAt: "document_start"});
chrome
.tabs.executeScript(tabId, {code: content_script, runAt: "document_idle"});

Where 'content_script' looks something like ...
(function($) {
   
if ($) {
     
...
   
} else {
      console
.log("extension: Zepto not found");
   
}
})(window.Zepto||window.$);

The issue is the extension works intermittently, working fine for a while, then "Zepto not found" in the console, then working again? I getting this behaviour using document_end so tried document_start, but same results. 

Any ideas on how to get this working consistently, gratefully received 
Thanks P

wOxxOm

unread,
Nov 7, 2016, 9:28:16 AM11/7/16
to Chromium-Extensions-Announce
Since executeScript is asynchronous as all of chrome API with a function callback, the correct approach is to chain-call executeScript with the second script in the callback of the first. Examples: https://stackoverflow.com/questions/21535233/injecting-multiple-scripts-through-executescript-in-google-chrome

patrick clancey

unread,
Nov 8, 2016, 4:13:09 AM11/8/16
to Chromium-Extensions-Announce
Great thanks. 

p
Reply all
Reply to author
Forward
0 new messages