Youtube plus

Small CSS tweaks to youtube with color theme settings

当前为 2019-10-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube plus
  3. // @description Small CSS tweaks to youtube with color theme settings
  4. // @grant GM_addStyle
  5. // @include *://youtube.com/*
  6. // @include *://www.youtube.com/*
  7. // @run-at document-load
  8. // @version 2.1
  9. // @namespace https://greasyfork.org/users/3167
  10. // ==/UserScript==
  11.  
  12. var defaultcolor = "hsla(3, 60%, 47%, 1)";
  13.  
  14. var customcolor = defaultcolor;
  15.  
  16. var firstrun = false;
  17.  
  18. if (typeof(Storage) !== "undefined") {
  19. if (localStorage.yt_custom_color) {
  20. customcolor = localStorage.yt_custom_color;
  21. } else {
  22. firstrun = true;
  23. localStorage.yt_custom_color = customcolor;
  24. }
  25. }
  26.  
  27. console.log("yt_custom_color", customcolor);
  28.  
  29. var stylesheet = `
  30. body {
  31. /* --yt-custom-color: " + customcolor + "; */
  32. }
  33. .ytp-pause-overlay {
  34. display: none;
  35. }
  36. #player-ads { display: none; }
  37. body, ytd-app {
  38. --yt-brand-paper-button-color: var(--yt-custom-color) !important;
  39. --yt-brand-color: var(--yt-custom-color) !important;
  40. }
  41. paper-button.ytd-subscribe-button-renderer {
  42. background: var(--yt-custom-color);
  43. }
  44. a.yt-simple-endpoint.yt-formatted-string, ytd-guide-entry-renderer[active] path.style-scope.yt-icon {
  45. color: var(--yt-custom-color);
  46. }
  47. #progress.ytd-thumbnail-overlay-resume-playback-renderer, .ytp-red2 .ytp-swatch-background-color, .ytp-red2 .ytp-swatch-background-color-secondary, .ytp-play-progress.ytp-swatch-background-color, .ytp-swatch-background-color-secondary {
  48. background-color: var(--yt-custom-color);
  49. }
  50. path#lozenge-path, #logo path.style-scope.yt-icon, path.style-scope.ytd-topbar-logo-renderer {
  51. fill: var(--yt-custom-color);
  52. }
  53. div#top div#player {
  54. max-height: calc(100vh - var(--ytd-masthead-height, 56px));
  55. }
  56. `;
  57.  
  58. if (typeof GM_addStyle != "undefined") {
  59. GM_addStyle (stylesheet);
  60. } else {
  61. var css = document.createElement("style");
  62. css.type = "text/css";
  63. css.innerHTML = stylesheet;
  64. document.head.appendChild(css);
  65. }
  66.  
  67. var customcolorpicker = document.createElement('input');
  68. customcolorpicker.type = "color";
  69. customcolorpicker.onchange = "setcustomcolor(this.value);";
  70. customcolorpicker.style = "display: none;";
  71. customcolorpicker.addEventListener("change", function() {
  72. setcustomcolor(this.value);
  73. });
  74.  
  75.  
  76. var setcustomcolor = function (colorinput) {
  77. console.log("Changing color theme to: " + colorinput);
  78. customcolor = colorinput;
  79. document.body.style.setProperty('--yt-custom-color', customcolor);
  80.  
  81. localStorage.yt_custom_color = customcolor;
  82.  
  83. var savedcolor = localStorage.yt_custom_color;
  84. console.log("Saved color theme: " + savedcolor);
  85. }
  86. unsafeWindow.setcustomcolor = setcustomcolor;
  87.  
  88. var pickcustomcolor = function () {
  89. console.log("click");
  90. customcolorpicker.click();
  91. }
  92. unsafeWindow.pickcustomcolor = pickcustomcolor;
  93.  
  94. var resetcustomcolor = function () {
  95. unsafeWindow.setcustomcolor(defaultcolor);
  96. }
  97. unsafeWindow.resetcustomcolor = resetcustomcolor;
  98.  
  99. var video_emitkey = function(keycode) {
  100. var video_element = document.getElementById("movie_player");
  101. if (video_element) {
  102. var ke = new KeyboardEvent('keydown');
  103. delete ke.keyCode;
  104. Object.defineProperty(ke, "keyCode", {"value" : keycode});
  105. video_element.dispatchEvent(ke);
  106. }
  107. }
  108.  
  109. var volumecontrol_init = function() {
  110. var video_player = document.getElementById("ytd-player");
  111. if (video_player) {
  112.  
  113. if (!video_player.youtubeplus_wheel_hooked) {
  114. video_player.addEventListener('wheel', function(e) {
  115. e.preventDefault();
  116. if (e.deltaY < 0) {
  117. video_emitkey(38);
  118. }
  119. if (e.deltaY > 0) {
  120. video_emitkey(40);
  121. }
  122. });
  123. video_player.youtubeplus_wheel_hooked = true;
  124. }
  125. }
  126. }
  127.  
  128. var init = function() {
  129. setcustomcolor(customcolor);
  130. volumecontrol_init();
  131. mutator_init();
  132. console.log("Youtube++ init!");
  133. }
  134.  
  135. var mutator_callback = function() {
  136. console.log("Mutation detected...");
  137. volumecontrol_init();
  138. mutator_init();
  139. }
  140.  
  141. var mutator_init = function() {
  142. var targetNode = document.body;
  143. if (targetNode && !targetNode.youtubeplus_mutator_hooked) {
  144. var observer = new MutationObserver(mutator_callback);
  145. observer.observe(targetNode, { childList: true });
  146. targetNode.youtubeplus_mutator_hooked = true;
  147. }
  148. }
  149.  
  150. console.log("Youtube++ loaded!");
  151.  
  152. init();
  153.