Detect youtube video events with Chrome extension content script

人走茶凉 提交于 2019-12-04 07:35:38

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!');
});

I found a similar questions here, the answers are great (HTML5 is amazing!!) How to play and pause a Youtube video in the console?

for instance, you can use these to play/pause the video (on youtube.com, without youtube iframe on an external website)

document.getElementsByTagName('video')[0].play()
document.getElementsByTagName('video')[0].pause()

there are more functions allowing you to track video length, current play time and more.

P.S: I was having this same question for over a day and it's surprisingly to hard to find the right answer/guide to this! but hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!