javascript - 浏览 YouTube 视频时检测 Chrome 扩展程序
问题描述
我有一个 Chrome 扩展程序,可以记录视频在 YouTube 上播放的时间。它获取视频开始播放的开始时间和视频暂停的结束时间,然后显示秒数。但是,如果用户在播放视频时离开页面,我怎么能得到秒数呢?
清单.json:
{
"manifest_version": 2,
"name": "TimeTracker",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"*://www.youtube.com/*",
"webNavigation",
"tabs"
]
}
背景.js:
chrome.webNavigation.onHistoryStateUpdated.addListener(function (details) {
// If the URL after navigation is the video URL, execute timetracker.js
if (details.url.includes('watch?')) {
chrome.tabs.executeScript(null,{file:'timetracker.js'});
}
});
timetracker.js:
function findVideo(callback) {
var getVideo = setInterval(function() {
var video = document.querySelector('.html5-video-player video');
if (video != null) {
clearInterval(getVideo);
callback(video);
}
}, 50)
}
function trackPlayTime(video) {
var start = 0;
var end = 0;
// Get the start UTC after video starts playing
video.onplaying = function() {
start = Date.now();
};
// Get the end UTC after video is paused, alert start and end, reset variables
video.onpause = function() {
end = Date.now();
var seconds = (end - start) / 1000;
alert(`Seconds: ${seconds}`);
start = 0;
end = 0;
};
}
findVideo(trackPlayTime);
解决方案
推荐阅读
- python - 如何通过随机初始化权重从头开始训练 keras.applications 中给出的模型?
- c++ - 为什么 include 语句返回一个错误,基本上表明该语言 (C++) 存在错误?
- python - 在python datetime中,为什么第一个月'January' = 1,但'Monday',一周的第一天= 0?
- sql - 我无法让它按月显示信息。是 GROUP BY 还是我的 LOOP 搞砸了?
- python-3.x - python MVC,如何让函数不按按钮自动运行?
- javascript - 如何从 Twilio 函数将数据加载到 S3 存储桶中?
- java - 匕首/缺少绑定。没有@Provides-annotated 方法就不能提供输出器
- laravel - 身份验证登录不适用于 laravel 6 中的新用户
- sql - 如何在 SQL Server 中使用 CTE 显示每个销售代表每年的销售订单总数?
- javascript - 我有一张图片,当我将它拖到红色圆圈时它会复制。我想要它,所以它保留在复制的位置,而不是对齐到左上角