YouTube Hide Chat by Default

Hides chat on YouTube live streams by default

目前为 2020-08-31 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Hide Chat by Default
  3. // @namespace https://skoshy.com
  4. // @version 0.2.3
  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.  
  15. const scriptId = "youtube-hide-chat-by-default";
  16. const buttonSelector = "paper-button";
  17.  
  18. function log(...toLog) {
  19. console.log(`[${scriptId}]:`, ...toLog);
  20. }
  21.  
  22. function setAndGetNodeId(node) {
  23. const nodeIdString = `${scriptId}-id`;
  24.  
  25. let nodeId = node.getAttribute(nodeIdString);
  26. let hadNodeIdSet = true;
  27.  
  28. // log("new node found", { nodeId, hadNodeIdSet, node });
  29.  
  30. if (!nodeId) {
  31. hadNodeIdSet = false;
  32. nodeId = Math.random().toString();
  33. node.setAttribute(nodeIdString, nodeId);
  34. }
  35.  
  36. return { nodeId, hadNodeIdSet };
  37. }
  38.  
  39. function addedNodeHandler(node) {
  40. if (!node.matches || !node.matches(buttonSelector)) {
  41. return;
  42. }
  43.  
  44. const { nodeId, hadNodeIdSet } = setAndGetNodeId(node);
  45.  
  46. if (!hadNodeIdSet) {
  47. // this is a new element
  48.  
  49. if (node.innerText.toUpperCase().trim() === "HIDE CHAT") {
  50. node.click();
  51. log(`Hid the chat by default`);
  52. }
  53. }
  54. }
  55.  
  56. const bodyObserver = new MutationObserver(function(mutations) {
  57. mutations.forEach(function(mutation) {
  58. mutation.addedNodes.forEach(addedNode => {
  59. addedNodeHandler(addedNode);
  60.  
  61. // it might be text node or comment node which don't have querySelectorAll
  62. addedNode.querySelectorAll &&
  63. addedNode.querySelectorAll(buttonSelector).forEach(addedNodeHandler);
  64. });
  65. });
  66. });
  67.  
  68. bodyObserver.observe(document.body, {
  69. attributes: true,
  70. childList: true,
  71. subtree: true,
  72. characterData: true
  73. });
  74. })();