Skip to content

Instantly share code, notes, and snippets.

@sh0ji
Last active August 6, 2020 03:33
Show Gist options
  • Save sh0ji/374dd7756e1b375dc0ebbe583baf73da to your computer and use it in GitHub Desktop.
Save sh0ji/374dd7756e1b375dc0ebbe583baf73da to your computer and use it in GitHub Desktop.
Set a tab size for code on the web
// ==UserScript==
// @name Tab Size
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Set the tab size in code views
// @author Evan Yamanishi
// @match *://*/*
// @grant none
// @run-at document-start
// ==/UserScript==
(function () {
'use strict';
// Your chosen tab size. Change this to whatever you want!
const TAB_SIZE_DEFAULT = 4;
// The elements that should use this tab size
const SELECTORS = [
'.highlight > pre > .line span:first-child:not(:only-child)',
'.prism-code .token-line > .token',
'.blob-code-inner',
'code',
'pre',
'tt',
].join(', ');
// The list of CSS properties that should have the tab size
const props = (tabSize) => [
'-moz-tab-size',
'-o-tab-size',
'tab-size',
].map((prop) => `${prop}: ${tabSize} !important;`).join('\n\t');
window.setTabSize = (tabSize = TAB_SIZE_DEFAULT) => {
const ID = 'sh0ji-tab-size';
const styleElement = document.getElementById(ID) || document.createElement('style');
styleElement.id = ID;
styleElement.innerHTML = `${SELECTORS} { ${props(tabSize)} }`;
document.head.appendChild(styleElement);
};
window.setTabSize();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment