Skip to content

Commit

Permalink
decrease miniplayer size, disable arrow keys after a navigation as well
Browse files Browse the repository at this point in the history
  • Loading branch information
glaucocustodio committed Aug 15, 2020
1 parent fb6b0df commit d742af3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Cancel default arrow (left and right) shortcuts when playing a video not in fullscreen and when miniplayer (picture-in-picture) is active
- Disable focus on links of the description
- Add shortcut to add current video to the queue (o)
- Decrease miniplayer size

### v0.0.10 (24/04/2020)
- Fix setting of default preferences
Expand Down
70 changes: 49 additions & 21 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,55 +52,82 @@ const setDarkMode = function() {
}
}

// this event has the purpose of cancelling the propagation of the keydown event
// when the following conditions are true
// this is necessary to cancel the default YT ArrowRight and ArrowLeft shortcuts when watching a video
// https://stackoverflow.com/a/35611393/1519240
document.addEventListener("keydown", function (event) {
window.addEventListener("ExtensionOptionsRead", function(event) {
window.extensionOptions = event.detail
})

const defineMiniplayerSize = function(){
if(miniPlayer) {
miniPlayer.style.setProperty('--ytd-miniplayer-width', '175px')
miniPlayer.style.setProperty('height', '435px')
miniPlayer.style.setProperty('--ytd-miniplayer-height', '88px')
}
}

// change the miniplayer size when it gets enabled
const miniPlayer = document.querySelector('ytd-miniplayer');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type == "attributes" && mutation.attributeName == 'enabled') {
defineMiniplayerSize()
}
});
});
if (miniPlayer) {
observer.observe(miniPlayer, {
attributes: true //configure it to listen to attribute changes
});
}

const cancelArrowHandler = function (event) {
let arrowRight = 39
let arrowLeft = 37
let watchPage = document.querySelector('ytd-app').attributes['is-watch-page']
// YT's picture-in-picture
let miniPlayerActive = document.querySelector('ytd-app').attributes['miniplayer-active_'] !== undefined
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

const focusIfFirefox = function() {
// this is ugly
if (isFirefox) {
SpatialNavigation.focus('ytd-player');
}
}
if ((miniPlayerActive || watchPage) && document.fullscreenElement == null) {
if (event.keyCode == arrowRight) {
event.stopPropagation();
SpatialNavigation.move('right')
SpatialNavigation.move('right');
} else if (event.keyCode == arrowLeft) {
event.stopPropagation();
// focusIfFirefox();
SpatialNavigation.move('left')

}
}
}, true);


window.addEventListener("ExtensionOptionsRead", function(event) {
window.extensionOptions = event.detail
})
}
const cancelArrowKeys = function() {
// this event has the purpose of cancelling the propagation of the keydown event
// when the following conditions are true
// this is necessary to cancel the default YT ArrowRight and ArrowLeft shortcuts when watching a video
// https://stackoverflow.com/a/35611393/1519240
document.addEventListener("keydown", cancelArrowHandler, true);
}

document.querySelector('ytd-app').addEventListener('yt-navigate-finish', function(e){
document.removeEventListener("keydown", cancelArrowHandler)
cancelArrowKeys()
runSpatial(true)
});

var setFocus = function(turnFullscreen = false){
var check = function(){
var video = document.querySelector('video');
var videoActive = video && video.src != "";
if(videoActive){
if(videoActive && !miniPlayer){
SpatialNavigation.focus('ytd-player');
window.scrollTo(0, 0);
if(turnFullscreen && window.extensionOptions.enterFullscreen){
document.documentElement.requestFullscreen();
}

// hack for Firefox
if(document.activeElement.tagName == 'BODY') {
setTimeout(function(){
check();
}, 300);
}
} else {
SpatialNavigation.focus('yc-initial');
}
Expand All @@ -110,6 +137,7 @@ var setFocus = function(turnFullscreen = false){

window.addEventListener('load', function() {
setDarkMode()
cancelArrowKeys()

window.addEventListener("keydown", function(event){
var letter_o = 79;
Expand Down

0 comments on commit d742af3

Please sign in to comment.