Wednesday, 15 January 2014

javascript - Chrome extension how to inject my content script into all Facebook pages -



javascript - Chrome extension how to inject my content script into all Facebook pages -

my manifest looks this:

{ "manifest_version": 2, "name": "facebook extension", "version": "1.0", "browser_action": { "default_icon": "images/fbh.png", "default_popup": "fbh-popup.html" }, "permissions": [ "storage", "background", "tabs", "http://www.facebook.com/*", "https://www.facebook.com/*" ], "content_scripts": [ { "matches": [ "http://www.facebook.com/*", "https://www.facebook.com/*" ], "js": ["js/jquery-1.9.1.min.js", "js/fbh-main.js"], "css": ["css/fbh-main.css"], "run_at": "document_end" } ], "background":{ "persistent": true, "scripts": ["js/jquery-1.9.1.min.js","js/fbh-background.js"] } }

i want inject content script each time go new page within facebook. example, if click on user's profile want inject content script.

in background page have next listener:

chrome.tabs.onupdated.addlistener(function(tabid, changeinfo) { console.log(changeinfo); if (changeinfo.status === 'complete') { chrome.tabs.executescript(null, {file: "js/fbh-main.js"}); } });

there couple of problems this, however. first, function tries inject content script pages. need able read manifest permissions. second, if start @ www.facebook.com, content script it's supposed do. if, there, click profile "foo" , go www.facebook.com/foo, content script injected (as evidenced console.log message) not it's supposed do. think it's beingness injected because if navigate straight www.facebook.com/foo, content script loaded , thing.

is there improve way this?

try code

chrome.tabs.onupdated.addlistener(function(tabid, changeinfo,tab) { console.log(changeinfo); if ((tab.url.indexof("http://www.facebook.com/") !=-1 || tab.url.indexof("https://www.facebook.com/") !=-1) && changeinfo.status === 'complete') { chrome.tabs.executescript(tabid, {file: "js/fbh-main.js"}); } }); chrome.tabs.executescript(null, execute script in active tab of current window, may alter on user interaction, chrome.tabs.executescript(tabid ensure script injection in tab targeted. chrome.tabs.onupdated.addlistener fire every tab, added filter tab.url.indexof("https://www.facebook.com/") ensure script injected in face book pages.

share manifest ,content script , related files if problem still persist

javascript google-chrome-extension

No comments:

Post a Comment