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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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