// ==UserScript== // @name GitHub Monospace Font Toggle // @version 1.2.4 // @description A userscript that adds monospace font toggle to comments // @license MIT // @author StylishThemes // @namespace https://github.com/StylishThemes // @include https://github.com/* // @run-at document-idle // @grant GM_addStyle // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=634242 // @icon https://avatars3.githubusercontent.com/u/6145677?v=3&s=200 // @updateURL https://raw.githubusercontent.com/StylishThemes/GitHub-Dark-Script/master/github-script-monospace-toggle.user.js // @downloadURL https://raw.githubusercontent.com/StylishThemes/GitHub-Dark-Script/master/github-script-monospace-toggle.user.js // @homepageURL https://github.com/StylishThemes/GitHub-Dark-Script // ==/UserScript== (() => { "use strict"; // This code is also part of the GitHub-Dark Script // (https://github.com/StylishThemes/GitHub-Dark-Script) // Extracted out into a separate userscript in case users // only want to add this functionality const icon = ` `; // Add monospace font toggle function addMonospaceToggle() { const button = document.createElement("button"); button.type = "button"; button.className = "ghd-monospace toolbar-item tooltipped tooltipped-n"; button.setAttribute("aria-label", "Toggle monospaced font"); button.setAttribute("tabindex", "-1"); button.innerHTML = icon; $$(".toolbar-commenting").forEach(el => { if (el && !$(".ghd-monospace", el)) { el.insertBefore(button.cloneNode(true), el.childNodes[0]); } }); } function addBindings() { $("body").addEventListener("click", event => { let textarea, active; const target = event.target; if (target && target.classList.contains("ghd-monospace")) { textarea = closest(".previewable-comment-form", target); textarea = $(".comment-form-textarea", textarea); textarea.classList.toggle("ghd-monospace-font"); textarea.focus(); active = textarea.classList.contains("ghd-monospace-font"); target.classList.toggle("text-blue", active); return false; } }); } function $(str, el) { return (el || document).querySelector(str); } function $$(str, el) { return Array.from((el || document).querySelectorAll(str)); } function closest(selector, el) { while (el && el.nodeType === 1) { if (el.matches(selector)) { return el; } el = el.parentNode; } return null; } // don't initialize if GitHub Dark Script is active if (!$("#ghd-menu")) { // monospace font toggle GM_addStyle(` .ghd-monospace-font { font-family: Menlo, Inconsolata, "Droid Mono", monospace !important; font-size: 1em !important; } .ghd-monospace > svg { pointer-events: none; } `); document.addEventListener("ghmo:container", addMonospaceToggle); document.addEventListener("ghmo:comments", addMonospaceToggle); addBindings(); addMonospaceToggle(); } })();