Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update player.js faster more reliable autoplayDisable #2272

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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