Created
June 18, 2013 23:37
-
-
Save acarabott/5810492 to your computer and use it in GitHub Desktop.
Inject multiple scripts on Chrome Extension pageAction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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