Go.dev Code Highlight

Golang code highlighter

当前为 2023-12-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Go.dev Code Highlight
  3. // @version 1.1.5
  4. // @description Golang code highlighter
  5. // @author Anton Vi
  6. // @match https://*.go.dev/*
  7. // @resource IMPORTED_CSS https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css
  8. // @grant GM_getResourceText
  9. // @grant GM_addStyle
  10. // @license GNU GPLv3
  11. // @namespace GolangDevNamespace
  12. // ==/UserScript==
  13.  
  14. (function() {
  15.  
  16. 'use strict';
  17.  
  18. document.addEventListener('readystatechange', (e) => {
  19. if (e.target.readyState === 'complete') {
  20. resetBlogCss();
  21. start();
  22. }
  23. })
  24.  
  25.  
  26. })();
  27.  
  28. function resetBlogCss() {
  29. // reset styles for go dev blog
  30. let css = `#blog pre, #content .code, #content .playground {
  31. background: black;
  32. font-family: Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace;
  33. font-size: 1rem;
  34. }`;
  35. GM_addStyle(css);
  36.  
  37. }
  38.  
  39. function start() {
  40. // create Prisma object
  41. window.Prism = window.Prism || {};
  42. // no highlight on auto
  43. Prism.manual = true;
  44.  
  45. // add highlight styles css
  46. const my_css = GM_getResourceText("IMPORTED_CSS");
  47. GM_addStyle(my_css);
  48.  
  49. // start prism
  50. initPrismCore();
  51.  
  52. // add languages syntax
  53. addClikeSyntax();
  54. addGoSyntax();
  55.  
  56.  
  57. // loop pre-s and add code child elements for highlight
  58. let pre_s = document.getElementsByTagName('pre');
  59. for (let el of pre_s) {
  60. // skip pre element for ==Output== code run in documentation
  61. if (el.getElementsByClassName('Documentation-exampleOutputLabel').length > 0) {
  62. continue;
  63. }
  64.  
  65.  
  66. if (!el.getElementsByTagName('code').length > 0) {
  67. let text = el.innerHTML;
  68. el.innerHTML = '';
  69.  
  70. let code = document.createElement('code');
  71. code.innerHTML = text;
  72. code.className = 'language-go';
  73. el.appendChild(code);
  74. }
  75.  
  76. }
  77.  
  78.  
  79. // get examples in go library doc
  80. let divS = document.getElementsByClassName('Documentation-exampleDetailsBody');
  81. for (divEl of divS) {
  82. let outputPre = divEl.getElementsByTagName('pre')[0];
  83. outputPre.className = 'language-go';
  84.  
  85.  
  86. let textareaBlock = divEl.querySelector('textarea.Documentation-exampleCode.code');
  87. textareaBlock.style.display = 'none';
  88. // textareaBlock.remove();
  89. divEl.insertAdjacentHTML('afterbegin', '<pre class="language-go"><code>' + textareaBlock.value + '</code></pre>');
  90.  
  91.  
  92. }
  93.  
  94.  
  95. // start highlight syntax
  96. Prism.highlightAll();
  97.  
  98. }
  99.  
  100. function initPrismCore() {
  101. var _self = "undefined" != typeof window ? window : "undefined" != typeof WorkerGlobalScope && self instanceof WorkerGlobalScope ? self : {},
  102. Prism = function(e) {
  103. var n = /(?:^|\s)lang(?:uage)?-([\w-]+)(?=\s|$)/i,
  104. t = 0,
  105. r = {},
  106. a = {
  107. manual: e.Prism && e.Prism.manual,
  108. disableWorkerMessageHandler: e.Prism && e.Prism.disableWorkerMessageHandler,
  109. util: {
  110. encode: function e(n) { return n instanceof i ? new i(n.type, e(n.content), n.alias) : Array.isArray(n) ? n.map(e) : n.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/\u00a0/g, " ") },
  111. type: function(e) { return Object.prototype.toString.call(e).slice(8, -1) },
  112. objId: function(e) { return e.__id || Object.defineProperty(e, "__id", { value: ++t }), e.__id },
  113. clone: function e(n, t) {
  114. var r, i;
  115. switch (t = t || {}, a.util.type(n)) {
  116. case "Object":
  117. if (i = a.util.objId(n), t[i]) return t[i];
  118. for (var l in r = {}, t[i] = r, n) n.hasOwnProperty(l) && (r[l] = e(n[l], t));
  119. return r;
  120. case "Array":
  121. return i = a.util.objId(n), t[i] ? t[i] : (r = [], t[i] = r, n.forEach((function(n, a) { r[a] = e(n, t) })), r);
  122. default:
  123. return n
  124. }
  125. },
  126. getLanguage: function(e) {
  127. for (; e;) {
  128. var t = n.exec(e.className);
  129. if (t) return t[1].toLowerCase();
  130. e = e.parentElement
  131. }
  132. return "none"
  133. },
  134. setLanguage: function(e, t) { e.className = e.className.replace(RegExp(n, "gi"), ""), e.classList.add("language-" + t) },
  135. currentScript: function() {
  136. if ("undefined" == typeof document) return null;
  137. if ("currentScript" in document) return document.currentScript;
  138. try { throw new Error } catch (r) {
  139. var e = (/at [^(\r\n]*\((.*):[^:]+:[^:]+\)$/i.exec(r.stack) || [])[1];
  140. if (e) {
  141. var n = document.getElementsByTagName("script");
  142. for (var t in n)
  143. if (n[t].src == e) return n[t]
  144. }
  145. return null
  146. }
  147. },
  148. isActive: function(e, n, t) {
  149. for (var r = "no-" + n; e;) {
  150. var a = e.classList;
  151. if (a.contains(n)) return !0;
  152. if (a.contains(r)) return !1;
  153. e = e.parentElement
  154. }
  155. return !!t
  156. }
  157. },
  158. languages: {
  159. plain: r,
  160. plaintext: r,
  161. text: r,
  162. txt: r,
  163. extend: function(e, n) { var t = a.util.clone(a.languages[e]); for (var r in n) t[r] = n[r]; return t },
  164. insertBefore: function(e, n, t, r) {
  165. var i = (r = r || a.languages)[e],
  166. l = {};
  167. for (var o in i)
  168. if (i.hasOwnProperty(o)) {
  169. if (o == n)
  170. for (var s in t) t.hasOwnProperty(s) && (l[s] = t[s]);
  171. t.hasOwnProperty(o) || (l[o] = i[o])
  172. } var u = r[e];
  173. return r[e] = l, a.languages.DFS(a.languages, (function(n, t) { t === u && n != e && (this[n] = l) })), l
  174. },
  175. DFS: function e(n, t, r, i) {
  176. i = i || {};
  177. var l = a.util.objId;
  178. for (var o in n)
  179. if (n.hasOwnProperty(o)) {
  180. t.call(n, o, n[o], r || o);
  181. var s = n[o],
  182. u = a.util.type(s);
  183. "Object" !== u || i[l(s)] ? "Array" !== u || i[l(s)] || (i[l(s)] = !0, e(s, t, o, i)) : (i[l(s)] = !0, e(s, t, null, i))
  184. }
  185. }
  186. },
  187. plugins: {},
  188. highlightAll: function(e, n) { a.highlightAllUnder(document, e, n) },
  189. highlightAllUnder: function(e, n, t) {
  190. var r = { callback: t, container: e, selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code' };
  191. a.hooks.run("before-highlightall", r), r.elements = Array.prototype.slice.apply(r.container.querySelectorAll(r.selector)), a.hooks.run("before-all-elements-highlight", r);
  192. for (var i, l = 0; i = r.elements[l++];) a.highlightElement(i, !0 === n, r.callback)
  193. },
  194. highlightElement: function(n, t, r) {
  195. var i = a.util.getLanguage(n),
  196. l = a.languages[i];
  197. a.util.setLanguage(n, i);
  198. var o = n.parentElement;
  199. o && "pre" === o.nodeName.toLowerCase() && a.util.setLanguage(o, i);
  200. var s = { element: n, language: i, grammar: l, code: n.textContent };
  201.  
  202. function u(e) { s.highlightedCode = e, a.hooks.run("before-insert", s), s.element.innerHTML = s.highlightedCode, a.hooks.run("after-highlight", s), a.hooks.run("complete", s), r && r.call(s.element) }
  203. if (a.hooks.run("before-sanity-check", s), (o = s.element.parentElement) && "pre" === o.nodeName.toLowerCase() && !o.hasAttribute("tabindex") && o.setAttribute("tabindex", "0"), !s.code) return a.hooks.run("complete", s), void(r && r.call(s.element));
  204. if (a.hooks.run("before-highlight", s), s.grammar)
  205. if (t && e.Worker) {
  206. var c = new Worker(a.filename);
  207. c.onmessage = function(e) { u(e.data) }, c.postMessage(JSON.stringify({ language: s.language, code: s.code, immediateClose: !0 }))
  208. } else u(a.highlight(s.code, s.grammar, s.language));
  209. else u(a.util.encode(s.code))
  210. },
  211. highlight: function(e, n, t) { var r = { code: e, grammar: n, language: t }; if (a.hooks.run("before-tokenize", r), !r.grammar) throw new Error('The language "' + r.language + '" has no grammar.'); return r.tokens = a.tokenize(r.code, r.grammar), a.hooks.run("after-tokenize", r), i.stringify(a.util.encode(r.tokens), r.language) },
  212. tokenize: function(e, n) {
  213. var t = n.rest;
  214. if (t) {
  215. for (var r in t) n[r] = t[r];
  216. delete n.rest
  217. }
  218. var a = new s;
  219. return u(a, a.head, e), o(e, a, n, a.head, 0),
  220. function(e) { for (var n = [], t = e.head.next; t !== e.tail;) n.push(t.value), t = t.next; return n }(a)
  221. },
  222. hooks: {
  223. all: {},
  224. add: function(e, n) {
  225. var t = a.hooks.all;
  226. t[e] = t[e] || [], t[e].push(n)
  227. },
  228. run: function(e, n) {
  229. var t = a.hooks.all[e];
  230. if (t && t.length)
  231. for (var r, i = 0; r = t[i++];) r(n)
  232. }
  233. },
  234. Token: i
  235. };
  236.  
  237. function i(e, n, t, r) { this.type = e, this.content = n, this.alias = t, this.length = 0 | (r || "").length }
  238.  
  239. function l(e, n, t, r) {
  240. e.lastIndex = n;
  241. var a = e.exec(t);
  242. if (a && r && a[1]) {
  243. var i = a[1].length;
  244. a.index += i, a[0] = a[0].slice(i)
  245. }
  246. return a
  247. }
  248.  
  249. function o(e, n, t, r, s, g) {
  250. for (var f in t)
  251. if (t.hasOwnProperty(f) && t[f]) {
  252. var h = t[f];
  253. h = Array.isArray(h) ? h : [h];
  254. for (var d = 0; d < h.length; ++d) {
  255. if (g && g.cause == f + "," + d) return;
  256. var v = h[d],
  257. p = v.inside,
  258. m = !!v.lookbehind,
  259. y = !!v.greedy,
  260. k = v.alias;
  261. if (y && !v.pattern.global) {
  262. var x = v.pattern.toString().match(/[imsuy]*$/)[0];
  263. v.pattern = RegExp(v.pattern.source, x + "g")
  264. }
  265. for (var b = v.pattern || v, w = r.next, A = s; w !== n.tail && !(g && A >= g.reach); A += w.value.length, w = w.next) {
  266. var E = w.value;
  267. if (n.length > e.length) return;
  268. if (!(E instanceof i)) {
  269. var P, L = 1;
  270. if (y) {
  271. if (!(P = l(b, A, e, m)) || P.index >= e.length) break;
  272. var S = P.index,
  273. O = P.index + P[0].length,
  274. j = A;
  275. for (j += w.value.length; S >= j;) j += (w = w.next).value.length;
  276. if (A = j -= w.value.length, w.value instanceof i) continue;
  277. for (var C = w; C !== n.tail && (j < O || "string" == typeof C.value); C = C.next) L++, j += C.value.length;
  278. L--, E = e.slice(A, j), P.index -= A
  279. } else if (!(P = l(b, 0, E, m))) continue;
  280. S = P.index;
  281. var N = P[0],
  282. _ = E.slice(0, S),
  283. M = E.slice(S + N.length),
  284. W = A + E.length;
  285. g && W > g.reach && (g.reach = W);
  286. var z = w.prev;
  287. if (_ && (z = u(n, z, _), A += _.length), c(n, z, L), w = u(n, z, new i(f, p ? a.tokenize(N, p) : N, k, N)), M && u(n, w, M), L > 1) {
  288. var I = { cause: f + "," + d, reach: W };
  289. o(e, n, t, w.prev, A, I), g && I.reach > g.reach && (g.reach = I.reach)
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296.  
  297. function s() {
  298. var e = { value: null, prev: null, next: null },
  299. n = { value: null, prev: e, next: null };
  300. e.next = n, this.head = e, this.tail = n, this.length = 0
  301. }
  302.  
  303. function u(e, n, t) {
  304. var r = n.next,
  305. a = { value: t, prev: n, next: r };
  306. return n.next = a, r.prev = a, e.length++, a
  307. }
  308.  
  309. function c(e, n, t) {
  310. for (var r = n.next, a = 0; a < t && r !== e.tail; a++) r = r.next;
  311. n.next = r, r.prev = n, e.length -= a
  312. }
  313. if (e.Prism = a, i.stringify = function e(n, t) {
  314. if ("string" == typeof n) return n;
  315. if (Array.isArray(n)) { var r = ""; return n.forEach((function(n) { r += e(n, t) })), r }
  316. var i = { type: n.type, content: e(n.content, t), tag: "span", classes: ["token", n.type], attributes: {}, language: t },
  317. l = n.alias;
  318. l && (Array.isArray(l) ? Array.prototype.push.apply(i.classes, l) : i.classes.push(l)), a.hooks.run("wrap", i);
  319. var o = "";
  320. for (var s in i.attributes) o += " " + s + '="' + (i.attributes[s] || "").replace(/"/g, "&quot;") + '"';
  321. return "<" + i.tag + ' class="' + i.classes.join(" ") + '"' + o + ">" + i.content + "</" + i.tag + ">"
  322. }, !e.document) return e.addEventListener ? (a.disableWorkerMessageHandler || e.addEventListener("message", (function(n) {
  323. var t = JSON.parse(n.data),
  324. r = t.language,
  325. i = t.code,
  326. l = t.immediateClose;
  327. e.postMessage(a.highlight(i, a.languages[r], r)), l && e.close()
  328. }), !1), a) : a;
  329. var g = a.util.currentScript();
  330.  
  331. function f() { a.manual || a.highlightAll() }
  332. if (g && (a.filename = g.src, g.hasAttribute("data-manual") && (a.manual = !0)), !a.manual) { var h = document.readyState; "loading" === h || "interactive" === h && g && g.defer ? document.addEventListener("DOMContentLoaded", f) : window.requestAnimationFrame ? window.requestAnimationFrame(f) : window.setTimeout(f, 16) }
  333. return a
  334. }(_self);
  335. "undefined" != typeof module && module.exports && (module.exports = Prism), "undefined" != typeof global && (global.Prism = Prism);
  336. }
  337.  
  338. function addClikeSyntax() {
  339. Prism.languages.clike = {
  340. comment: [{ pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/, lookbehind: !0, greedy: !0 },
  341. { pattern: /(^|[^\\:])\/\/.*/, lookbehind: !0, greedy: !0 }
  342. ],
  343. string: { pattern: /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, greedy: !0 },
  344. "class-name": { pattern: /(\b(?:class|extends|implements|instanceof|interface|new|trait)\s+|\bcatch\s+\()[\w.\\]+/i, lookbehind: !0, inside: { punctuation: /[.\\]/ } },
  345. keyword: /\b(?:break|catch|continue|do|else|finally|for|function|if|in|instanceof|new|null|return|throw|try|while)\b/,
  346. boolean: /\b(?:false|true)\b/,
  347. function: /\b\w+(?=\()/,
  348. number: /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
  349. operator: /[<>]=?|[!=]=?=?|--?|\+\+?|&&?|\|\|?|[?*/~^%]/,
  350. punctuation: /[{}[\];(),.:]/
  351. };
  352. }
  353.  
  354. function addGoSyntax() {
  355. Prism.languages.go = Prism.languages
  356. .extend("clike", {
  357. string: { pattern: /(^|[^\\])"(?:\\.|[^"\\\r\n])*"|`[^`]*`/, lookbehind: !0, greedy: !0 },
  358. keyword: /\b(?:break|case|chan|const|continue|default|defer|else|fallthrough|for|func|go(?:to)?|if|import|interface|map|package|range|return|select|struct|switch|type|var)\b/,
  359. boolean: /\b(?:_|false|iota|nil|true)\b/,
  360. number: [/\b0(?:b[01_]+|o[0-7_]+)i?\b/i, /\b0x(?:[a-f\d_]+(?:\.[a-f\d_]*)?|\.[a-f\d_]+)(?:p[+-]?\d+(?:_\d+)*)?i?(?!\w)/i, /(?:\b\d[\d_]*(?:\.[\d_]*)?|\B\.\d[\d_]*)(?:e[+-]?[\d_]+)?i?(?!\w)/i],
  361. operator: /[*\/%^!=]=?|\+[=+]?|-[=-]?|\|[=|]?|&(?:=|&|\^=?)?|>(?:>=?|=)?|<(?:<=?|=|-)?|:=|\.\.\./,
  362. builtin: /\b(?:append|bool|byte|cap|close|complex|complex(?:64|128)|copy|delete|error|float(?:32|64)|u?int(?:8|16|32|64)?|imag|len|make|new|panic|print(?:ln)?|real|recover|rune|string|uintptr)\b/
  363. }),
  364. Prism.languages.insertBefore("go", "string", { char: { pattern: /'(?:\\.|[^'\\\r\n]){0,10}'/, greedy: !0 } }), delete Prism.languages.go["class-name"];
  365. }