// ==UserScript== // @name GitHub Toggle Code Wrap // @version 1.1.0 // @description A userscript that adds a code wrap toggle button // @license https://creativecommons.org/licenses/by-sa/4.0/ // @author StylishThemes // @namespace https://github.com/StylishThemes // @include https://github.com/* // @run-at document-idle // @grant GM_registerMenuCommand // @grant GM_getValue // @grant GM_setValue // @grant GM_addStyle // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=188090 // @icon https://avatars3.githubusercontent.com/u/6145677?v=3&s=200 // @downloadURL none // ==/UserScript== /* jshint esnext:true, unused:true */ (() => { "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 */ // set by GM popup menu let globalWrap = GM_getValue("github-global-code-wrap", true); const wrapIcon = ` `, // inline code wrap css wrapCss = { "wrapped": "white-space: pre-wrap !important; word-break: break-all !important; overflow-wrap: break-word !important; display: block !important;", "unwrap": "white-space: pre !important; word-break: normal !important; display: block !important;" }; function findWrap(event) { const target = event.target; if (target.classList.contains("ghd-wrap-toggle")) { toggleClasses(target); } } // equivalent to .next("code, pre, .highlight, .diff-table"); function findNext(el) { const nextSib = el.nextElementSibling; if ( /code|pre/i.test(nextSib.nodeName) || nextSib && ( nextSib.classList.contains("highlight") || nextSib.classList.contains("diff-table") ) ) { return nextSib; } return el; } function toggleClasses(icon) { let css, code = findNext(icon); if ($("code", code)) { code = $("code", code); } if (!code) { console.error("Code wrap icon associated code not found", icon); return; } // code with line numbers if (code.nodeName === "TABLE") { if (code.className.indexOf("wrap-table") < 0) { css = !globalWrap; } else { css = code.classList.contains("ghd-unwrap-table"); } if (css) { code.classList.add("ghd-wrap-table"); code.classList.remove("ghd-unwrap-table"); icon.classList.add("wrapped"); icon.classList.remove("unwrap"); } else { code.classList.remove("ghd-wrap-table"); code.classList.add("ghd-unwrap-table"); icon.classList.remove("wrapped"); icon.classList.add("unwrap"); } } else { css = code.getAttribute("style") || ""; if (css === "") { css = wrapCss[globalWrap ? "unwrap" : "wrapped"]; } else { css = wrapCss[css === wrapCss.wrapped ? "unwrap" : "wrapped"]; } code.setAttribute("style", css); if (css === wrapCss.wrapped) { icon.classList.add("wrapped"); icon.classList.remove("unwrap"); } else { icon.classList.add("unwrap"); icon.classList.remove("wrapped"); } } } function getPrevSib(el, name) { let prev = el.previousSibling; while (prev) { if (prev.nodeType !== 1 || !prev.classList.contains(name)) { prev = prev.previousSibling; } else { return prev; } } return null; } // Add code wrap toggle function buildCodeWrap() { // add wrap code icons let tmp, wrapper = $$(".blob-wrapper"), indx = wrapper ? wrapper.length : 0, //