google chrome extension - Content Script pattern patch confusion -
i'm trying write first chrome extension , can't content script loading correctly. load home page of tube (ie, https://www.youtube.com/); however, not load other page, example, after user searches (ie, https://www.youtube.com/results?search_query=programming). here have:
"content_scripts": [ { "matches": ["*://*.youtube.com/*"], "exclude_matches": ["*://*.youtube.com/"], "js": ["jquery.js", "content.js"] }
]
using above code, content.js doesn't load @ all; however, if take out "exclude_matches", content script loads on https://www.youtube.com/.
currently manifest includes all of youtube pages except main page.
the following include main page:
"content_scripts": [ { "matches": ["*://www.youtube.com/"], "js": ["jquery.js", "content.js"] } ]
however youtube uses history api navigation means if user first opened video page , navigated main page content script won't injected automatically. need use chrome.webnavigation.onhistorystateupdated event handler url filters:
chrome.webnavigation.onhistorystateupdated.addlistener( function(details) { var tabid = details.tabid; chrome.tabs.executescript(tabid, {file: "jquery.js", runat: "document_start"}, function() { chrome.tabs.executescript(tabid, {file: "content.js", runat: "document_start"}); }); }, { url: [ {urlequals: "https://www.youtube.com/"} ] } );
and you'll need handler remove effect of content scripts when user navigates main page. can implemented pagehide listener in content script or using (another) onhistorystateupdated
listener.
alternatively can have scripts on of youtube , check whether current url of home page in content script. might useful in case script injection onhistorystateupdated
happens late , see delay between navigation , subsequent applying of content scripts.
Comments
Post a Comment