YouTube Focus Mode

Put YouTube in focus mode! Hides all unasked for content (recommendations, subscriptions, related, etc.) on the home and watch pages.

目前为 2019-10-15 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Focus Mode
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.2
  5. // @description Put YouTube in focus mode! Hides all unasked for content (recommendations, subscriptions, related, etc.) on the home and watch pages.
  6. // @author EmeraldSlash
  7. // @match *://*.youtube.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // css selectors and enable states for various features of this script
  15. // delete home page feed
  16. const HOME_PAGE_FEED = ["ytd-section-list-renderer[page-subtype='home']", true];
  17. // auto focus search bar when home page feed deleted
  18. const HOME_PAGE_SEARCH_BAR = ["input#search", true];
  19. // delete related feed on watch page
  20. const WATCH_PAGE_RELATED = ["ytd-compact-video-renderer.ytd-watch-next-secondary-results-renderer:not([expansion='collapsed']).ytd-watch-next-secondary-results-renderer", true];
  21. // delete the related feed's continuations element which is what handles infinite scroll
  22. const WATCH_PAGE_CONTINUATION = ["#continuations.style-scope.ytd-watch-next-secondary-results-renderer", true];
  23. // delete the video wall that shows within the player when a video finishes
  24. const WATCH_PAGE_VIDEOWALL = [".ytp-endscreen-content", true];
  25.  
  26. // import library for detecting added elements
  27. // insertion-query v1.0.3 (2016-01-20)
  28. // license:MIT
  29. // Zbyszek Tenerowicz <naugtur@gmail.com> (http://naugtur.pl/)
  30. var insertionQ=function(){"use strict";function a(a,b){var d,e="insQ_"+g++,f=function(a){(a.animationName===e||a[i]===e)&&(c(a.target)||b(a.target))};d=document.createElement("style"),d.innerHTML="@"+j+"keyframes "+e+" { from { outline: 1px solid transparent } to { outline: 0px solid transparent } }\n"+a+" { animation-duration: 0.001s; animation-name: "+e+"; "+j+"animation-duration: 0.001s; "+j+"animation-name: "+e+"; } ",document.head.appendChild(d);var h=setTimeout(function(){document.addEventListener("animationstart",f,!1),document.addEventListener("MSAnimationStart",f,!1),document.addEventListener("webkitAnimationStart",f,!1)},n.timeout);return{destroy:function(){clearTimeout(h),d&&(document.head.removeChild(d),d=null),document.removeEventListener("animationstart",f),document.removeEventListener("MSAnimationStart",f),document.removeEventListener("webkitAnimationStart",f)}}}function b(a){a.QinsQ=!0}function c(a){return n.strictlyNew&&a.QinsQ===!0}function d(a){return c(a.parentNode)?a:d(a.parentNode)}function e(a){for(b(a),a=a.firstChild;a;a=a.nextSibling)void 0!==a&&1===a.nodeType&&e(a)}function f(f,g){var h=[],i=function(){var a;return function(){clearTimeout(a),a=setTimeout(function(){h.forEach(e),g(h),h=[]},10)}}();return a(f,function(a){if(!c(a)){b(a);var e=d(a);h.indexOf(e)<0&&h.push(e),i()}})}var g=100,h=!1,i="animationName",j="",k="Webkit Moz O ms Khtml".split(" "),l="",m=document.createElement("div"),n={strictlyNew:!0,timeout:20};if(m.style.animationName&&(h=!0),h===!1)for(var o=0;o<k.length;o++)if(void 0!==m.style[k[o]+"AnimationName"]){l=k[o],i=l+"AnimationName",j="-"+l.toLowerCase()+"-",h=!0;break}var p=function(b){return h&&b.match(/[^{}]/)?(n.strictlyNew&&e(document.body),{every:function(c){return a(b,c)},summary:function(a){return f(b,a)}}):!1};return p.config=function(a){for(var b in a)a.hasOwnProperty(b)&&(n[b]=a[b])},p}();
  31.  
  32. // update function that gets called everytime a new page loads
  33. // mostly redundant because of insertionQ? only relevant on fresh page loads & for search bar focusing
  34. function update() {
  35. // remove homepage feed
  36. if (HOME_PAGE_FEED[1]) {
  37. let result = document.querySelectorAll(HOME_PAGE_FEED[0]);
  38. if (result.length > 0) {
  39. result[0].remove();
  40.  
  41. // focus search bar if homepage feed was removed
  42. if (HOME_PAGE_SEARCH_BAR[1]) {
  43. let search = document.querySelector(HOME_PAGE_SEARCH_BAR[0]);
  44. if (search) {
  45. search.focus();
  46. }
  47. }
  48. }
  49. }
  50.  
  51. // remove related section on watch page
  52. if (WATCH_PAGE_RELATED[1]) {
  53. let result = document.querySelectorAll(WATCH_PAGE_RELATED[1]);
  54. if (WATCH_PAGE_CONTINUATION[1]) {
  55. result = [...result, ...document.querySelectorAll(WATCH_PAGE_CONTINUATION[0])];
  56. }
  57. if (WATCH_PAGE_VIDEOWALL[1]) {
  58. result = [...result, ...document.querySelectorAll(WATCH_PAGE_VIDEOWALL[0])];
  59. }
  60. if (result.length > 0) {
  61. for (let i = 0; i < result.length; i++) {
  62. result[i].remove()
  63. }
  64. }
  65. }
  66. }
  67.  
  68. // page change and element added listeners
  69. document.addEventListener('yt-navigate-finish', update);
  70.  
  71. // remove related section on watch page and infinite scroll element
  72. if (WATCH_PAGE_RELATED[1]) {
  73. insertionQ(WATCH_PAGE_RELATED[0]).every(function(element) {
  74. element.remove()
  75. })
  76. if (WATCH_PAGE_CONTINUATION[1]) {
  77. insertionQ(WATCH_PAGE_CONTINUATION[0]).every(function(element) {
  78. element.remove()
  79. })
  80. }
  81. if (WATCH_PAGE_VIDEOWALL[1]) {
  82. insertionQ(WATCH_PAGE_VIDEOWALL[0]).every(function(element) {
  83. element.remove()
  84. })
  85. }
  86. }
  87.  
  88. // call initial update
  89. update()
  90. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址