Added Parser
Browse files- README.md +6 -1
- index.html +209 -19
README.md
CHANGED
@@ -8,4 +8,9 @@ pinned: false
|
|
8 |
license: apache-2.0
|
9 |
---
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
8 |
license: apache-2.0
|
9 |
---
|
10 |
|
11 |
+
Parse Community Highlights
|
12 |
+
|
13 |
+
Steps:
|
14 |
+
|
15 |
+
1. Copy content from Discord Web
|
16 |
+
1. Paste into "Content" textarea
|
index.html
CHANGED
@@ -1,19 +1,209 @@
|
|
1 |
-
|
2 |
-
<
|
3 |
-
|
4 |
-
<
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<html>
|
2 |
+
<head>
|
3 |
+
|
4 |
+
<script type="text/javascript">
|
5 |
+
let CURRENT_CONTENT;
|
6 |
+
let parser = new DOMParser()
|
7 |
+
|
8 |
+
function FormatDiscordMessage(html){
|
9 |
+
let dom = parser.parseFromString(html, "text/html");
|
10 |
+
CURRENT_CONTENT.dom = dom;
|
11 |
+
|
12 |
+
let allChilds = dom.querySelectorAll("body > *");
|
13 |
+
|
14 |
+
let FullContent = [];
|
15 |
+
|
16 |
+
let buffContent = []
|
17 |
+
let buffLinks = []
|
18 |
+
let authorName= [];
|
19 |
+
let edition;
|
20 |
+
|
21 |
+
let flushContent = function(){
|
22 |
+
console.log("line break found!");
|
23 |
+
|
24 |
+
let AllText = buffContent.join("").trim();
|
25 |
+
let links = buffLinks.join(",");
|
26 |
+
|
27 |
+
if(!links)
|
28 |
+
return;
|
29 |
+
|
30 |
+
FullContent.push({
|
31 |
+
text: AllText
|
32 |
+
,links
|
33 |
+
,authors: authorName.join(",")
|
34 |
+
,edition
|
35 |
+
});
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
buffContent = []
|
40 |
+
buffLinks = [];
|
41 |
+
authorName = []
|
42 |
+
}
|
43 |
+
|
44 |
+
for(let c of allChilds){
|
45 |
+
let text;
|
46 |
+
|
47 |
+
if(c.tagName == "A")
|
48 |
+
buffLinks.push(c.href);
|
49 |
+
|
50 |
+
|
51 |
+
if(c.tagName.at(0) == 'H'){
|
52 |
+
|
53 |
+
let editionMatch = c.textContent.match(/#\d+/g);
|
54 |
+
|
55 |
+
if(editionMatch){
|
56 |
+
edition = parseInt( editionMatch[0].replace('#',''))
|
57 |
+
}
|
58 |
+
|
59 |
+
continue;
|
60 |
+
}
|
61 |
+
|
62 |
+
if(c.classList.contains("mention"))
|
63 |
+
authorName.push(c.textContent);
|
64 |
+
|
65 |
+
text = c.textContent;
|
66 |
+
|
67 |
+
if(text)
|
68 |
+
buffContent.push(text);
|
69 |
+
|
70 |
+
if(/\!?\s*\n+/.test(text)){
|
71 |
+
flushContent();
|
72 |
+
}
|
73 |
+
}
|
74 |
+
|
75 |
+
|
76 |
+
// last
|
77 |
+
if(buffContent){
|
78 |
+
flushContent();
|
79 |
+
}
|
80 |
+
|
81 |
+
|
82 |
+
return { dom, content: FullContent };
|
83 |
+
}
|
84 |
+
|
85 |
+
function ProcessPastedMessage(){
|
86 |
+
let res = FormatDiscordMessage(CURRENT_CONTENT.html)
|
87 |
+
|
88 |
+
CURRENT_CONTENT.result = res;
|
89 |
+
|
90 |
+
let out = document.querySelector("#result");
|
91 |
+
|
92 |
+
let xDoc = document.implementation.createDocument(null, "highlights");
|
93 |
+
let rootDoc = xDoc.querySelector("highlights");
|
94 |
+
|
95 |
+
let Stats = {
|
96 |
+
total: 0
|
97 |
+
,edition: null
|
98 |
+
};
|
99 |
+
|
100 |
+
Stats.edition = res.content[0].edition;
|
101 |
+
|
102 |
+
for(let [i,high] of res.content.entries()){
|
103 |
+
let xHigh = xDoc.createElement("highlight");
|
104 |
+
|
105 |
+
xHigh.setAttribute("autor", high.authors);
|
106 |
+
xHigh.setAttribute("links", high.links);
|
107 |
+
xHigh.setAttribute("edition", high.edition);
|
108 |
+
xHigh.textContent = high.text;
|
109 |
+
|
110 |
+
rootDoc.appendChild(xHigh)
|
111 |
+
|
112 |
+
Stats.total++
|
113 |
+
|
114 |
+
}
|
115 |
+
|
116 |
+
|
117 |
+
document.querySelector("#stats").innerHTML = `Stats: total = ${Stats.total}, edition = ${Stats.edition}`
|
118 |
+
|
119 |
+
let serializer = new XMLSerializer();
|
120 |
+
out.innerHTML = serializer.serializeToString(xDoc);
|
121 |
+
}
|
122 |
+
|
123 |
+
|
124 |
+
function ProcessPasted(content){
|
125 |
+
|
126 |
+
navigator.clipboard.read(["text/html"])
|
127 |
+
.then( async (content) => {
|
128 |
+
|
129 |
+
console.log("content", content[0].types)
|
130 |
+
|
131 |
+
let contentTypes = content[0].types;
|
132 |
+
let plainText = await (await content[0].getType("text/plain")).text();
|
133 |
+
let html = null;
|
134 |
+
|
135 |
+
if(contentTypes.includes("text/html")){
|
136 |
+
htmlContent = await content[0].getType("text/html");
|
137 |
+
console.log("html:", htmlContent);
|
138 |
+
|
139 |
+
html = await htmlContent.text();
|
140 |
+
|
141 |
+
CURRENT_CONTENT = {
|
142 |
+
html: await htmlContent.text()
|
143 |
+
,text: plainText
|
144 |
+
}
|
145 |
+
|
146 |
+
} else {
|
147 |
+
console.log("NotContainsHtml");
|
148 |
+
}
|
149 |
+
|
150 |
+
|
151 |
+
CURRENT_CONTENT = {
|
152 |
+
html
|
153 |
+
,text: plainText
|
154 |
+
}
|
155 |
+
|
156 |
+
setTimeout(ProcessPastedMessage, 100)
|
157 |
+
|
158 |
+
})
|
159 |
+
|
160 |
+
return false;
|
161 |
+
}
|
162 |
+
|
163 |
+
addEventListener("paste", ProcessPasted);
|
164 |
+
|
165 |
+
|
166 |
+
</script>
|
167 |
+
<style>
|
168 |
+
textarea {
|
169 |
+
width: 100%;
|
170 |
+
}
|
171 |
+
|
172 |
+
.container {
|
173 |
+
display: flex;
|
174 |
+
flex-direction: row;
|
175 |
+
}
|
176 |
+
|
177 |
+
.container > div {
|
178 |
+
width: 50%;
|
179 |
+
height: 70vh;
|
180 |
+
padding: 5px;
|
181 |
+
}
|
182 |
+
|
183 |
+
.container textarea {
|
184 |
+
height: 100%;
|
185 |
+
}
|
186 |
+
</style>
|
187 |
+
|
188 |
+
</head>
|
189 |
+
<body>
|
190 |
+
<div>
|
191 |
+
<p id="stats"></p>
|
192 |
+
</div>
|
193 |
+
<div class="container">
|
194 |
+
<div>
|
195 |
+
<p>Content</p>
|
196 |
+
<textarea></textarea>
|
197 |
+
</div>
|
198 |
+
|
199 |
+
<div>
|
200 |
+
<p>XML</p>
|
201 |
+
<textarea readonly id="result"></textarea>
|
202 |
+
</div>
|
203 |
+
</div>
|
204 |
+
|
205 |
+
|
206 |
+
|
207 |
+
|
208 |
+
</body>
|
209 |
+
</html>
|