Skip to content

Commit dcfe5a0

Browse files
committed
use DOMParser instead of regex to get text from html
1 parent 3517f90 commit dcfe5a0

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/background/background.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import browser from 'webextension-polyfill'
22
import api from './api/owncast'
33
import Storage from './util/storage'
4+
import stripHtml from '../shared/util/stripHtml'
45
import urlcat from 'urlcat'
56

67
function dateDiff (timestamp, structure = dateDiff.structure) {
@@ -80,7 +81,7 @@ const sendNotifications = async (oldData, newData) => {
8081
browser.notifications.create({
8182
type: 'basic',
8283
title: item.name + ' is online',
83-
message: item.description.replace(/<\/?[^>]+>/ig, ' '),
84+
message: stripHtml(item.description),
8485
iconUrl: item.logo,
8586
})
8687
})

src/popup/components/Instance.vue

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
>{{ instance.status }}</span></a>
2626
</div>
2727
<div class="instance-text-description">
28-
{{ instance.description | stripHTML }}
28+
{{ instanceDescription }}
2929
</div>
3030
<div class="instance-icon-bar">
3131
<div id="left">
@@ -71,6 +71,7 @@
7171
</template>
7272

7373
<script>
74+
import stripHtml from '../../shared/util/stripHtml'
7475
import ViewerIcon from './icons/ViewerIcon.vue'
7576
import UptimeIcon from './icons/UptimeIcon.vue'
7677
import RemoveIcon from './icons/RemoveIcon.vue'
@@ -82,11 +83,6 @@ export default {
8283
UptimeIcon,
8384
RemoveIcon,
8485
},
85-
filters: {
86-
stripHTML: function (string) {
87-
return string.replace(/<\/?[^>]+>/ig, ' ')
88-
},
89-
},
9086
props: {
9187
instance: {
9288
type: Object,
@@ -104,6 +100,9 @@ export default {
104100
'background-image': 'url(' + (this.instance.online ? this.instance.thumbnail : this.instance.logo) + ')',
105101
}
106102
},
103+
instanceDescription () {
104+
return stripHtml(this.instance.description)
105+
},
107106
},
108107
methods: {
109108
toggleRemove () {

src/shared/util/stripHtml.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Extracts the plain text of a string containing html
3+
* @param {string} html html to extract text from
4+
* @returns {string} text without html tags
5+
*/
6+
export default function stripHtml (html) {
7+
const parser = new DOMParser()
8+
const doc = parser.parseFromString(html, 'text/html')
9+
return doc.querySelector('body').innerText
10+
}

0 commit comments

Comments
 (0)