【发布时间】:2021-06-15 09:58:11
【问题描述】:
我希望 youtube 播放列表中的视频在到达我想要的第二个视频时转到下一个视频。但是我不能一直检查location.href,我希望代码在再次打开相同的url时运行,因为我循环播放列表。
const counterEle = document.querySelector("video");
function generateCount(limit) {
mutation(160.510023);
if (counterEle.currentTime > limit) {
console.log("current > limit");
} else {
setTimeout(generateCount, 3000, limit);
setTimeout(mutation, 3000, limit);
}
}
let previousUrl = 'https://www.youtube.com/watch?v=93qE6Z8qheA&list=PL9dIg-VwAsxZdZvuHjzTOP8-49CWpis17&index=5';
const observer = new MutationObserver(mutation);
function mutation(limit) {
generateCount(160.510023);
if (location.href == previousUrl) {
console.log(`URL changed to ${location.href}`);
if (counterEle.currentTime > limit) {
document
.querySelector(
"#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > a.ytp-next-button.ytp-button"
)
.click();
} else {
setTimeout(generateCount, 3000);
}
} else {
console.log("!=");
}
}
我尝试了很多方法,我做错了什么?
(代码运行没有问题,只是检查域名有问题)
【问题讨论】:
-
试试这个:不要检查视频是否达到给定时间(即
if (counterEle.currentTime > limit) {),而是用setTimeout(<your_method>, <set_milliseconds_here>);调用你的方法mutation并存储播放新视频时播放列表的 url,然后循环这个新数组并检查 url 是否已添加到数组中 - 添加您需要的逻辑。
标签: javascript performance url youtube single-page-application