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.3
  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-one-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. text-shadow: none !important;
  53. }
  54. .token.keyword,
  55. .token.bold {
  56. font-weight: normal !important;
  57. }
  58. `
  59.  
  60. const styleElement = document.createElement("style")
  61. styleElement.setAttribute("type", "text/css")
  62. styleElement.appendChild(document.createTextNode(customCSS))
  63. document.head.appendChild(styleElement)
  64.  
  65. /* Create Button */
  66. const switchButton = document.createElement("button")
  67. switchButton.style.cursor = "pointer"
  68. switchButton.style.position = "fixed"
  69. switchButton.style.bottom = "16px"
  70. switchButton.style.right = "16px"
  71. switchButton.style.width = "32px"
  72. switchButton.style.height = "32px"
  73. switchButton.style.borderRadius = "16px"
  74. switchButton.style.border = "1px solid #2a2734"
  75. switchButton.style.fontSize = "16px"
  76. switchButton.style.paddingBottom = "4px"
  77.  
  78. switchButton.style.backgroundColor = "#f9f9f9"
  79. switchButton.style.color = "#2a2734"
  80. switchButton.textContent = "🌞"
  81.  
  82. document.body.appendChild(switchButton)
  83.  
  84. /* Switch between dark and light themes */
  85. let isDarkTheme = false
  86. switchButton.addEventListener("click", () => {
  87. if (isDarkTheme) {
  88. GM_addStyle(GM_getResourceText("PRISM_THEME_LIGHT"))
  89. switchButton.style.backgroundColor = "#f9f9f9"
  90. switchButton.style.color = "#2a2734"
  91. switchButton.textContent = "🌞"
  92. isDarkTheme = false
  93. } else {
  94. GM_addStyle(GM_getResourceText("PRISM_THEME_DARK"))
  95. switchButton.style.backgroundColor = "#2a2734"
  96. switchButton.style.color = "#f9f9f9"
  97. switchButton.textContent = "🌙"
  98. isDarkTheme = true
  99. }
  100. })
  101. })()
  102.  
  103. const CSHARP = 0
  104. const HLSL = 1
  105.  
  106. var waitForGlobal = function (key, callback) {
  107. if (window[key]) {
  108. callback()
  109. } else {
  110. setTimeout(function () {
  111. waitForGlobal(key, callback)
  112. }, 100)
  113. }
  114. }
  115. function waitForLangLoad (lang, callback) {
  116. if (Prism.util.getLanguage(lang) != null) {
  117. callback()
  118. } else {
  119. setTimeout(function () {
  120. waitForLangLoad(lang, callback)
  121. }, 100)
  122. }
  123. }
  124.  
  125. function detectCodeLanguage (elem) {
  126. if (elem.classList.contains("codeExampleCS")) {
  127. return CSHARP
  128. }
  129.  
  130. if (
  131. elem.innerHTML.match(/CGPROGRAM|ENDCG|CGINCLUDE|#pragma|SubShader \"/g) !=
  132. null
  133. ) {
  134. return HLSL
  135. }
  136.  
  137. return CSHARP
  138. }
  139.  
  140. waitForGlobal("Prism", () => {
  141. waitForLangLoad("csharp", () => {
  142. waitForLangLoad("hlsl", () => {
  143. document.querySelectorAll(".content-wrap pre").forEach((el) => {
  144. el.innerHTML = el.innerHTML.replace(/\<br\>/g, "\n")
  145. if (detectCodeLanguage(el) == CSHARP) {
  146. el.classList.add("language-csharp")
  147. } else {
  148. el.classList.add("language-hlsl")
  149. }
  150. if (el.firstChild.nodeName != "CODE") {
  151. el.innerHTML = `<code>${el.innerHTML}</code>`
  152. }
  153. })
  154. })
  155. })
  156. })