YouTube Code Highlighter

Highlight code in YouTube comments

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         YouTube Code Highlighter
// @namespace    https://greasyfork.org/ru/users/901750-gooseob
// @version      1.1.2
// @description  Highlight code in YouTube comments
// @author       GooseOb
// @license      MIT
// @require      https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// ==/UserScript==

(function(){// index.ts
var until = (getItem, check, msToWait = 1e4, msReqTimeout = 20) => new Promise((res, rej) => {
  const reqLimit = msToWait / msReqTimeout;
  let i = 0;
  const interval = setInterval(() => {
    if (i++ > reqLimit)
      exit(rej);
    const item = getItem();
    if (!check(item))
      return;
    exit(() => res(item));
  }, msReqTimeout);
  const exit = (cb) => {
    clearInterval(interval);
    cb();
  };
});
var untilAppear = (getItem, msToWait) => until(getItem, Boolean, msToWait);
untilAppear(() => document.getElementById("comments")).then((comments) => {
  let isCSSLoaded = false;
  const visitedComments = new Set;
  const loadCSS = () => {
    fetch("https://cdn.jsdelivr.net/npm/highlight.js/styles/atom-one-dark.css").then((r) => r.text()).then((cssText) => {
      const style = document.createElement("style");
      style.innerHTML = cssText;
      document.head.appendChild(style);
    });
  };
  const _highlighter = {
    createHTML: (code) => code.replace(/```(\S*)\n(.+?)```/gs, (_$0, $1, $2) => `<code>\`\`\`${$1}
${($1 ? hljs.highlight($2, { language: $1 }) : hljs.highlightAuto($2)).value}\`\`\`</code>`)
  };
  const highlighter = window.trustedTypes?.createPolicy?.("highlightedCode", _highlighter) || _highlighter;
  setInterval(() => {
    for (const comment of comments.querySelectorAll("ytd-comment-view-model #content .yt-core-attributed-string")) {
      if (!visitedComments.has(comment) && /```\S*\n/.test(comment.textContent)) {
        visitedComments.add(comment);
        if (!isCSSLoaded) {
          loadCSS();
          isCSSLoaded = true;
        }
        comment.innerHTML = highlighter.createHTML(comment.textContent);
      }
    }
  }, 3000);
  document.addEventListener("yt-navigate-finish", () => {
    visitedComments.clear();
  });
});
})()