Skip to content

Instantly share code, notes, and snippets.

@nascarsayan
Last active March 28, 2021 06:39
Show Gist options
  • Save nascarsayan/19706d2042bbe4f963c610462c8eb6e8 to your computer and use it in GitHub Desktop.
Save nascarsayan/19706d2042bbe4f963c610462c8eb6e8 to your computer and use it in GitHub Desktop.
Tampermonkey script for customizing the code UI.
// ==UserScript==
// @name Code customizer
// @description Custom css setter : 1. Set monospace font on websites 2. Set tab width
// @namespace local.greasemonkey.codecustomizer
// @include *
// @version 1
// @grant none
// @author [email protected]
// ==/UserScript==
const monoFont = "Jetbrains Mono, Fira Code, monospace" // Set your monospace font here.
const tabWidth = 2 // Set your tab width
// Function helper to inject css
function resetMonoFont() {
Array.from(document.querySelectorAll("code, pre")).forEach(el => {
let style = window.getComputedStyle(el)
el.style.removeProperty('font-family')
el.style.setProperty('font-family', monoFont, 'important')
})
}
function addGlobalStyle(css) {
let head, style
head = document.getElementsByTagName('head')[0]
if (!head) { return }
style = document.createElement('style')
style.type = 'text/css'
style.innerHTML = css
head.appendChild(style)
}
// Apply the font-family definition to code styles.
let selectors = [
'code', // General
'pre', // General
'.monospace', // General
'.blob-viewer', // Gitlab
'.blob-code', // Github
'.blob-num', // Github
'.code', // General
// 'td.code div.container code', // Geeksforgeeks,
]
//
let cssRules = `* { tab-size: ${tabWidth} !important; }`
for (let i = 0; i < selectors.length; i++) {
cssRules = `${cssRules} ${selectors[i]}, ${selectors[i]} > * { font-family: ${monoFont} !important; }`
}
resetMonoFont()
addGlobalStyle(cssRules)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment