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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
})();