Skip to content

Commit

Permalink
Merge pull request #2272 from raszpl/patch-15
Browse files Browse the repository at this point in the history
Update player.js faster more reliable autoplayDisable
  • Loading branch information
ImprovedTube authored May 16, 2024
2 parents ae40d81 + e341388 commit 5aa518e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
7 changes: 5 additions & 2 deletions js&css/web-accessible/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,14 @@ ImprovedTube.playerOnPlay = function () {
this.removeEventListener('ended', ImprovedTube.playerOnEnded, true);
this.addEventListener('ended', ImprovedTube.playerOnEnded, true);

ImprovedTube.autoplayDisable(this);
ImprovedTube.playerLoudnessNormalization();
ImprovedTube.playerCinemaModeEnable();

return original.apply(this, arguments);
const returnValue = original.apply(this, arguments);

ImprovedTube.autoplayDisable(this);

return returnValue;
}
})(HTMLMediaElement.prototype.play);
};
Expand Down
58 changes: 28 additions & 30 deletions js&css/web-accessible/www.youtube.com/player.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
/*------------------------------------------------------------------------------
AUTOPLAY DISABLE
------------------------------------------------------------------------------*/
ImprovedTube.autoplayDisable = function (playerElement) {
if (ImprovedTube.storage.player_autoplay_disable
|| ImprovedTube.storage.playlist_autoplay === false
|| ImprovedTube.storage.channel_trailer_autoplay === false){
let player; let tries=0;
(function waitForPlayer(){if(player=ImprovedTube.elements.player||playerElement.closest('#movie_player')){return;}
ImprovedTube.autoplayDisable = function (videoElement) {
if (this.storage.player_autoplay_disable
|| this.storage.playlist_autoplay === false
|| this.storage.channel_trailer_autoplay === false) {
let player; let tries=0;
(function waitForPlayer(){if(player=this.elements.player||videoElement.closest('#movie_player')){return;}
else if(tries++<4){
console.log("autoplayOff is waiting for ImprovedTube.elements.player or #movie_player");
setTimeout(waitForPlayer,500);
}else if(tries===4){console.error("resigning autoplayOff after 1.5s")}
})()
if (ImprovedTube.video_url !== location.href) { this.user_interacted = false; }

// if (no user clicks) and (no ads playing) and
// ( there is a player and ( (it is not in a playlist and auto play is off ) or ( playlist auto play is off and in a playlist ) ) ) or (if we are in a channel and the channel trailer autoplay is off) )

// user didnt click
if (player && !this.user_interacted
// no ads playing
&& !player.classList.contains('ad-showing')
// video page
&& ((location.href.includes('/watch?') // #1703
// player_autoplay_disable & not playlist
&& (ImprovedTube.storage.player_autoplay_disable && !location.href.includes('list='))
// !playlist_autoplay & playlist
|| (ImprovedTube.storage.playlist_autoplay === false && location.href.includes('list=')))
// channel homepage & !channel_trailer_autoplay
|| (ImprovedTube.storage.channel_trailer_autoplay === false && ImprovedTube.regex.channel.test(location.href)))) {

if (!ImprovedTube.autoplayDeniedOnce) {
setTimeout(function () { player.pauseVideo(); });
ImprovedTube.autoplayDeniedOnce = true;
} else { player.pauseVideo();
console.log("autoplay:off - should we pause here again?");

if (this.video_url !== location.href) { this.user_interacted = false; }

// if (no user clicks) and (no ads playing) and
// ( there is a player and ( (it is not in a playlist and auto play is off ) or ( playlist auto play is off and in a playlist ) ) ) or (if we are in a channel and the channel trailer autoplay is off) )

// user didnt click
if (player && !this.user_interacted
// no ads playing
&& !player.classList.contains('ad-showing')
// video page
&& ((location.href.includes('/watch?') // #1703
// player_autoplay_disable & not playlist
&& (this.storage.player_autoplay_disable && !location.href.includes('list='))
// !playlist_autoplay & playlist
|| (this.storage.playlist_autoplay === false && location.href.includes('list=')))
// channel homepage & !channel_trailer_autoplay
|| (this.storage.channel_trailer_autoplay === false && this.regex.channel.test(location.href)))) {

videoElement.pause();
player.pauseVideo();
console.log("autoplayDisable: Pausing");
}
}
}
};
/*------------------------------------------------------------------------------
FORCED PLAY VIDEO FROM THE BEGINNING
Expand Down

0 comments on commit 5aa518e

Please sign in to comment.