Toggle Youtube Styles

Adds toggles to enable/disable some styles that change Youtube

当前为 2024-04-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Toggle Youtube Styles
  3. // @license MIT
  4. // @namespace rtonne
  5. // @match https://www.youtube.com/*
  6. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  7. // @version 1.3
  8. // @author Rtonne
  9. // @description Adds toggles to enable/disable some styles that change Youtube
  10. // @grant GM.addStyle
  11. // @grant GM.registerMenuCommand
  12. // @grant GM.unregisterMenuCommand
  13. // @grant GM.getValue
  14. // @grant GM.setValue
  15. // ==/UserScript==
  16.  
  17. // To check if inplace command replacement is supported
  18. // https://violentmonkey.github.io/api/gm/#gm_registermenucommand
  19. const inplace = "test" === GM.registerMenuCommand("test", ()=>{}, {id: "test"});
  20. GM.unregisterMenuCommand("test");
  21.  
  22. const commands = [
  23. {
  24. id: "guide_sections",
  25. disabled_caption: "☑ Guide Sections",
  26. enabled_caption: "☐ Guide Sections",
  27. disableStyle: () => GM.addStyle(`
  28. #sections.ytd-guide-renderer > ytd-guide-section-renderer:nth-child(n+3) {
  29. display: unset;
  30. }
  31. `),
  32. enableStyle: () => GM.addStyle(`
  33. #sections.ytd-guide-renderer > ytd-guide-section-renderer:nth-child(n+3) {
  34. display: none;
  35. }
  36. `)
  37. },
  38. {
  39. id: "comments",
  40. disabled_caption: "☑ Comments",
  41. enabled_caption: "☐ Comments",
  42. disableStyle: () => GM.addStyle(`
  43. ytd-comments#comments {
  44. display: unset;
  45. }
  46. `),
  47. enableStyle: () => GM.addStyle(`
  48. ytd-comments#comments {
  49. display: none;
  50. }
  51. `)
  52. },
  53. {
  54. id: "related_videos",
  55. disabled_caption: "☑ Related Videos",
  56. enabled_caption: "☐ Related Videos",
  57. disableStyle: () => GM.addStyle(`
  58. #secondary.ytd-watch-flexy > #secondary-inner > #related {
  59. display: unset;
  60. }
  61. `),
  62. enableStyle: () => GM.addStyle(`
  63. #secondary.ytd-watch-flexy > #secondary-inner > #related {
  64. display: none;
  65. }
  66. `)
  67. },
  68. {
  69. id: "shorts",
  70. disabled_caption: "☑ Shorts",
  71. enabled_caption: "☐ Shorts",
  72. disableStyle: () => GM.addStyle(`
  73. ytd-rich-section-renderer:has(> div > [is-shorts]) {
  74. display: unset;
  75. }
  76. `),
  77. enableStyle: () => GM.addStyle(`
  78. ytd-rich-section-renderer:has(> div > [is-shorts]) {
  79. display: none;
  80. }
  81. `)
  82. },
  83. {
  84. id: "community",
  85. disabled_caption: "☑ Community Posts",
  86. enabled_caption: "☐ Community Posts",
  87. disableStyle: () => GM.addStyle(`
  88. ytd-rich-section-renderer:has(> div > :not([is-shorts])) {
  89. display: unset;
  90. }
  91. `),
  92. enableStyle: () => GM.addStyle(`
  93. ytd-rich-section-renderer:has(> div > :not([is-shorts])) {
  94. display: none;
  95. }
  96. `)
  97. },
  98. ];
  99.  
  100. for (const command of commands) {
  101. setCommand(command);
  102. }
  103. registerAllCommands();
  104.  
  105. async function registerAllCommands() {
  106. for (const command of commands) {
  107. GM.registerMenuCommand(
  108. await getCaption(command),
  109. () => toggleCommand(command),
  110. {id: command.id, autoClose:false}
  111. );
  112. }
  113. }
  114. async function unregisterAllCommands() {
  115. for (const command of commands) {
  116. GM.unregisterMenuCommand(command.id);
  117. }
  118. }
  119. async function toggleCommand(command) {
  120. await GM.setValue(command.id, !(await GM.getValue(command.id, false)));
  121. setCommand(command);
  122. if (inplace) {
  123. GM.registerMenuCommand(
  124. await getCaption(command),
  125. () => toggleCommand(command),
  126. {id: command.id, autoClose:false}
  127. );
  128. } else {
  129. unregisterAllCommands();
  130. registerAllCommands();
  131. }
  132. }
  133. async function setCommand(command) {
  134. if (await GM.getValue(command.id, false)) {
  135. command.enableStyle();
  136. } else {
  137. command.disableStyle();
  138. }
  139. }
  140. async function getCaption(command) {
  141. let caption = command.enabled_caption;
  142. if (!(await GM.getValue(command.id, false))) {
  143. caption = command.disabled_caption;
  144. }
  145. return caption;
  146. }

QingJ © 2025

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