Unity Docs Syntax Hightligher Fork

Adds syntax highlighting to the Unity Documentation. Forked from hyblocker.

当前为 2023-10-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Unity Docs Syntax Hightligher Fork
  3. // @namespace Violentmonkey Scripts
  4. // @version 1.2
  5. // @author Maoyeedy
  6. // @license MIT
  7. // @description Adds syntax highlighting to the Unity Documentation. Forked from hyblocker.
  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://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.js
  18. // @require https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-c.min.js
  19. // @require https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-clike.min.js
  20. // @require https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-csharp.min.js
  21. // @require https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-hlsl.min.js
  22. // @resource PRISM_THEME_LIGHT https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-one-light.css
  23. // @resource PRISM_THEME_DARK https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-duotone-dark.css
  24. //
  25. // Recommended Light Themes
  26. // https://cdn.jsdelivr.net/npm/prismjs/themes/prism.min.css
  27. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-one-light.css
  28. //
  29. // Recommended Dark Themes
  30. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-xonokai.css
  31. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-duotone-dark.css
  32. //
  33. // Extra Themes
  34. // https://github.com/PrismJS/prism-themes
  35.  
  36. //
  37. // ==/UserScript==
  38.  
  39. (function() {
  40. 'use strict';
  41. GM_addStyle(GM_getResourceText("PRISM_THEME_LIGHT"));
  42. //GM_addStyle(GM_getResourceText("PRISM_THEME_DARK"));
  43.  
  44.  
  45. // Inject custom CSS
  46. const customCSS = `
  47. code {
  48. font-family: 'Jetbrains Mono', monospace !important;
  49. font-size: 0.875em !important;
  50. }
  51. pre[class*=language-]{
  52. border-radius:.5em;
  53. }
  54. `;
  55.  
  56. const styleElement = document.createElement('style');
  57. styleElement.type = 'text/css';
  58. styleElement.appendChild(document.createTextNode(customCSS));
  59. document.head.appendChild(styleElement);
  60. }());
  61.  
  62.  
  63. const CSHARP = 0;
  64. const HLSL = 1;
  65.  
  66. var waitForGlobal = function(key, callback) {
  67. if (window[key]) {
  68. callback();
  69. } else {
  70. setTimeout(function() {
  71. waitForGlobal(key, callback);
  72. }, 100);
  73. }
  74. };
  75. function waitForLangLoad(lang, callback) {
  76. if (Prism.util.getLanguage(lang) != null) {
  77. callback();
  78. } else {
  79. setTimeout(function() {
  80. waitForLangLoad(lang, callback);
  81. }, 100);
  82. }
  83. }
  84.  
  85. function detectCodeLanguage(elem) {
  86. if (elem.classList.contains('codeExampleCS')) {
  87. return CSHARP;
  88. }
  89.  
  90. if (elem.innerHTML.match(/CGPROGRAM|ENDCG|CGINCLUDE|#pragma|SubShader \"/g) != null) {
  91. return HLSL;
  92. }
  93.  
  94. return CSHARP;
  95. }
  96.  
  97. waitForGlobal("Prism", () => {
  98. waitForLangLoad("csharp", () => {
  99. waitForLangLoad("hlsl", () => {
  100. document.querySelectorAll('.content-wrap pre').forEach((el) => {
  101. el.innerHTML = el.innerHTML.replace(/\<br\>/g, '\n');
  102. if (detectCodeLanguage(el) == CSHARP) {
  103. el.classList.add("language-csharp");
  104. } else {
  105. el.classList.add("language-hlsl");
  106. }
  107. if (el.firstChild.nodeName != 'CODE') {
  108. el.innerHTML = `<code>${el.innerHTML}</code>`;
  109. }
  110. });
  111. });
  112. });
  113. });
  114.