Skip to content

Commit 3b4e677

Browse files
committed
refactor: use dompurify in view/list/list.html too
1 parent 124eec3 commit 3b4e677

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

view/list/list.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,19 @@ String.prototype.trunc = String.prototype.trunc || function(n){
77
var list = {
88
last_selected_index: null,
99

10-
_strip: html => html.replace(/<(?:.|\n)*?>/gm, ''),
10+
domParser: null,
11+
domPurify: null,
12+
async getDomPurify() {
13+
if (this.domPurify) return this.domPurify
14+
const m = await import('../../vendor/dompurify.js')
15+
return this.domPurify = m.default
16+
},
17+
html2text(h) {
18+
const clean = this.domPurify.sanitize(h)
19+
const dp = this.domParser
20+
const d = dp.parseFromString(clean, 'text/html')
21+
return d.firstChild.textContent
22+
},
1123

1224
_escapeHTML: str => str.replace(/[&"'<>]/g, m => ({
1325
"&": "&amp;",
@@ -37,13 +49,15 @@ var list = {
3749
});
3850
},
3951

40-
init: () => {
52+
init: async () => {
4153
var me = list;
4254

43-
runtime.sendMessage({
44-
behavior: 'load'
45-
}).then( ( res => {
46-
55+
me.domParser = new DOMParser()
56+
const [res, domPurify] = await Promise.all([
57+
runtime.sendMessage({behavior: 'load'}),
58+
me.getDomPurify()
59+
])
60+
{
4761
if (!(res && res.data)) return false;
4862

4963
delete res.data.version;
@@ -58,8 +72,7 @@ var list = {
5872
me.onSelect();
5973
me.initDelBtn();
6074
me.initSearchBar();
61-
}));
62-
75+
}
6376
me.onDelete();
6477
},
6578

@@ -107,7 +120,7 @@ var list = {
107120
<td><a href="${cache.url}">${cache.url}</a></td>
108121
<td>
109122
<a class="open-frame" href="./entity.html?id=${encodeURI(cache.key)}" target="detail-frame">
110-
${me._escapeHTML(me._strip(cache.val)).trunc(120)}
123+
${me._escapeHTML(me.html2text(cache.val)).trunc(120)}
111124
</a>
112125
</td>
113126
<td>${time.toLocaleString()}</td>

view/lite/lite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var panel = {
4141
return timestamp + ' ' + else_info;
4242
},
4343

44+
domPurify: null,
4445
async getDomPurify() {
4546
if (this.domPurify) return this.domPurify
4647
const m = await import('../../vendor/dompurify.js')

0 commit comments

Comments
 (0)