google chrome extension - Inject multiple content scripts -
i have couple of extensions utilize multiple content scripts. adding these via manifest means aren't available when extension first installed (user must refresh content pages first), extension appears not work, , users un-install it!
have tried number of things can't find way of injecting multiple javascript files. has found way of doing this, or alerting user fact pages need refreshed before extension can run?
thanks
there no need refresh pages, utilize chrome.tabs.executescript
injecting old pages.
check working version of sample implementation.
demonstration manifest.jsonregistered background page , given permissions chrome extension.
{ "name": "inject code", "description": "http://stackoverflow.com/questions/14727263/inject-multiple-content-scripts", "manifest_version": 2, "version": "1", "permissions": [ "<all_urls>", "tabs" ], "background": { "scripts": [ "background.js" ] } }
background.js inject code using query api
document.addeventlistener("domcontentloaded", function () { //fetch inactive completly loaded tabs or utilize own query chrome.tabs.query({ active: false, "status": "complete" }, function (tabs) { (tab in tabs) { //it returns array of tabs chrome.tabs.executescript(tabs[tab].id, { "file": "inject.js" //inject code }); } }); });
inject.js console.log("injected..");
output you see script injected without need refresh of page.
references tab api google-chrome-extension
No comments:
Post a Comment