YouTube Hide Chat by Default

Hides chat on YouTube live streams by default

当前为 2021-04-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Hide Chat by Default
  3. // @namespace https://skoshy.com
  4. // @version 0.3.0
  5. // @description Hides chat on YouTube live streams by default
  6. // @author Stefan K.
  7. // @match https://*.youtube.com/*
  8. // @grant none
  9. // @icon https://youtube.com/favicon.ico
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. "use strict";
  14. const scriptId = "youtube-hide-chat-by-default";
  15.  
  16. // configurable vars
  17. // - if youtube decides to use a new button type, add it here
  18. const buttonSelectors = ["paper-button", "ytd-toggle-button-renderer"];
  19. // - for different languages for the HIDE CHAT text, add them here
  20. const hideChatTexts = [
  21. 'HIDE CHAT', // english
  22. 'OCULTAR CHAT', // spanish
  23. 'MASQUER LA CONVERSATION PAR CHAT', // french
  24. 'MASQUER LE CLAVARDAGE', // french canada
  25. 'NASCONDI CHAT', // italian
  26. 'OCULTAR CONVERSA', // portuguese
  27. 'চ্চাট লুকুৱাওক', // bengali
  28. 'च्याट लुकाउनुहोस्', // nepali
  29. 'चैट छिपाएं', // hindi
  30. 'チャットを非表示', // japanese
  31. '隐藏聊天记录', // zh-Hans-CN
  32. '隱藏即時通訊', // zh-Hant-TW and zh-Hant-HK
  33. ];
  34.  
  35. function log(...toLog) {
  36. console.log(`[${scriptId}]:`, ...toLog);
  37. }
  38.  
  39. function setAndGetNodeId(node) {
  40. const nodeIdString = `${scriptId}-id`;
  41.  
  42. let nodeId = node.getAttribute(nodeIdString);
  43. let hadNodeIdSet = true;
  44.  
  45. // log("new node found", { nodeId, hadNodeIdSet, node });
  46.  
  47. if (!nodeId) {
  48. hadNodeIdSet = false;
  49. nodeId = Math.random().toString();
  50. node.setAttribute(nodeIdString, nodeId);
  51. }
  52.  
  53. return { nodeId, hadNodeIdSet };
  54. }
  55.  
  56. function addedNodeHandler(node) {
  57. if (!(
  58. node.matches &&
  59. buttonSelectors.some(b => node.matches(b))
  60. )) {
  61. return;
  62. }
  63.  
  64. const { nodeId, hadNodeIdSet } = setAndGetNodeId(node);
  65.  
  66. if (!hadNodeIdSet) {
  67. // this is a new element
  68.  
  69. if (hideChatTexts.includes(node.innerText.toUpperCase().trim())) {
  70. node.click();
  71. log(`Hid the chat by default`);
  72. }
  73. }
  74. }
  75.  
  76. const bodyObserver = new MutationObserver(function(mutations) {
  77. mutations.forEach(function(mutation) {
  78. mutation.addedNodes.forEach(addedNode => {
  79. addedNodeHandler(addedNode);
  80.  
  81. // it might be text node or comment node which don't have querySelectorAll
  82. if (addedNode.querySelectorAll) {
  83. buttonSelectors.forEach(bs => {
  84. addedNode.querySelectorAll(bs).forEach(addedNodeHandler);
  85. })
  86. }
  87. });
  88. });
  89. });
  90.  
  91. bodyObserver.observe(document.body, {
  92. attributes: true,
  93. childList: true,
  94. subtree: true,
  95. characterData: true
  96. });
  97. })();