Skip to content

Commit

Permalink
Merge pull request #2421 from raszpl/patch-5
Browse files Browse the repository at this point in the history
Update shortcuts.js handlers rewrite, multiple fixes
  • Loading branch information
ImprovedTube authored Jun 30, 2024
2 parents e1c339d + bbeadcd commit 8510b2c
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 452 deletions.
29 changes: 27 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,35 @@ chrome.runtime.onInstalled.addListener(function (installed) {
if(installed.reason == 'update') {
// var thisVersion = chrome.runtime.getManifest().version;
// console.log("Updated from " + installed.previousVersion + " to " + thisVersion + "!");
// Shortcut renames:
chrome.storage.local.get(['shortcut_auto', 'shortcut_144p', 'shortcut_240p', 'shortcut_360p', 'shortcut_480p', 'shortcut_720p', 'shortcut_1080p', 'shortcut_1440p', 'shortcut_2160p', 'shortcut_2880p', 'shortcut_4320p'], function (result) {
// validate and move to new name
for (let [name, keys] of Object.entries(result)) {
if (!keys) continue;
let newKeys = {},
newName = name.replace('shortcut_', 'shortcut_quality_');
for (const button of ['alt','ctrl','shift','wheel','toggle']) {
if (keys[button]) newKeys[button] = keys[button];
}
if (keys['keys'] && Object.keys(keys['keys'])?.length) {
newKeys['keys'] = keys['keys'];
}
// only shortcuts with Key of Wheel are valid and saved
if (newKeys['keys'] || newKeys['wheel']) chrome.storage.local.set({[newName]: newKeys});
}
chrome.storage.local.remove(Object.keys(result));
});
chrome.storage.local.get(['shortcut_volume_step', 'shortcut_playback_speed_step'], function (result) {
for (let [name, value] of Object.entries(result)) {
let newName = name.replace('shortcut_', 'shortcuts_');
chrome.storage.local.set({[newName]: value});
}
chrome.storage.local.remove(Object.keys(result));
});
chrome.storage.local.get('player_autoplay', function (result) {
if (result.player_autoplay === false) {
chrome.storage.local.set({player_autoplay_disable: true});
chrome.storage.local.remove(['player_autoplay'], (i) => {});
chrome.storage.local.remove(['player_autoplay']);
}
});
chrome.storage.local.get('channel_default_tab', function (result) {
Expand All @@ -54,7 +79,7 @@ chrome.runtime.onInstalled.addListener(function (installed) {
chrome.storage.local.get('hideSubscribe', function (result) {
if (result.hideSubscribe === true) {
chrome.storage.local.set({subscribe: 'hidden'});
chrome.storage.local.remove(['hideSubscribe'], (i) => {});
chrome.storage.local.remove(['hideSubscribe']);
}
});
chrome.storage.local.get('limit_page_width', function (result) {
Expand Down
16 changes: 16 additions & 0 deletions js&css/web-accessible/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ var ImprovedTube = {
played_before_blur: false,
played_time: 0,
user_interacted: false,
input: {
listening: {},
listeners: {},
pressed: {
keys: [],
alt: false,
ctrl: false,
shift: false,
player: false,
wheel: 0
},
cancelled: []
},
mini_player__mode: false,
mini_player__move: false,
mini_player__cursor: '',
Expand Down Expand Up @@ -387,6 +400,9 @@ document.addEventListener('it-message-from-extension', function () {
break
}

// dont trigger shortcuts on config change, reinitialize handler instead
if (message.key.startsWith('shortcut_')) camelized_key = 'shortcuts';

if (ImprovedTube[camelized_key]) {
try{ImprovedTube[camelized_key]()}catch{};
}
Expand Down
47 changes: 37 additions & 10 deletions js&css/web-accessible/www.youtube.com/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ ImprovedTube.autoplayDisable = function (videoElement) {
// 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
// user didnt click
if (player && !this.user_interacted
// no ads playing
// no ads playing
&& !player.classList.contains('ad-showing')
// video page
// video page
&& ((location.href.includes('/watch?') // #1703
// player_autoplay_disable & not playlist
// player_autoplay_disable & not playlist
&& (this.storage.player_autoplay_disable && !location.href.includes('list='))
// !playlist_autoplay & playlist
// !playlist_autoplay & playlist
|| (this.storage.playlist_autoplay === false && location.href.includes('list=')))
// channel homepage & !channel_trailer_autoplay
// channel homepage & !channel_trailer_autoplay
|| (this.storage.channel_trailer_autoplay === false && this.regex.channel.test(location.href)))) {
setTimeout(function() { try { player.pauseVideo(); }
catch (error) { console.log("autoplayDisable: Pausing"); videoElement.pause(); }
});

setTimeout(function() {
try { player.pauseVideo(); } catch (error) { console.log("autoplayDisable: Pausing"); videoElement.pause(); }
});
} else {
document.dispatchEvent(new CustomEvent('it-play'));
}
Expand Down Expand Up @@ -80,7 +81,33 @@ ImprovedTube.playerAutoPip = function () {
}
})();
}
}
};
/*------------------------------------------------------------------------------
PLAYBACK SPEED
------------------------------------------------------------------------------*/
ImprovedTube.playbackSpeed = function (newSpeed) {
const video = this.elements.video,
player = this.elements.player,
speed = video?.playbackRate ? Number(video.playbackRate.toFixed(2)) : (player?.getPlaybackRate ? Number(player.getPlaybackRate().toFixed(2)) : null);

if (!speed) {
console.error('PlaybackSpeed: Cant establish playbackRate/getPlaybackRate');
return false;
}

// called with no option or demanded speed already set, only provide readback
if (!newSpeed || speed == newSpeed) return speed;

if (video?.playbackRate) {
video.playbackRate = newSpeed;
newSpeed = video.playbackRate;
} else if (player?.setPlaybackRate && player.getPlaybackRate) {
player.setPlaybackRate(newSpeed);
newSpeed = player.getPlaybackRate();
} else newSpeed = false;

return newSpeed;
};
/*------------------------------------------------------------------------------
FORCED PLAYBACK SPEED
------------------------------------------------------------------------------*/
Expand Down
Loading

0 comments on commit 8510b2c

Please sign in to comment.