-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShow-Markdown.user.js
184 lines (171 loc) · 6.88 KB
/
Show-Markdown.user.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// ==UserScript==
// @name Show Markdown
// @namespace work.pythoner
// @match *://*acwing.com/*
// @match *://*jisuanke.com/*
// @match *://*leetcode.cn/*
// @match *://*luogu.com.cn/*
// @match *://*segmentfault.com/*
// @match *://*/*blog*
// @match *://*/*post*
// @match *://*/*problem*
// @require https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js
// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/showdown.min.js
// @run-at document-idle
// @grant GM_setClipboard
// @version 1.1
// @author Hanson Hu
// @description 5/4/2022, 6:12:01 PM
// @homepage https://blog.pythoner.work
// @icon https://blog.pythoner.work/icons/markdown.png
// @license MIT
// ==/UserScript==
(function() {
function getTargets() {
let ret = [], old = null;
$('h1,h2,h3,h4,p,pre').each(function() {
let parent = $(this).parent();
if ((!old || parent.get(0) != old.get(0)) &&
parent.children('h1,h2,h3,h4,p,pre').length >= 2 &&
parent.children('div:visible').length == 0) {
ret.push(parent);
old = parent;
}
});
return ret;
}
function getMarkdown(src, escape = false) {
// Clean up
let doc = document.createElement('div');
doc.innerHTML = src;
// segmentfault
for (const e of doc.querySelectorAll('.widget-codetool'))
e.remove();
// MathJax (AcWing)
for (const e of doc.querySelectorAll('.MathJax_Preview,.MathJax'))
e.remove();
// KaTeX (计蒜客)
for (const e of doc.querySelectorAll('mrow,.katex-html'))
e.remove();
let text = cleanDomTree(doc, escape);
// Remove redundant tags and "copy" buttons
text = text.replace(/<!--\s*-->\s*/gi, '')
.replace(/<span.*?>/gi, '')
.replace(/<\/span>/gi, '')
.replace(/\s*<button.*?<\/button>\s*/gi, '');
// MathJax
text = text.replace(/<script.*?>/gi, '$')
.replace(/<\/script>/gi, '$');
// KaTeX
text = text.replace(/<math.*?<annotation.*?>/gi, '$')
.replace(/<\/annotation>.*?<\/math>/gi, '$');
// Avoid the bug of showdown dealing with <br />
// https://github.com/showdownjs/showdown/issues/649
text = text.replace(/<br[ ]?[/]?>\s*/gi, '%line-break%');
let md = converter.makeMarkdown(text);
// Extra comment added with <ol> and <ul>
// https://github.com/showdownjs/showdown/issues/700
// Image URL
// https://github.com/showdownjs/showdown/issues/925
// And restore line breaks
md = md.replace(/\s*<!--\s*-->\s*/gi, '\n\n')
.replace(/]\(<(.+?)>\)/gi, ']($1)')
.replace(/%line-break%/gi, ' \n')
.trim();
return md + '\n';
}
function cleanDomTree(doc, escape) {
let nodes = doc.childNodes,
ret = '';
for (let i = 0; i < nodes.length; i ++ ) {
let node = nodes[i];
if (node.nodeType === 1) {
if (node.nodeName.toLowerCase() === 'pre') {
let c = node.firstChild;
if (c.nodeType === 1 && c.nodeName.toLowerCase() === 'code') {
let cls = node.getAttribute('class');
if (cls && cls.search(/^hljs /) != -1) {
let ls = cls.split(' ');
node.firstChild.setAttribute('data-language', ls[ls.length - 1]);
}
if (escape)
node.firstChild.textContent = escapeHtml(node.textContent);
else
node.firstChild.textContent = node.textContent;
ret += node.outerHTML + '\n';
} else {
// 计蒜客
ret += '<pre><code>' + node.textContent + '<\/pre><\/code>\n';
}
} else {
ret += node.outerHTML + '\n';
}
}
}
return ret;
}
function escapeHtml(unsafe) {
return unsafe.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
let converter = new showdown.Converter();
converter.setFlavor('github');
// Wait for JS loading (力扣)
setTimeout(function() {
let targets = getTargets();
targets.forEach(function(elem) {
let parent = elem.get(0).parentNode;
parent.setAttribute('style', 'position: relative;');
let offset = 0, offset2 = 30;
// Prevent overlapping
if (parent.cnt === undefined) {
parent.cnt = 1;
} else {
offset = parent.cnt * 60, offset2 = offset + 30;
parent.cnt ++ ;
}
$(elem).after(
'<div style="position: absolute; top: ' + offset + 'px; right: 0px;">' +
'<button class="btn-show" style="color: red; opacity: 0.2;">显示Markdown</button></div>' +
'<div style="position: absolute; top: ' + offset2 + 'px; right: 0px;">' +
'<button class="btn-copy" style="color: red; opacity: 0.2;">复制</button></div>'
);
});
$('.btn-show').click(function() {
let target = $(this).parent().prev().get(0);
if (target.flag) {
target.flag = false;
$(this).text('显示Markdown');
$(target).html(target.orig);
} else {
target.flag = true;
if (!target.orig)
target.orig = $(target).html();
if (!target.md_html)
target.md_html = getMarkdown($(target).html(), true);
$(this).text('显示HTML');
$(target).html(
'<pre style="max-height: none; white-space: pre-wrap; word-wrap: break-word; ' +
'font-family: Consolas, monospace;">' +
'<code class="nohighlight" style="white-space: pre-wrap;">' +
target.md_html +
'</code></pre>'
);
}
});
$('.btn-copy').click(function() {
let target = $(this).parent().prev().prev().get(0);
if (target.flag) {
if (!target.md)
target.md = getMarkdown(target.orig);
} else {
if (!target.md)
target.md = getMarkdown($(target).html());
}
GM_setClipboard(target.md);
});
}, 2000);
})();