Unity Docs Syntax Hightligher

Adds syntax highlighting to the Unity Documentation

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name        Unity Docs Syntax Hightligher
// @namespace   Violentmonkey Scripts
// @version     1.0
// @author      Hekky
// @license     MIT
// @description Adds syntax highlighting to the Unity Documentation
//
// @match       https://docs.unity3d.com/Manual/*
// @match       https://docs.unity3d.com/ScriptReference/*
// @match       https://docs.unity3d.com/*/Manual/*
// @match       https://docs.unity3d.com/*/ScriptReference/*
//
// @grant       GM_getResourceText
// @grant       GM_addStyle
//
// @require     https://unpkg.com/[email protected]/prism.js
//
// @require     https://unpkg.com/[email protected]/components/prism-c.min.js
// @require     https://unpkg.com/[email protected]/components/prism-clike.min.js
// @require     https://unpkg.com/[email protected]/components/prism-csharp.min.js
// @require     https://unpkg.com/[email protected]/components/prism-hlsl.min.js
//
// @require     https://unpkg.com/[email protected]/plugins/line-numbers/prism-line-numbers.min.js
// @require     https://unpkg.com/[email protected]/plugins/show-language/prism-show-language.min.js
//
// @resource    PRISM_THEME https://unpkg.com/[email protected]/themes/prism-tomorrow.min.css
//
// @resource    LINE_NUMS_THEME https://unpkg.com/[email protected]/plugins/line-numbers/prism-line-numbers.min.css
// ==/UserScript==

(function() {
    'use strict';
    GM_addStyle(GM_getResourceText("PRISM_THEME"));
    GM_addStyle(GM_getResourceText("LINE_NUMS_THEME"));
}());

const CSHARP = 0;
const HLSL = 1;

var waitForGlobal = function(key, callback) {
  if (window[key]) {
    callback();
  } else {
    setTimeout(function() {
      waitForGlobal(key, callback);
    }, 100);
  }
};
function waitForLangLoad(lang, callback) {
  if (Prism.util.getLanguage(lang) != null) {
    callback();
  } else {
    setTimeout(function() {
      waitForLangLoad(lang, callback);
    }, 100);
  }
}

function detectCodeLanguage(elem) {
  if (elem.classList.contains('codeExampleCS')) {
    return CSHARP;
  }

  if (elem.innerHTML.match(/CGPROGRAM|ENDCG|CGINCLUDE|#pragma|SubShader \"/g) != null) {
    return HLSL;
  }

  return CSHARP;
}

waitForGlobal("Prism", () => {
  waitForLangLoad("csharp", () => {
    waitForLangLoad("hlsl", () => {
      document.querySelectorAll('.content-wrap pre').forEach((el) => {
        el.innerHTML = el.innerHTML.replace(/\<br\>/g, '\n');
        el.classList.add("line-numbers");
        if (detectCodeLanguage(el) == CSHARP) {
            el.classList.add("language-csharp");
        } else {
            el.classList.add("language-hlsl");
        }
        if (el.firstChild.nodeName != 'CODE') {
          el.innerHTML = `<code>${el.innerHTML}</code>`;
        }
      });
    });
  });
});