Skip to content

Commit

Permalink
Merge pull request #1670 from raszpl/raszpl-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ImprovedTube authored May 2, 2023
2 parents 73cbd81 + c225de2 commit c36bb06
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 119 deletions.
100 changes: 78 additions & 22 deletions js&css/web-accessible/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,37 @@ var ImprovedTube = {
defaultApiKey: 'AIzaSyCXRRCFwKAXOiF1JkUBmibzxJF1cPuKNwA'
};

/*--------------------------------------------------------------
CODEC || 30FPS
----------------------------------------------------------------
Do not move, needs to be on top of first injected content
file to patch HTMLMediaElement before YT player uses it.
--------------------------------------------------------------*/
if (localStorage['it-codec'] || localStorage['it-player30fps']) {
function overwrite(self, callback, mime) {
if (localStorage['it-codec']) {
var re = new RegExp(localStorage['it-codec']);
// /webm|vp8|vp9/av01/
if (re.test(mime)) return '';
}
if (localStorage['it-player30fps']) {
var match = /framerate=(\d+)/.exec(mime);
if (match && match[1] > 30) return '';
}
return callback.call(self, mime);
}

if (window.MediaSource) {
var isTypeSupported = window.MediaSource.isTypeSupported;
window.MediaSource.isTypeSupported = function (mime) {
return overwrite(this, isTypeSupported, mime);
}
}
var canPlayType = HTMLMediaElement.prototype.canPlayType;
HTMLMediaElement.prototype.canPlayType = function (mime) {
return overwrite(this, canPlayType, mime);
}
};

/*--------------------------------------------------------------
# MESSAGES
Expand Down Expand Up @@ -120,15 +151,40 @@ document.addEventListener('it-message-from-extension', function () {

if (message.action === 'storage-loaded') {
ImprovedTube.storage = message.storage;

if (ImprovedTube.storage.player_h264) {
localStorage['it-codec'] = "/webm|vp8|vp9|av01/";
} else {
localStorage.removeItem('it-codec');
}
if (!ImprovedTube.storage.player_60fps) {
localStorage['it-player30fps'] = true;
} else {
localStorage.removeItem('it-player30fps');
}

// FEEDBACK WHEN THE USER CHANGED A SETTING
ImprovedTube.init();
} else if (message.action === 'storage-changed') {
ImprovedTube.init();
} else if (message.action === 'storage-changed') {
var camelized_key = message.camelizedKey;

ImprovedTube.storage[message.key] = message.value;
if(message.key==="player_h264"){
if (ImprovedTube.storage.player_h264) {
localStorage['it-codec'] = "/webm|vp8|vp9|av01/";
} else {
localStorage.removeItem('it-codec');
}
}
if(message.key==="player_60fps"){
if (!ImprovedTube.storage.player_60fps) {
localStorage['it-player30fps'] = true;
} else {
localStorage.removeItem('it-player30fps');
}
}
if(ImprovedTube.storage[message.key]==="when_paused"){
ImprovedTube.whenPaused();
ImprovedTube.whenPaused();
};
if (camelized_key === 'blacklistActivate') {
camelized_key = 'blacklist';
Expand All @@ -138,27 +194,27 @@ document.addEventListener('it-message-from-extension', function () {
ImprovedTube.myColors();
ImprovedTube.setTheme();
} else if (camelized_key === 'description') {
if (ImprovedTube.storage.description === "expanded" || ImprovedTube.storage.description === "classic_expanded" )
{try{document.querySelector("#more").click() || document.querySelector("#expand").click() ;} catch{} }
if (ImprovedTube.storage.description === "normal" || ImprovedTube.storage.description === "classic" )
{try{document.querySelector("#less").click() || document.querySelector("#collapse").click();} catch{}}
if (ImprovedTube.storage.description === "expanded" || ImprovedTube.storage.description === "classic_expanded" )
{try{document.querySelector("#more").click() || document.querySelector("#expand").click() ;} catch{} }
if (ImprovedTube.storage.description === "normal" || ImprovedTube.storage.description === "classic" )
{try{document.querySelector("#less").click() || document.querySelector("#collapse").click();} catch{}}
ImprovedTube.improvedtubeYoutubeButtonsUnderPlayer();
}
else if (camelized_key === 'transcript') {
if (ImprovedTube.storage.transcript === true) {try{document.querySelector('*[target-id*=transcript]').removeAttribute('visibility');}catch{}
}
else if (camelized_key === 'transcript') {
if (ImprovedTube.storage.transcript === true) {try{document.querySelector('*[target-id*=transcript]').removeAttribute('visibility');}catch{}
} if (ImprovedTube.storage.transcript === false){try{document.querySelector('*[target-id*=transcript] #visibility-button button').click();}catch{}}
}
else if (camelized_key === 'chapters') {
if (ImprovedTube.storage.chapters === true){try{document.querySelector('*[target-id*=chapters]').removeAttribute('visibility');}catch{}
}
else if (camelized_key === 'chapters') {
if (ImprovedTube.storage.chapters === true){try{document.querySelector('*[target-id*=chapters]').removeAttribute('visibility');}catch{}
} if (ImprovedTube.storage.chapters === false){try{document.querySelector('*[target-id*=chapters] #visibility-button button').click();}catch{}}
}
else if (camelized_key === 'commentsSidebar') {
if(ImprovedTube.storage.comments_sidebar === false)
{document.querySelector("#below").appendChild(document.querySelector("#comments"));
document.querySelector("#secondary").appendChild(document.querySelector("#related")); }
}
else if (camelized_key === 'commentsSidebar') {
if(ImprovedTube.storage.comments_sidebar === false)
{document.querySelector("#below").appendChild(document.querySelector("#comments"));
document.querySelector("#secondary").appendChild(document.querySelector("#related")); }
else{ImprovedTube.commentsSidebar();}
}
}

if (ImprovedTube[camelized_key]) {
try{ImprovedTube[camelized_key]()}catch{};
}
Expand All @@ -175,7 +231,7 @@ document.addEventListener('it-message-from-extension', function () {
} else if (message.pause === true) {
if (ImprovedTube.elements.player) {
ImprovedTube.played_before_blur = ImprovedTube.elements.player.getPlayerState() === 1;
ImprovedTube.elements.player.pauseVideo();
ImprovedTube.elements.player.pauseVideo();
}
} else if (message.hasOwnProperty('setVolume')) {
if (ImprovedTube.elements.player) {
Expand Down Expand Up @@ -223,4 +279,4 @@ ImprovedTube.messages.send = function (message) {

document.dispatchEvent(new CustomEvent('it-message-from-youtube'));
}
};
};
2 changes: 0 additions & 2 deletions js&css/web-accessible/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ ImprovedTube.init = function () {
window.addEventListener('yt-player-updated', yt_player_updated);

this.playerOnPlay();
this.playerH264();
this.player60fps();
this.playerSDR();
this.shortcuts();
this.onkeydown();
Expand Down
120 changes: 25 additions & 95 deletions js&css/web-accessible/www.youtube.com/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ ImprovedTube.autoplay = function () {
)
) {
setTimeout(function () {console.log("autoplayyOFFFF");
video.pauseVideo();
});
video.pauseVideo();
});
}
};
/*------------------------------------------------------------------------------
Expand Down Expand Up @@ -49,15 +49,15 @@ AUTO PIP WHEN SWITCHING TABS
------------------------------------------------------------------------------*/
ImprovedTube.playerAutoPip = function () {
const video = ImprovedTube.elements.video;

if (this.storage.player_autoPip === true && video) {
(async () => {
try {
await video.requestPictureInPicture();
} catch (error) {
console.error('Failed to enter Picture-in-Picture mode', error);
}
})();
})();
}
};
/*------------------------------------------------------------------------------
Expand All @@ -84,8 +84,8 @@ ImprovedTube.playerPlaybackSpeed = function (change) {
if (player.getVideoData().isLive === false &&
(this.storage.player_force_speed_on_music === true ||
document.querySelector('h3#title')?.innerText !== 'Music' // (=buyable/registered music table)
|| (
(ImprovedTube.elements.category !== 'Music' && !/official (music )?video|lyrics|cover[\)\]]|[\(\[]cover|cover version|karaok|(sing|play)[- ]?along|卡拉OK|卡拉OK|الكاريوكيкараоке|カラオケ|노래방/i.test(ImprovedTube.elements.title + ImprovedTube.elements.keywords)
|| (
(ImprovedTube.elements.category !== 'Music' && !/official (music )?video|lyrics|cover[\)\]]|[\(\[]cover|cover version|karaok|(sing|play)[- ]?along|卡拉OK|卡拉OK|الكاريوكيкараоке|カラオケ|노래방/i.test(ImprovedTube.elements.title + ImprovedTube.elements.keywords)
) || /do[ck]u|interv[iyj]|back[- ]?stage|インタビュー|entrevista|面试|面試|회견|wawancara|مقابلة|интервью|entretien|기록한 것|记录|記錄|ドキュメンタリ|وثائقي|документальный/i.test(ImprovedTube.elements.keywords + ImprovedTube.elements.title)
) // && location.href.indexOf('music') === -1 // (=only running on www.youtube.com anyways)
)) {
Expand All @@ -98,7 +98,7 @@ ImprovedTube.playerPlaybackSpeed = function (change) {
}, intervalMs);
};
/*------------------------------------------------------------------------------
SUBTITLES
SUBTITLES
------------------------------------------------------------------------------*/
ImprovedTube.subtitles = function () {
if (this.storage.player_subtitles === true) {
Expand All @@ -108,7 +108,7 @@ ImprovedTube.subtitles = function () {
player.toggleSubtitlesOn();
}
}
};
};
/*------------------------------------------------------------------------------
SUBTITLES LANGUAGE
------------------------------------------------------------------------------*/
Expand All @@ -129,7 +129,7 @@ ImprovedTube.subtitlesLanguage = function () {
if( false === tracklist[i].vss_id.includes("a.") || true === this.storage.auto_generate){
this.elements.player.setOption('captions', 'track', tracklist[i]);
matchTrack = true; break;
}
}
}
}
// if (!matchTrack){ player.toggleSubtitles(); }
Expand Down Expand Up @@ -348,18 +348,18 @@ ImprovedTube.playerAds = function (parent) {
try{var button = parent.querySelector('.ytp-ad-skip-button.ytp-button');}catch{}
if (button) {
if (this.storage.player_ads === 'block_all') {
button.click();
button.click();
} else if (this.storage.player_ads === 'subscribed_channels') {
if (!parent.querySelector('#meta paper-button[subscribed]')) {
button.click();
button.click();
}
} else if (this.storage.player_ads === 'block_music') {
if (ImprovedTube.elements.category === 'music') {
button.click();
button.click();
}
}
}
};
}
};
/*------------------------------------------------------------------------------
AUTO FULLSCREEN
------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -394,76 +394,6 @@ ImprovedTube.playerQuality = function () {
}
};
/*------------------------------------------------------------------------------
CODEC H.264
------------------------------------------------------------------------------*/
ImprovedTube.playerH264 = function () {
if (this.storage.player_h264 === true) {
var canPlayType = HTMLMediaElement.prototype.canPlayType;

function overwrite(self, callback, mime) {
if (/webm|vp8|vp9/.test(mime)) {
return false;
} else {
return callback.call(self, mime);
}
}

if (window.MediaSource) {
var isTypeSupported = window.MediaSource.isTypeSupported;

window.MediaSource.isTypeSupported = function (mime) {
return overwrite(this, isTypeSupported, mime);
};
}

HTMLMediaElement.prototype.canPlayType = function (mime) {
var status = overwrite(this, canPlayType, mime);

if (!status) {
return '';
} else {
return status;
}
};
}
};
/*------------------------------------------------------------------------------
ALLOW 60FPS
------------------------------------------------------------------------------*/
ImprovedTube.player60fps = function () {
if (this.storage.player_60fps === false) {
var canPlayType = HTMLMediaElement.prototype.canPlayType;

function overwrite(self, callback, mime) {
var match = /framerate=(\d+)/.exec(mime);

if (match && match[1] > 30) {
return '';
} else {
return callback.call(self, mime);
}
}

if (window.MediaSource) {
var isTypeSupported = window.MediaSource.isTypeSupported;

window.MediaSource.isTypeSupported = function (mime) {
return overwrite(this, isTypeSupported, mime);
};
}

HTMLMediaElement.prototype.canPlayType = function (mime) {
var status = overwrite(this, canPlayType, mime);

if (!status) {
return '';
} else {
return status;
}
};
}
};
/*------------------------------------------------------------------------------
FORCED VOLUME
------------------------------------------------------------------------------*/
ImprovedTube.playerVolume = function () {
Expand Down Expand Up @@ -537,7 +467,7 @@ ImprovedTube.screenshot = function () {

cvs.toBlob(function (blob) {
if (ImprovedTube.storage.player_screenshot_save_as !== 'clipboard') {
var a = document.createElement('a');
var a = document.createElement('a');
a.href = URL.createObjectURL(blob); console.log("screeeeeeenshot tada!");

a.download = location.href.match(/(\?|\&)v=[^&]+/)[0].substr(3) || location.href.match(/()embed\/[^&]+/)[0].substr(3) || improvedTube.videoID || location.href.match + '-' + new Date(ImprovedTube.elements.player.getCurrentTime() * 1000).toISOString().substr(11, 8).replace(/:/g, '-') + '.png';
Expand Down Expand Up @@ -728,21 +658,21 @@ ImprovedTube.playerControls = function (pause=false) {
player.hideControls();
} else if(hide === 'off') {
player.showControls();
} else if(hide === 'when_paused') {
if(this.elements.video.paused){
player.hideControls( );
ImprovedTube.elements.player.parentNode.addEventListener('mouseenter', function () {
} else if(hide === 'when_paused') {
if(this.elements.video.paused){
player.hideControls( );

ImprovedTube.elements.player.parentNode.addEventListener('mouseenter', function () {
player.showControls();});
ImprovedTube.elements.player.parentNode.addEventListener('mouseleave', function () {
ImprovedTube.elements.player.parentNode.addEventListener('mouseleave', function () {
player.hideControls( );});

ImprovedTube.elements.player.parentNode.onmousemove = (function() {

ImprovedTube.elements.player.parentNode.onmousemove = (function() {
let onmousestop = function() {
player.hideControls( );
}, thread;

return function() {
player.showControls();
clearTimeout(thread);
Expand All @@ -767,7 +697,7 @@ ImprovedTube.mini_player__setSize = function (width, height, keep_ar, keep_area)
}

ImprovedTube.elements.player.style.width = width + 'px';
ImprovedTube.elements.player.style.height = height + 'px';
ImprovedTube.elements.player.style.height = height + 'px';
};

ImprovedTube.miniPlayer_scroll = function () {
Expand Down

0 comments on commit c36bb06

Please sign in to comment.