YouTube Hide Chat by Default

Hides chat on YouTube live streams by default

目前为 2020-07-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         YouTube Hide Chat by Default
// @namespace    https://skoshy.com
// @version      0.2.0
// @description  Hides chat on YouTube live streams by default
// @author       Stefan K.
// @match        https://*.youtube.com/*
// @grant        none
// ==/UserScript==

const scriptId = "youtube-hide-chat-by-default";
const buttonSelector = "paper-button";

(function() {
  "use strict";

  function log(...toLog) {
    console.log(`[${scriptId}]:`, ...toLog);
  }

  function setAndGetNodeId(node) {
    const nodeIdString = `${scriptId}-id`;

    let nodeId = node.getAttribute(nodeIdString);
    let hadNodeIdSet = true;

    // log("new node found", { nodeId, hadNodeIdSet, node });

    if (!nodeId) {
      hadNodeIdSet = false;
      nodeId = Math.random().toString();
      node.setAttribute(nodeIdString, nodeId);
    }

    return { nodeId, hadNodeIdSet };
  }

  function addedNodeHandler(node) {
    if (!node.matches || !node.matches(buttonSelector)) {
      return;
    }

    const { nodeId, hadNodeIdSet } = setAndGetNodeId(node);

    if (!hadNodeIdSet) {
      // this is a new element

      if (node.innerText === "HIDE CHAT") {
        node.click();
        log(`Hid the chat by default`);
      }
    }
  }

  var bodyObserver = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      mutation.addedNodes.forEach(addedNode => {
        addedNodeHandler(addedNode);

        // it might be text node or comment node which don't have querySelectorAll
        addedNode.querySelectorAll &&
          addedNode.querySelectorAll(buttonSelector).forEach(addedNodeHandler);
      });
    });
  });

  var bodyObserverConfig = {
    attributes: true,
    childList: true,
    subtree: true,
    characterData: true
  };

  bodyObserver.observe(document.body, bodyObserverConfig);
})();

QingJ © 2025

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