GitHub Monospace Font Toggle

A userscript that adds monospace font toggle to comments

当前为 2016-09-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Monospace Font Toggle
  3. // @version 1.1.0
  4. // @description A userscript that adds monospace font toggle to comments
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace https://github.com/StylishThemes
  7. // @include https://github.com/*
  8. // @grant GM_addStyle
  9. // @run-at document-end
  10. // @author StylishThemes
  11. // ==/UserScript==
  12. /* global GM_addStyle */
  13. /* jshint esnext:true, unused:true */
  14. (function() {
  15. "use strict";
  16. /*
  17. This code is also part of the GitHub-Dark Script
  18. (https://github.com/StylishThemes/GitHub-Dark-Script)
  19. Extracted out into a separate userscript in case users
  20. only want to add this functionality
  21. */
  22. let busy = false;
  23.  
  24. const icon = `
  25. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewbox="0 0 32 32">
  26. <path d="M5.91 7.31v8.41c0 .66.05 1.09.14 1.31.09.21.23.37.41.48.18.11.52.16 1.02.16v.41H2.41v-.41c.5 0 .86-.05 1.03-.14.16-.11.3-.27.41-.5.11-.23.16-.66.16-1.3V11.7c0-1.14-.04-1.87-.11-2.2-.04-.26-.13-.42-.24-.53-.11-.1-.27-.14-.46-.14-.21 0-.48.05-.77.18l-.18-.41 3.14-1.28h.52v-.01zm-.95-5.46c.32 0 .59.11.82.34.23.23.34.5.34.82 0 .32-.11.59-.34.82-.23.22-.51.34-.82.34-.32 0-.59-.11-.82-.34s-.36-.5-.36-.82c0-.32.11-.59.34-.82.24-.23.51-.34.84-.34zm19.636 19.006h-3.39v-1.64h5.39v9.8h3.43v1.66h-9.18v-1.66h3.77v-8.16h-.02zm.7-6.44c.21 0 .43.04.63.13.18.09.36.2.5.34s.25.3.34.5c.07.18.13.39.13.61 0 .22-.04.41-.13.61s-.19.36-.34.5-.3.25-.5.32c-.2.09-.39.13-.62.13-.21 0-.43-.04-.61-.12-.19-.07-.35-.19-.5-.34-.14-.14-.25-.3-.34-.5-.07-.2-.13-.39-.13-.61s.04-.43.13-.61c.07-.18.2-.36.34-.5s.31-.25.5-.34c.17-.09.39-.12.6-.12zM2 30L27.82 2H30L4.14 30H2z"/>
  27. </svg>
  28. `;
  29.  
  30. // Add monospace font toggle
  31. function addMonospaceToggle() {
  32. busy = true;
  33. const button = document.createElement("button");
  34. button.type = "button";
  35. button.className = "ghd-monospace toolbar-item tooltipped tooltipped-n";
  36. button.setAttribute("aria-label", "Toggle monospaced font");
  37. button.setAttribute("tabindex", "-1");
  38. button.innerHTML = icon;
  39. Array.from(
  40. document.querySelectorAll(".toolbar-commenting")
  41. ).forEach(el => {
  42. if (el && !el.querySelector(".ghd-monospace")) {
  43. el.insertBefore(button.cloneNode(true), el.childNodes[0]);
  44. }
  45. });
  46. busy = false;
  47. }
  48.  
  49. function closest(el, selector) {
  50. while (el && el.nodeName !== "BODY" && !el.matches(selector)) {
  51. el = el.parentNode;
  52. }
  53. return el && el.matches(selector) ? el : [];
  54. }
  55.  
  56. function addBindings() {
  57. document.querySelector("body").addEventListener("click", function(event) {
  58. var textarea, active,
  59. target = event.target;
  60. if (target && target.classList.contains("ghd-monospace")) {
  61. textarea = closest(target, ".previewable-comment-form");
  62. textarea = textarea.querySelector(".comment-form-textarea");
  63. textarea.classList.toggle("ghd-monospace-font");
  64. textarea.focus();
  65. active = textarea.classList.contains("ghd-monospace-font");
  66. target.classList[active ? "add" : "remove"]("text-blue");
  67. return false;
  68. }
  69. });
  70. }
  71.  
  72. // don't initialize if GitHub Dark Script is active
  73. if (!document.querySelector("#ghd-menu")) {
  74. // monospace font toggle
  75. GM_addStyle(`
  76. .ghd-monospace-font { font-family: Menlo, Inconsolata, "Droid Mono",
  77. monospace !important; font-size: 1em !important; }
  78. .ghd-monospace > svg { pointer-events: none; }
  79. `);
  80.  
  81. Array.from(
  82. document.querySelectorAll(`
  83. #js-repo-pjax-container,
  84. #js-pjax-container,
  85. .js-preview-body
  86. `)
  87. ).forEach(target => {
  88. new MutationObserver(mutations => {
  89. mutations.forEach(mutation => {
  90. if (!busy && mutation.target === target) {
  91. addMonospaceToggle();
  92. }
  93. });
  94. }).observe(target, {
  95. childList: true,
  96. subtree: true
  97. });
  98. });
  99.  
  100. addBindings();
  101. addMonospaceToggle();
  102. }
  103. })();