GitHub 代码块图标增强(基于 NerdFont)

通过为 GitHub 代码块设置 NerdFont 字体以显示更多图标。

  1. // ==UserScript==
  2. // @name GitHub CodeBlock Icon Enhance PowerBy NerdFont
  3. // @name:zh-CN GitHub 代码块图标增强(基于 NerdFont)
  4. // @description Display more icons by setting NerdFont for GitHub's code block.
  5. // @description:zh-CN 通过为 GitHub 代码块设置 NerdFont 字体以显示更多图标。
  6. // @icon https://github.githubassets.com/favicons/favicon.png
  7. // @author cuiko
  8. // @namespace https://github.com/cuiko/codeblock-icon-enhance
  9. // @homepage https://github.com/cuiko/codeblock-icon-enhance
  10. // @supportURL https://github.com/cuiko/codeblock-icon-enhance/issues
  11. // @require https://unpkg.com/sweetalert2@11/dist/sweetalert2.min.js
  12. // @resource Swal https://unpkg.com/sweetalert2@11/dist/sweetalert2.min.css
  13. // @version 0.1.1
  14. // @license GPL-3.0
  15. // @match *://github.com/*
  16. // @match *://gitlab.com/*
  17. // @match *://gitee.com/*
  18. // @match *://stackoverflow.com/*
  19. // @match *://stackexchange.com/*
  20. // @match *://reddit.com/*
  21. // @match *://juejin.cn/*
  22. // @match *://segmentfault.com/*
  23. // @grant GM_setValue
  24. // @grant GM_getValue
  25. // @grant GM_addStyle
  26. // @grant GM_registerMenuCommand
  27. // @grant GM_getResourceText
  28. // @run-at document-start
  29. // ==/UserScript==
  30.  
  31. ;(function () {
  32. "use strict"
  33.  
  34. class Fonts {
  35. constructor(...fonts) {
  36. if (fonts.length === 1 && Array.isArray(fonts[0])) {
  37. this.fonts = fonts.split(",")
  38. } else {
  39. this.fonts = fonts
  40. }
  41. }
  42.  
  43. add(...fonts) {
  44. this.fonts.push(...fonts)
  45. }
  46.  
  47. remove(font) {
  48. let index = this.fonts.indexOf(font)
  49. if (index !== -1) {
  50. this.fonts.splice(index, 1)
  51. }
  52. }
  53.  
  54. toString(separator = ",") {
  55. return this.fonts.join(separator)
  56. }
  57. }
  58.  
  59. let default_settings = {
  60. presetFonts: new Fonts(
  61. "ui-monospace",
  62. "SFMono-Regular",
  63. "SF Mono",
  64. "Menlo",
  65. "Consolas",
  66. "Liberation Mono",
  67. "monospace"
  68. ),
  69. customFonts: new Fonts("CaskaydiaCove Nerd Font Propo"),
  70. }
  71.  
  72. const Symbol = {
  73. PLUGIN_NAME: "codeblock-icon-enhance",
  74. }
  75.  
  76. let kit = {
  77. /**
  78. * prompt
  79. * @param {Object} options
  80. * @param {string} options.title
  81. * @param {string} options.default_value
  82. * @param {Function} options.callback
  83. */
  84. prompt: function (options) {
  85. let { title, default_value, callback } = options
  86.  
  87. if (Swal) {
  88. Swal.fire({
  89. title,
  90. input: "text",
  91. inputValue: default_value,
  92. }).then(({ value }) => callback(value))
  93. } else {
  94. const value = window.prompt(title, default_value)
  95. callback(value)
  96. }
  97. },
  98. }
  99.  
  100. /**
  101. * add style
  102. * @param {string} id
  103. * @param {string} tag
  104. * @param {string} css
  105. */
  106. function addStyle(id, tag, css) {
  107. tag = tag || "style"
  108. let doc = document,
  109. styleDom = doc.getElementById(id)
  110. if (styleDom) styleDom.remove()
  111. let style = doc.createElement(tag)
  112. style.rel = "stylesheet"
  113. style.id = id
  114. tag === "style" ? (style.innerHTML = css) : (style.href = css)
  115. doc.getElementsByTagName("body")[0].appendChild(style)
  116. }
  117.  
  118. /**
  119. * apply fonts setting
  120. * @param fonts string
  121. * @returns {Fonts}
  122. */
  123. function applyFonts(fonts) {
  124. let codeblockFonts = fonts ? new Fonts(fonts) : default_settings.customFonts
  125.  
  126. GM_setValue(Symbol.PLUGIN_NAME, codeblockFonts.toString())
  127. addStyle(
  128. Symbol.PLUGIN_NAME,
  129. "style",
  130. ` :root :is(pre,pre *,code,code *,.cm-editor [class*='cm-'] *,.code,.code *,.blob-num,.blob-num *,.blob-code,.blob-code *,textarea,.react-line-numbers *,.react-code-lines *):not([class*='expand' i],[class*='collapse' i]) { font-family: ${default_settings.presetFonts.toString()},${codeblockFonts} !important; } `
  131. )
  132.  
  133. return codeblockFonts
  134. }
  135.  
  136. function main() {
  137. // init default settings
  138. addStyle("swal-pub-style", "style", GM_getResourceText("Swal"))
  139. let fonts = GM_getValue(Symbol.PLUGIN_NAME)
  140. let appliedFonts = applyFonts(fonts)
  141.  
  142. // init menu
  143. GM_registerMenuCommand("Font Settings", () => {
  144. kit.prompt({
  145. title: "Enter the font(s) you want to use for the code block",
  146. default_value: appliedFonts.toString(),
  147. callback: (value) => {
  148. appliedFonts = applyFonts(value)
  149. },
  150. })
  151. })
  152. }
  153. main()
  154. })()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址