Skip to content

Instantly share code, notes, and snippets.

@hyrious
Created July 12, 2020 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyrious/0dd035757615cd0678f564f2e2ea801e to your computer and use it in GitHub Desktop.
Save hyrious/0dd035757615cd0678f564f2e2ea801e to your computer and use it in GitHub Desktop.
油猴脚本
// ==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