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 https://github.com/Maoyeedy
  4. // @version 1.2.2
  5. // @author Maoyeedy
  6. // @license MIT
  7. // @description Adds syntax highlighting to the Unity Documentation. Forked from hyblocker.
  8. // @icon https://unity.com/favicon.ico
  9. //
  10. // @match https://docs.unity3d.com/Manual/*
  11. // @match https://docs.unity3d.com/ScriptReference/*
  12. // @match https://docs.unity3d.com/*/Manual/*
  13. // @match https://docs.unity3d.com/*/ScriptReference/*
  14. //
  15. // @grant GM_getResourceText
  16. // @grant GM_addStyle
  17. //
  18. // @require https://cdn.jsdelivr.net/npm/prismjs@1/prism.min.js
  19. // @require https://cdn.jsdelivr.net/npm/prismjs@1/components/prism-c.min.js
  20. // @require https://cdn.jsdelivr.net/npm/prismjs@1/components/prism-clike.min.js
  21. // @require https://cdn.jsdelivr.net/npm/prismjs@1/components/prism-csharp.min.js
  22. // @require https://cdn.jsdelivr.net/npm/prismjs@1/components/prism-hlsl.min.js
  23. // @resource PRISM_THEME_LIGHT https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-one-light.min.css
  24. // @resource PRISM_THEME_DARK https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-duotone-dark.min.css
  25. //
  26. // Recommended Light Themes
  27. // https://cdn.jsdelivr.net/npm/prismjs/themes/prism.min.css
  28. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-one-light.min.css
  29. //
  30. // Recommended Dark Themes
  31. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-xonokai.min.css
  32. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-duotone-dark.min.css
  33. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-duotone-space.min.css
  34. // Extra Themes
  35. // https://github.com/PrismJS/prism-themes
  36.  
  37. //
  38. // ==/UserScript==
  39.  
  40. (function () {
  41. "use strict"
  42. GM_addStyle(GM_getResourceText("PRISM_THEME_LIGHT"))
  43.  
  44. /* InjectCustomCSS */
  45. const customCSS = `
  46. code[class*="language-"],
  47. pre[class*="language-"] {
  48. border-radius: .5em;
  49. font-family: 'Jetbrains Mono', monospace !important;
  50. font-size: 0.875em !important;
  51. line-height: 1.5 !important;
  52. }
  53. .token.keyword,
  54. .token.bold {
  55. font-weight: normal !important;
  56. }
  57. `
  58.  
  59. const styleElement = document.createElement("style")
  60. styleElement.setAttribute("type", "text/css")
  61. styleElement.appendChild(document.createTextNode(customCSS))
  62. document.head.appendChild(styleElement)
  63.  
  64. /* Create Button */
  65. const switchButton = document.createElement("button")
  66. switchButton.style.cursor = "pointer"
  67. switchButton.style.position = "fixed"
  68. switchButton.style.bottom = "16px"
  69. switchButton.style.right = "16px"
  70. switchButton.style.width = "32px"
  71. switchButton.style.height = "32px"
  72. switchButton.style.borderRadius = "16px"
  73. switchButton.style.border = "1px solid #2a2734"
  74. switchButton.style.fontSize = "16px"
  75. switchButton.style.paddingBottom = "4px"
  76.  
  77. switchButton.style.backgroundColor = "#f9f9f9"
  78. switchButton.style.color = "#2a2734"
  79. switchButton.textContent = "🌞"
  80.  
  81. document.body.appendChild(switchButton)
  82.  
  83. /* Switch between dark and light themes */
  84. let isDarkTheme = false
  85. switchButton.addEventListener("click", () => {
  86. if (isDarkTheme) {
  87. GM_addStyle(GM_getResourceText("PRISM_THEME_LIGHT"))
  88. switchButton.style.backgroundColor = "#f9f9f9"
  89. switchButton.style.color = "#2a2734"
  90. switchButton.textContent = "🌞"
  91. isDarkTheme = false
  92. } else {
  93. GM_addStyle(GM_getResourceText("PRISM_THEME_DARK"))
  94. switchButton.style.backgroundColor = "#2a2734"
  95. switchButton.style.color = "#f9f9f9"
  96. switchButton.textContent = "🌙"
  97. isDarkTheme = true
  98. }
  99. })
  100. })()
  101.  
  102. const CSHARP = 0
  103. const HLSL = 1
  104.  
  105. var waitForGlobal = function (key, callback) {
  106. if (window[key]) {
  107. callback()
  108. } else {
  109. setTimeout(function () {
  110. waitForGlobal(key, callback)
  111. }, 100)
  112. }
  113. }
  114. function waitForLangLoad (lang, callback) {
  115. if (Prism.util.getLanguage(lang) != null) {
  116. callback()
  117. } else {
  118. setTimeout(function () {
  119. waitForLangLoad(lang, callback)
  120. }, 100)
  121. }
  122. }
  123.  
  124. function detectCodeLanguage (elem) {
  125. if (elem.classList.contains("codeExampleCS")) {
  126. return CSHARP
  127. }
  128.  
  129. if (
  130. elem.innerHTML.match(/CGPROGRAM|ENDCG|CGINCLUDE|#pragma|SubShader \"/g) !=
  131. null
  132. ) {
  133. return HLSL
  134. }
  135.  
  136. return CSHARP
  137. }
  138.  
  139. waitForGlobal("Prism", () => {
  140. waitForLangLoad("csharp", () => {
  141. waitForLangLoad("hlsl", () => {
  142. document.querySelectorAll(".content-wrap pre").forEach((el) => {
  143. el.innerHTML = el.innerHTML.replace(/\<br\>/g, "\n")
  144. if (detectCodeLanguage(el) == CSHARP) {
  145. el.classList.add("language-csharp")
  146. } else {
  147. el.classList.add("language-hlsl")
  148. }
  149. if (el.firstChild.nodeName != "CODE") {
  150. el.innerHTML = `<code>${el.innerHTML}</code>`
  151. }
  152. })
  153. })
  154. })
  155. })