-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
69 lines (63 loc) · 2.06 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
if (document.contentType !== 'text/html' && (document.contentType.startsWith('text/') || document.contentType.startsWith('application/')) && document.body.firstChild.tagName === 'PRE') {
let code = document.body.firstChild.textContent;
let ext = location.pathname.split('.').pop().toLowerCase();
if (ext.length > 9 || !/^\w+$/.test(ext)) ext = document.contentType.split('/').pop().toLowerCase();
if (['cjs', 'mjs'].includes(ext)) ext = 'js';
if (['cts', 'mts'].includes(ext)) ext = 'ts';
switch (ext) {
case 'html':
code = html_beautify(code);
break;
case 'css':
case 'less':
case 'scss':
case 'sass':
code = css_beautify(code);
break;
case 'js':
case 'jsx':
case 'javascript':
case 'ts':
case 'tsx':
case 'json':
code = js_beautify(code);
break;
}
highlightCode(code, ext);
}
async function highlightCode(code, ext) {
const style = `<style>
body {
background-color: #1e1e1e;
}
code {
counter-increment: step 0;
counter-reset: step;
font-family: Consolas, monospace;
font-size: 0.875rem;
line-height: 1.125rem;
white-space: pre-wrap;
}
.line::before {
display: inline-block;
counter-increment: step;
margin-right: 1rem;
width: 2.5rem;
content: counter(step);
color: #6e7681;
text-align: right
}
</style>`;
if (location.hostname === 'raw.githubusercontent.com') {
chrome.runtime.sendMessage({ code, ext }, response => {
if (response) document.body.innerHTML = response + style;
});
} else {
const highlighter = await shiki.getHighlighter({
theme: 'dark-plus',
langs: []
});
await highlighter.loadLanguage(ext);
document.body.innerHTML = highlighter.codeToHtml(code, { lang: ext }) + style;
}
}