YouTube Hide Chat by Default

Hides chat on YouTube live streams by default

目前為 2020-07-27 提交的版本,檢視 最新版本

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