Skip to content

Instantly share code, notes, and snippets.

@acarabott
Created June 18, 2013 23:37
Show Gist options
  • Save acarabott/5810492 to your computer and use it in GitHub Desktop.
Save acarabott/5810492 to your computer and use it in GitHub Desktop.
Inject multiple scripts on Chrome Extension pageAction
function createSrcCall(tabId, src, callback) {
return function () {
if (callback !== undefined) {
chrome.tabs.executeScript(tabId, {file: src}, callback);
} else {
chrome.tabs.executeScript(tabId, {file: src});
}
};
}
function injectScripts(tabId, srcs) {
var i, callback;
srcs.reverse();
for (i = 0; i < srcs.length; i++) {
callback = createSrcCall(tabId, srcs[i], callback);
}
callback();
}
chrome.pageAction.onClicked.addListener(function (tab) {
// Scripts in execution order for cognitive ease, reversed in injectScripts
injectScripts(tab.id, ["jquery.min.js", "another.min.js", "final.js"]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment