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.5
  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 #272b33"
  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.textContent = "🌞"
  59. }
  60.  
  61. function SetDarkTheme () {
  62. GM_addStyle(GM_getResourceText("PRISM_THEME_DARK"))
  63. switchButton.style.backgroundColor = "#272b33"
  64. switchButton.textContent = "🌙"
  65. }
  66.  
  67. function SetTheme () {
  68. isLightTheme ? SetLightTheme() : SetDarkTheme()
  69. }
  70.  
  71. let isLightTheme = true
  72. SetTheme()
  73.  
  74. document.body.appendChild(switchButton)
  75. switchButton.addEventListener("click", () => {
  76. isLightTheme = !isLightTheme
  77. SetTheme()
  78. })
  79.  
  80. /* InjectCustomCSS */
  81. const customCSS = `
  82. code[class*="language-"],
  83. pre[class*="language-"] {
  84. border-radius: .5em;
  85. font-family: 'Jetbrains Mono', monospace !important;
  86. font-size: 0.875em !important;
  87. line-height: 1.5 !important;
  88. text-shadow: none !important;
  89. }
  90. .token.keyword,
  91. .token.bold {
  92. font-weight: normal !important;
  93. }
  94. .token.comment {
  95. font-style: italic !important;
  96. }
  97. .token.operator,
  98. .token.entity,
  99. .token.url,
  100. .style .token.string {
  101. background: transparent !important;
  102. }
  103. `
  104. GM_addStyle(customCSS)
  105. })()
  106.  
  107. const CSHARP = 0
  108. const HLSL = 1
  109.  
  110. var waitForGlobal = function (key, callback) {
  111. if (window[key]) {
  112. callback()
  113. } else {
  114. setTimeout(function () {
  115. waitForGlobal(key, callback)
  116. }, 100)
  117. }
  118. }
  119. function waitForLangLoad (lang, callback) {
  120. if (Prism.util.getLanguage(lang) != null) {
  121. callback()
  122. } else {
  123. setTimeout(function () {
  124. waitForLangLoad(lang, callback)
  125. }, 100)
  126. }
  127. }
  128.  
  129. function detectCodeLanguage (elem) {
  130. if (elem.classList.contains("codeExampleCS")) {
  131. return CSHARP
  132. }
  133.  
  134. if (
  135. elem.innerHTML.match(/CGPROGRAM|ENDCG|CGINCLUDE|#pragma|SubShader \"/g) !=
  136. null
  137. ) {
  138. return HLSL
  139. }
  140.  
  141. return CSHARP
  142. }
  143.  
  144. waitForGlobal("Prism", () => {
  145. waitForLangLoad("csharp", () => {
  146. waitForLangLoad("hlsl", () => {
  147. document.querySelectorAll(".content-wrap pre").forEach((el) => {
  148. el.innerHTML = el.innerHTML.replace(/\<br\>/g, "\n")
  149. if (detectCodeLanguage(el) == CSHARP) {
  150. el.classList.add("language-csharp")
  151. } else {
  152. el.classList.add("language-hlsl")
  153. }
  154. if (el.firstChild.nodeName != "CODE") {
  155. el.innerHTML = `<code>${el.innerHTML}</code>`
  156. }
  157. })
  158. })
  159. })
  160. })