【问题标题】:how to use mutationobserver with youtube spa technology in javascript如何在javascript中使用mutationobserver和youtube spa技术
【发布时间】: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


【解决方案1】:

您可以在video html 媒体元素上使用ontimeupdate。这样,您会在 currentTime 发生变化时收到通知。

您还可以使用document.querySelector('.ad-interrupting')检查广告是否正在播放

以下代码将在 5 秒后播放播放列表中的下一个元素:

const specificTime = 5; //play next after 5 seconds
const vid = document.querySelector("video");
vid.ontimeupdate = function onTimeUpdate(){
    var ad = document.querySelector('.ad-interrupting');
    if (!ad && vid.currentTime >= specificTime){
        console.log("playing next");
        document.querySelector(
            "#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > a.ytp-next-button.ytp-button"
        ).click();
    }
}

Tampermonkey 脚本示例:

// ==UserScript==
// @name         autoplay next video in playslist
// @version      0.1
// @description  play next video in playlist after X seconds
// @include      /https:\/\/www\.youtube\.com\/watch?.*list=PL9dIg-VwAsxZdZvuHjzTOP8-49CWpis17.*/
// ==/UserScript==

(function() {
    'use strict';

    const specificTime = 5; //play next after 5 seconds
    const vid = document.querySelector("video");
    vid.ontimeupdate = function onTimeUpdate(){
        var ad = document.querySelector('.ad-interrupting');
        if (!ad && vid.currentTime >= specificTime){
            console.log("playing next");
            document.querySelector(
                "#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > a.ytp-next-button.ytp-button"
            ).click();
        }
    }
})();

激活时匹配this playlist

【讨论】:

  • if (!ad && vid.currentTime >= specificTime && document.location.href == "https://www.youtube.com/watch?v=93qE6Z8qheA&list=PL9dIg-VwAsxZdZvuHjzTOP8-49CWpis17&index=5") 当我将字段名称添加到 if 条件时,它工作正常,谢谢
猜你喜欢
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
  • 2010-10-13
  • 1970-01-01
  • 2011-11-14
  • 2013-08-31
  • 2015-10-28
相关资源
最近更新 更多