使用Chrome扩展程序内容脚本检测youtube视频事件 [英] Detect youtube video events with Chrome extension content script

查看:142
本文介绍了使用Chrome扩展程序内容脚本检测youtube视频事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Chrome扩展程序,我想检测用户在youtube.com上观看视频时视频何时开始/结束.我的情况与我已阅读的其他教程之间的区别是,我没有在自己的网站上嵌入自己的youtube视频,我只是想检测用户正在youtube上观看的视频中的事件.

I'm writing a Chrome extension and I want to detect when a video starts/ends while a user is on youtube.com watching videos. The difference between my situation and other tutorials I've read is I'm not embedding my own youtube video on my own site, I just want to detect events off of the video the user is watching on youtube.

现在,我的manifest.json文件中的内容脚本包含以下几行:

Right now my manifest.json file has the following lines for content scripts:

"content_scripts": [
    {
        "matches": ["http://*/*","https://*/*"],
        "js": ["js/jquery.js", "js/iframe_api.js", "js/contentScript.js"],
        "all_frames": true
    }
],

我下载了jquery,并且iframe_api.js是 https://www.youtube.com/iframe_api .我的contentScript.js文件非常简单,我只想在视频准备就绪后将一些内容打印到控制台:

I have jquery downloaded and iframe_api.js is the contents of https://www.youtube.com/iframe_api. My contentScript.js file is very simple, I just want to print something to the console when the video is ready:

function onYouTubeIframeAPIReady() {
    var player;
    player = new YT.Player('ytplayer', {
        events: {
            'onReady': onPlayerReady,
        }
    });
}

function onPlayerReady(event) {
    console.log('It worked!');
}

有人可以帮我吗?内容脚本可以做到这一点吗,还是因为内容脚本不在隔离环境"中,所以我不能访问youtube视频的iframe?

Could someone help me out? Can a content script even do this, or can I not access a youtube video's iframe because it isn't in the "isolated environment" that content scripts live in?

推荐答案

发布此问题后不久,我便找到了答案.不使用youtube iframe API,而是使用html5 API.我将内容脚本修改为以下内容,以便在视频结束并正常运行时打印一条消息.

Shortly after I posted this question I found out an answer. Not using the youtube iframe api, but rather the html5 api. I modified the content script to the following to print a message when the video ends and it works fine.

var vid = $('video').get(0);

vid.addEventListener('ended', function(e) {
    console.log('The video ended!');
});

这篇关于使用Chrome扩展程序内容脚本检测youtube视频事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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