Unity Docs Syntax Hightligher

Adds syntax highlighting to the Unity Documentation

  1. // ==UserScript==
  2. // @name Unity Docs Syntax Hightligher
  3. // @namespace Violentmonkey Scripts
  4. // @version 1.0
  5. // @author Hekky
  6. // @license MIT
  7. // @description Adds syntax highlighting to the Unity Documentation
  8. //
  9. // @match https://docs.unity3d.com/Manual/*
  10. // @match https://docs.unity3d.com/ScriptReference/*
  11. // @match https://docs.unity3d.com/*/Manual/*
  12. // @match https://docs.unity3d.com/*/ScriptReference/*
  13. //
  14. // @grant GM_getResourceText
  15. // @grant GM_addStyle
  16. //
  17. // @require https://unpkg.com/prismjs@1.29.0/prism.js
  18. //
  19. // @require https://unpkg.com/prismjs@1.29.0/components/prism-c.min.js
  20. // @require https://unpkg.com/prismjs@1.29.0/components/prism-clike.min.js
  21. // @require https://unpkg.com/prismjs@1.29.0/components/prism-csharp.min.js
  22. // @require https://unpkg.com/prismjs@1.29.0/components/prism-hlsl.min.js
  23. //
  24. // @require https://unpkg.com/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.js
  25. // @require https://unpkg.com/prismjs@1.29.0/plugins/show-language/prism-show-language.min.js
  26. //
  27. // @resource PRISM_THEME https://unpkg.com/prismjs@1.29.0/themes/prism-tomorrow.min.css
  28. //
  29. // @resource LINE_NUMS_THEME https://unpkg.com/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.css
  30. // ==/UserScript==
  31.  
  32. (function() {
  33. 'use strict';
  34. GM_addStyle(GM_getResourceText("PRISM_THEME"));
  35. GM_addStyle(GM_getResourceText("LINE_NUMS_THEME"));
  36. }());
  37.  
  38. const CSHARP = 0;
  39. const HLSL = 1;
  40.  
  41. var waitForGlobal = function(key, callback) {
  42. if (window[key]) {
  43. callback();
  44. } else {
  45. setTimeout(function() {
  46. waitForGlobal(key, callback);
  47. }, 100);
  48. }
  49. };
  50. function waitForLangLoad(lang, callback) {
  51. if (Prism.util.getLanguage(lang) != null) {
  52. callback();
  53. } else {
  54. setTimeout(function() {
  55. waitForLangLoad(lang, callback);
  56. }, 100);
  57. }
  58. }
  59.  
  60. function detectCodeLanguage(elem) {
  61. if (elem.classList.contains('codeExampleCS')) {
  62. return CSHARP;
  63. }
  64.  
  65. if (elem.innerHTML.match(/CGPROGRAM|ENDCG|CGINCLUDE|#pragma|SubShader \"/g) != null) {
  66. return HLSL;
  67. }
  68.  
  69. return CSHARP;
  70. }
  71.  
  72. waitForGlobal("Prism", () => {
  73. waitForLangLoad("csharp", () => {
  74. waitForLangLoad("hlsl", () => {
  75. document.querySelectorAll('.content-wrap pre').forEach((el) => {
  76. el.innerHTML = el.innerHTML.replace(/\<br\>/g, '\n');
  77. el.classList.add("line-numbers");
  78. if (detectCodeLanguage(el) == CSHARP) {
  79. el.classList.add("language-csharp");
  80. } else {
  81. el.classList.add("language-hlsl");
  82. }
  83. if (el.firstChild.nodeName != 'CODE') {
  84. el.innerHTML = `<code>${el.innerHTML}</code>`;
  85. }
  86. });
  87. });
  88. });
  89. });