Chrome webNavigation.onComplete无法正常工作? [英] Chrome webNavigation.onComplete not working?
问题描述
我试图编写一个Chrome扩展程序,当用户使用视频在YouTube网页上时执行一些代码。据我所知,我的代码是正确的,但它不工作。
eventPage.js:
chrome.webNavigation.onCompleted.addListener(function(){
console.log(Test)
},{url:[{pathContains: watch,hostSuffix:youtube.com}]});
和我的清单文件
{
pre>
manifest_version:2,
name:youtubeExtension,
description:一个用于youtube的Chrome扩展程序,
版本:0.1,
权限:[https://www.youtube.com/,webNavigation],
背景 :{
scripts:[eventPage.js],
persistant:false
}
}
看起来onCompleted在youtube上无效。
解决方案
chrome.webNavigation.onCompleted
仅在文档加载完成时触发。当您导航到YouTube上的其他页面时,新页面并非以传统方式加载,而是页面被动态更新,并且URL被替换为history.pushState
。要检测这种导航,请使用chrome.webNavigation。 onHistoryStateUpdated
事件除onCompleted 事件外。
另一种方式要在YouTube页面上检测视频,请使用YouTube上的内容脚本和YouTube特定的某些事件,请参阅这个答案。
I'm trying to write a chrome extension that executes some code when the user is on a youtube page with a video. As far as I can tell, my code is correct, but It isn't working.
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 onCompleted doesn't work on youtube.
解决方案
chrome.webNavigation.onCompleted
is only triggered when a document has finished loading. When you navigate to a different page on YouTube, the new page is not really "loaded" in a traditional way, but the page is dynamically updated, and the URL is replaced usinghistory.pushState
. To detect this kind of "navigations", use thechrome.webNavigation.onHistoryStateUpdated
event in addition to theonCompleted
event.Another way to detect videos on YouTube pages is by using content scripts on YouTube and some event specific to YouTube, see this answer.
这篇关于Chrome webNavigation.onComplete无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!