Twitch Theatermode

Auto activate theater mode on Twitch + remove front-page-carousel and reapply on page change

当前为 2025-05-22 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Twitch Theatermode
// @namespace    http://tampermonkey.net/
// @version      1.1.4
// @description  Auto activate theater mode on Twitch + remove front-page-carousel and reapply on page change
// @author       HaCk3Dq
// @match        http*://*.twitch.tv/*
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(() => {
  "use strict";

  const waitForElement = (selector, timeout = 7000) => {
    return new Promise((resolve, reject) => {
      const element = document.querySelector(selector);
      if (element) return resolve(element);

      const observer = new MutationObserver(() => {
        const el = document.querySelector(selector);
        if (el) {
          observer.disconnect();
          resolve(el);
        }
      });
      observer.observe(document.body, { childList: true, subtree: true });

      setTimeout(() => {
        observer.disconnect();
        console.log(`Timeout waiting for element: ${selector}`);
        reject();
      }, timeout);
    });
  };

  function removeMainCarousel() {
    setTimeout(() => {
      const videoElement = document.querySelector(
        '[data-test-selector="featured-item-video"] video',
      );
      if (videoElement) {
        videoElement.pause();
      } else {
        console.log("video not found");
      }
    }, 3000);
  }

  function removePinned() {
    waitForElement('button[aria-label="Hide for yourself"]').then((el) =>
      el.click(),
    );
  }

  function hideMutedVOD() {
    waitForElement('button[aria-label="Dismiss muted audio notice"]').then(
      (el) => el.click(),
    );
  }

  function activateTheatreMode() {
    setTimeout(() => {
      const theatreModeButton = document.querySelector(
        'button[aria-label="Theatre Mode (alt+t)"]',
      );
      if (theatreModeButton) {
        theatreModeButton.click();
      }
    }, 1500);
  }

  const handlePageChange = () => {
    activateTheatreMode();
    removeMainCarousel();
    removePinned();
    hideMutedVOD();
  };

  function watchURL() {
    let lastUrl = location.href;
    new MutationObserver(() => {
      const currentUrl = location.href;
      if (currentUrl !== lastUrl) {
        lastUrl = currentUrl;
        handlePageChange();
      }
    }).observe(document, { subtree: true, childList: true });
  }

  handlePageChange();
  watchURL();
})();