Unity Docs Syntax Hightligher Fork

Adds syntax highlighting to the Unity Documentation. This is a fork from Hekky.

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

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