Created
July 12, 2020 16:42
-
-
Save hyrious/0dd035757615cd0678f564f2e2ea801e to your computer and use it in GitHub Desktop.
油猴脚本
This file contains 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 本地文件加高亮 | |
// @namespace Violentmonkey Scripts | |
// @match file:///* | |
// @grant none | |
// @version 1.0 | |
// @author hyrious | |
// @description 浏览器就是用来看代码的 | |
// ==/UserScript== | |
void (function() { | |
function $(sel) { | |
return document.querySelector(sel) | |
} | |
function C(tag, attr, ...children) { | |
const el = document.createElement(tag) | |
if (attr) Object.assign(el, attr) | |
el.append(...children) | |
return el | |
} | |
const a = location.href.split('.') | |
const ext = a[a.length - 1] | |
const b = location.href.split('/') | |
const file = b[b.length - 1] | |
function importScript(src) { | |
const script = C('script', { src }) | |
return new Promise(r => { | |
script.onload = () => r(script) | |
document.body.append(script) | |
}) | |
} | |
(async function () { | |
const now = performance.now() | |
if (ext === 'js') { | |
const p1 = importScript('https://cdn.jsdelivr.net/npm/prettier@2.0.5/standalone.js') | |
const p2 = importScript('https://cdn.jsdelivr.net/npm/prettier@2.0.5/parser-babel.js') | |
await Promise.all([p1, p2]) | |
const text = prettier.format($('pre').textContent, { parser: 'babel', plugins: prettierPlugins, tabWidth: 4, printWidth: 100 }) | |
$('pre').textContent = text; | |
} | |
const link = C('link', { rel: 'stylesheet', href: 'https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.1/build/styles/ir-black.min.css' }) | |
document.head.append(link) | |
await importScript('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@10.1.1/build/highlight.min.js') | |
if (ext) $('pre').classList.add(ext) | |
hljs.highlightBlock($('pre')) | |
$('pre').style.background = 'initial' | |
const t = performance.now() - now | |
document.title = file + ' - ' + t.toFixed(2) + 'mss' | |
})(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment