Chrome webNavigation.onComplete not working?

I am trying to write a chrome extension that executes some code when a user is on a youtube page with a video. As far as I can tell, my code is correct, but it does not work.

eventPage.js:

chrome.webNavigation.onCompleted.addListener(function(){ console.log("Test") },{url: [{pathContains: "watch", hostSuffix: "youtube.com"}]}); 

and my manifest file

 { "manifest_version": 2, "name": "youtubeExtension", "description": "A chrome extension for youtube", "version": "0.1", "permissions": ["https://www.youtube.com/", "webNavigation"], "background": { "scripts": ["eventPage.js"], "persistant": false } } 

It seems that onCompleted does not work on youtube.

0
source share
2 answers

chrome.webNavigation.onCompleted only starts when the download of the document is complete. When you switch to another page on YouTube, the new page does not actually load in the traditional way, but the page is dynamically updated and the URL is replaced using history.pushState . To detect this type of "navigation", use the chrome.webNavigation.onHistoryStateUpdated event in addition to the onCompleted event.

Another way to detect videos on YouTube pages is to use content scripts on YouTube and some YouTube-specific events, see this answer .

+2
source

Instead of using web navigation you can also use

Add this to your background.js

 chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { if (changeInfo.status == 'complete') { if (tab.url.indexOf("youtube.com") != -1) { alert("Youtube load complete"); injectScripts(); } } }); 

and add the following to your mainfest.json

 "permissions": ["https://www.youtube.com/", "webNavigation","tabs"] 

It will work just as well.

+1
source

Source: https://habr.com/ru/post/1210122/


All Articles