Unity Docs Syntax Hightligher Fork

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

目前為 2023-10-17 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Unity Docs Syntax Hightligher Fork
  3. // @namespace https://github.com/Maoyeedy
  4. // @version 1.2.6
  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 hhttps://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-one-dark.min.css
  25. //
  26. // Recommended Light Themes
  27. // https://cdn.jsdelivr.net/npm/prismjs@1/themes/prism.min.css
  28. //
  29. // Recommended Dark Themes
  30. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-xonokai.min.css
  31. // https://cdn.jsdelivr.net/gh/PrismJS/prism-themes/themes/prism-duotone-dark.min.css
  32. //
  33. // Extra Themes
  34. // https://github.com/PrismJS/prism-themes
  35.  
  36. //
  37. // ==/UserScript==
  38.  
  39. (function () {
  40. "use strict"
  41. /* Create Button */
  42. const switchButton = document.createElement("button")
  43. switchButton.style.cursor = "pointer"
  44. switchButton.style.position = "fixed"
  45. switchButton.style.bottom = "16px"
  46. switchButton.style.right = "16px"
  47. switchButton.style.width = "32px"
  48. switchButton.style.height = "32px"
  49. switchButton.style.borderRadius = "16px"
  50. switchButton.style.border = "1px solid"
  51. switchButton.style.fontSize = "16px"
  52. switchButton.style.paddingBottom = "2px"
  53.  
  54. /* Switch between dark and light themes */
  55. function SetLightTheme () {
  56. GM_addStyle(GM_getResourceText("PRISM_THEME_LIGHT"))
  57. switchButton.style.backgroundColor = "#f9f9f9"
  58. switchButton.style.borderColor = "#272b33"
  59. switchButton.textContent = "🌞"
  60. }
  61.  
  62. function SetDarkTheme () {
  63. GM_addStyle(GM_getResourceText("PRISM_THEME_DARK"))
  64. switchButton.style.backgroundColor = "#272b33"
  65. switchButton.style.borderColor = "#fae3a2"
  66. switchButton.textContent = "🌙"
  67. }
  68.  
  69. function SetTheme () {
  70. isLightTheme ? SetLightTheme() : SetDarkTheme()
  71. }
  72.  
  73. let isLightTheme = true
  74. SetTheme()
  75.  
  76. document.body.appendChild(switchButton)
  77. switchButton.addEventListener("click", () => {
  78. isLightTheme = !isLightTheme
  79. SetTheme()
  80. })
  81.  
  82. /* InjectCustomCSS */
  83. const customCSS = `
  84. code[class*="language-"],
  85. pre[class*="language-"] {
  86. border-radius: .5em;
  87. font-family: 'Jetbrains Mono', monospace !important;
  88. font-size: 0.875em !important;
  89. line-height: 1.5 !important;
  90. text-shadow: none !important;
  91. }
  92. .token.keyword,
  93. .token.bold {
  94. font-weight: normal !important;
  95. }
  96. .token.comment {
  97. font-style: italic !important;
  98. }
  99. .token.operator,
  100. .token.entity,
  101. .token.url,
  102. .style .token.string {
  103. background: transparent !important;
  104. }
  105. `
  106. GM_addStyle(customCSS)
  107. })()
  108.  
  109. const CSHARP = 0
  110. const HLSL = 1
  111.  
  112. var waitForGlobal = function (key, callback) {
  113. if (window[key]) {
  114. callback()
  115. } else {
  116. setTimeout(function () {
  117. waitForGlobal(key, callback)
  118. }, 100)
  119. }
  120. }
  121. function waitForLangLoad (lang, callback) {
  122. if (Prism.util.getLanguage(lang) != null) {
  123. callback()
  124. } else {
  125. setTimeout(function () {
  126. waitForLangLoad(lang, callback)
  127. }, 100)
  128. }
  129. }
  130.  
  131. function detectCodeLanguage (elem) {
  132. if (elem.classList.contains("codeExampleCS")) {
  133. return CSHARP
  134. }
  135.  
  136. if (
  137. elem.innerHTML.match(/CGPROGRAM|ENDCG|CGINCLUDE|#pragma|SubShader \"/g) !=
  138. null
  139. ) {
  140. return HLSL
  141. }
  142.  
  143. return CSHARP
  144. }
  145.  
  146. waitForGlobal("Prism", () => {
  147. waitForLangLoad("csharp", () => {
  148. waitForLangLoad("hlsl", () => {
  149. document.querySelectorAll(".content-wrap pre").forEach((el) => {
  150. el.innerHTML = el.innerHTML.replace(/\<br\>/g, "\n")
  151. if (detectCodeLanguage(el) == CSHARP) {
  152. el.classList.add("language-csharp")
  153. } else {
  154. el.classList.add("language-hlsl")
  155. }
  156. if (el.firstChild.nodeName != "CODE") {
  157. el.innerHTML = `<code>${el.innerHTML}</code>`
  158. }
  159. })
  160. })
  161. })
  162. })