YouTube Live Chat Resurrection

Fixes missing live chat on YouTube livestreams. Reloads once, then forces chat to appear instantly on every stream. Fixed for archived livestreams

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Live Chat Resurrection
// @namespace    yt-autofix
// @version      1.1
// @description  Fixes missing live chat on YouTube livestreams. Reloads once, then forces chat to appear instantly on every stream. Fixed for archived livestreams
// @author       KiyoshiNatsumi
// @match        https://www.youtube.com/*
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
'use strict';

let lastVideoId = null;
let reloadedThisSession = false;
let chatReady = false;
let reloadCount = 0;

function getVideoId() {
  return new URL(location.href).searchParams.get('v');
}

function isLivestream() {
  const meta = document.querySelector('meta[itemprop="isLiveBroadcast"]');
  if (meta && meta.content === "true") return true;
  return location.href.includes('/watch') && location.href.includes('live');
}

function isLiveArchive() {
  const meta = document.querySelector('meta[itemprop="isLiveBroadcast"]');
  return meta && meta.content === "false";
}

function forceChatLoad(retry = 0) {
  if (chatReady) return;
  const vid = getVideoId();
  if (!vid) return;
  const chatFrame = document.querySelector('iframe#chatframe');
  if (!chatFrame) {
    if (retry < 15) setTimeout(() => forceChatLoad(retry + 1), 1000);
    return;
  }

  const src = chatFrame.getAttribute('src') || '';
  if (!src.includes('live_chat')) {
    chatFrame.src = `https://www.youtube.com/live_chat?v=${vid}&is_popout=1`;
    return;
  }

  try {
    const doc = chatFrame.contentDocument;
    const hasChat = doc?.querySelector('#chat');
    if (hasChat) {
      chatReady = true;
      return;
    }

    if (!hasChat && reloadCount < 2 && isLivestream()) {
      reloadCount++;
      setTimeout(() => {
        chatFrame.src = chatFrame.src;
        forceChatLoad();
      }, 1500);
    }
  } catch (e) {}
}

function handleVideoChange() {
  const vid = getVideoId();
  if (!vid || vid === lastVideoId) return;
  lastVideoId = vid;
  chatReady = false;
  reloadCount = 0;

  if (isLivestream() && !reloadedThisSession) {
    reloadedThisSession = true;
    console.log('[Live Chat Resurrection] Detected LIVE → reloading once...');
    setTimeout(() => location.reload(), 1200);
  } else {
    setTimeout(() => forceChatLoad(), 2000);
  }
}

const observer = new MutationObserver(() => handleVideoChange());
observer.observe(document.body, { childList: true, subtree: true });

setTimeout(() => {
  const vid = getVideoId();
  if (!vid) return;
  if (isLivestream() && !reloadedThisSession) {
    console.log('[Live Chat Resurrection] First live detected → reload once');
    reloadedThisSession = true;
    setTimeout(() => location.reload(), 1200);
  } else {
    forceChatLoad();
  }
}, 2000);
})();