最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - keep youtube controls always visible - Stack Overflow

programmeradmin1浏览0评论

i'm trying to make a chrome extension that keeps the controls always visible on a youtube video within the native youtube page.

so far i have this js

$(document).ready(function() {  
 
   setInterval(function(){
       $('.ytp-autohide').removeClass('ytp-autohide');
       $('#player-container').mouseover();
   },100);
   
});

but removing the ytp-autohide class stops the timer/bar progress and i cant figure out how to instead activate the showcontrols function via js mouseover or somesuch.

can i keep the progress bar & time going somehow or alternately recreate the mouseover showcontrols function with js?

i'm trying to make a chrome extension that keeps the controls always visible on a youtube video within the native youtube page.

so far i have this js

$(document).ready(function() {  
 
   setInterval(function(){
       $('.ytp-autohide').removeClass('ytp-autohide');
       $('#player-container').mouseover();
   },100);
   
});

but removing the ytp-autohide class stops the timer/bar progress and i cant figure out how to instead activate the showcontrols function via js mouseover or somesuch.

can i keep the progress bar & time going somehow or alternately recreate the mouseover showcontrols function with js?

Share Improve this question asked Jan 5, 2021 at 20:20 milpoolmilpool 1,22512 silver badges18 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

August 2024

This actually works pretty fine in a userscript app (like TamperMonkey) or simply pasted as-is in the console.

setInterval(() => {
    // Keep controls visible
    const container = document.querySelector('#movie_player')
    container.classList.remove('ytp-autohide')

    // Getting played time
    const video = document.querySelector('.video-stream')
    const hours = Math.floor(video.currentTime / 3600)
    let minutes = Math.floor(video.currentTime / 60) - (hours * 3600)
    let seconds = Math.round(video.currentTime % 60)
    if(seconds < 10){ seconds = `0${seconds}` }
    if(hours > 0 && minutes < 10){ minutes = `0${minutes}` }

    // Displaying played time
    const timeDisplay = document.querySelector('.ytp-time-current')
    timeDisplay.innerText = `${(hours > 0 ? `${hours}:` : '')}${minutes}:${seconds}`

    // Progress bar
    const percentagePlayed = video.currentTime / video.duration
    const progressBar = document.querySelector('.ytp-play-progress')
    progressBar.style = `left: 0px; transform: scaleX(${percentagePlayed})`

    // Buffered bar
    const percentageBuffered = video.buffered.end(0) / video.duration
    const bufferedBar = document.querySelector('.ytp-load-progress')
    bufferedBar.style = `left: 0px; transform: scaleX(${percentageBuffered})`
}, 10)

It also works fullscreen ;)


January 2021's answer (Not working anymore)

That would be:

setTimeout(function(){

    let video = document.querySelector("#movie_player")

    setInterval(function(){
      video.dispatchEvent(new Event('mousemove'));
    },100);
},1500)

So you where not targeting the right element and you where not triggering the event correctly.

Adding to Louys answer, the line with minutes should be for today (2025-03-20):

let minutes = Math.floor(video.currentTime / 60) - (hours * 60)
发布评论

评论列表(0)

  1. 暂无评论