Chrome webNavigation.onComplete无法正常工作? [英] Chrome webNavigation.onComplete not working?

查看:733
本文介绍了Chrome webNavigation.onComplete无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个Chrome扩展程序,当用户使用视频在YouTube网页上时执行一些代码。据我所知,我的代码是正确的,但它不工作。



eventPage.js:

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

和我的清单文件

  {
manifest_version:2,

name:youtubeExtension,
description:一个用于youtube的Chrome扩展程序,
版本:0.1,

权限:[https://www.youtube.com/,webNavigation],
背景 :{
scripts:[eventPage.js],
persistant:false
}
}
pre>

看起来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 using history.pushState. To detect this kind of "navigations", use the chrome.webNavigation.onHistoryStateUpdated event in addition to the onCompleted 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆