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