Last active
September 10, 2020 18:40
-
-
Save martianyi/ba41ece11e2815bce17fff02793a8197 to your computer and use it in GitHub Desktop.
userscript to beautify js and css files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name jsbeautify | |
// @namespace https://gist.github.com/martianyi/ba41ece11e2815bce17fff02793a8197/ | |
// @version 0.1 | |
// @description beautify js and css files | |
// @author martianyi | |
// @match *://*/* | |
// @require https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.7.5/beautify.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.7.5/beautify-css.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js | |
// @resource hlCSS https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css | |
// @grant GM_addStyle | |
// @grant GM_getResourceText | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var hlCSS = GM_getResourceText ("hlCSS"); | |
GM_addStyle (hlCSS); | |
var isJS = location.href.match(/\.js(?:\?|$)/); | |
var isCSS = location.href.match(/\.css(?:\?|$)/); | |
var preEl = document.querySelector('pre'); | |
var btn; | |
if (preEl && (isJS || isCSS)) { | |
btn = document.createElement('button'); | |
btn.textContent = 'beautify'; | |
btn.addEventListener('click', beautify); | |
document.body.insertBefore(btn, preEl); | |
} | |
function beautify() { | |
var code = preEl.textContent; | |
if (isJS) code = js_beautify(code); | |
else if (isCSS) code = css_beautify(code); | |
document.body.removeChild(btn); | |
var codeEl = document.createElement('code'); | |
codeEl.textContent = code; | |
preEl.textContent = ''; | |
preEl.appendChild(codeEl); | |
hljs.initHighlighting(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment