Disable Youtube PiP Miniplayer

Prevents Youtube from keep playing videos in PiP/miniplayer

  1. // ==UserScript==
  2. // @name Disable Youtube PiP Miniplayer
  3. // @namespace disableMPYTxFIRKx
  4. // @description Prevents Youtube from keep playing videos in PiP/miniplayer
  5. // @version 0.87
  6. // @author xFIRKx
  7. // @match http://*.youtube.com/*
  8. // @match https://*.youtube.com/*
  9. // @homepageURL https://greasyfork.org/it/scripts/493793-disable-youtube-pip-miniplayer
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. document.body.addEventListener("yt-navigate-finish", function(event) {
  15. if (document.getElementsByTagName('ytd-miniplayer').length) {
  16. document.querySelector('ytd-miniplayer').parentNode.removeChild(document.querySelector('ytd-miniplayer'));
  17. }
  18. if (document.getElementsByClassName('ytp-miniplayer-button').length) {
  19. document.querySelector('.ytp-miniplayer-button').parentNode.removeChild(document.querySelector('.ytp-miniplayer-button'))
  20. }
  21. if (window.location.pathname != "/watch") {
  22. document.querySelector('#movie_player video').parentNode.removeChild(document.querySelector('#movie_player video'));
  23. }
  24. });
  25. })();
  26. (() => {
  27. 'use strict'
  28. // credit: https://stackoverflow.com/a/46428962
  29. let oldHref = document.location.href
  30. window.onload = () => {
  31. let bodyList = document.querySelector('body')
  32. let observer = new MutationObserver(ms => {
  33. ms.forEach(_m => {
  34. if (oldHref != document.location.href) {
  35. oldHref = document.location.href
  36. // allow some delay for page to load.
  37. setTimeout(() => {
  38. const jq = $('#movie_player > div.ytp-miniplayer-ui > div > button.ytp-miniplayer-close-button.ytp-button')
  39. if (jq.length && !document.location.href.startsWith('https://www.youtube.com/watch?')) {
  40. jq.click()
  41. console.log('[AutoCloseYoutubeMiniplayer] miniplayer dismissed')
  42. }}, 200)
  43. }
  44. })
  45. })
  46. observer.observe(bodyList, {childList: true, subtree: true})
  47. }
  48. })()
  49. // Function to disable play/pause keyboard shortcut within the miniplayer
  50. function disableMiniplayerShortcut(event) {
  51. let miniplayer = document.querySelector('ytd-miniplayer');
  52. if (miniplayer && miniplayer.contains(event.target)) {
  53. if (event.code === 'Space' || event.code === 'ArrowRight' || event.code === 'ArrowLeft') {
  54. event.stopImmediatePropagation(); // Stop event propagation
  55. event.preventDefault(); // Prevent default keyboard shortcut action
  56. console.log('Keyboard shortcut disabled for miniplayer.');
  57. }
  58. }
  59. }
  60.  
  61. // Add event listener for keyboard shortcut within the miniplayer
  62. document.addEventListener('keydown', disableMiniplayerShortcut, true); // Use capture phase for early interception