diff --git a/js/utils/dom.js b/js/utils/dom.js index 86f7814c5..d5611de1d 100644 --- a/js/utils/dom.js +++ b/js/utils/dom.js @@ -49,3 +49,17 @@ export function insertAtCursor(myField, myValue) { myField.value += myValue; } } + +/** + * Returns a string of text with html stripped out. + * (https://stackoverflow.com/a/822486/632806) + */ +export function stripHtml(text) { + if (typeof text !== 'string') { + throw new Error('Please provide text as a string.'); + } + + const el = document.createElement('div'); + el.innerHTML = text; + return el.textContent || el.innerText || ''; +} diff --git a/js/utils/notification.js b/js/utils/notification.js index 9e14dbe03..86fb14b26 100644 --- a/js/utils/notification.js +++ b/js/utils/notification.js @@ -1,5 +1,6 @@ import _ from 'underscore'; import { ipcRenderer } from 'electron'; +import { stripHtml } from './dom'; let unreadNotifCount = 0; let unreadChatMsgCount = 0; @@ -65,9 +66,19 @@ function playNotifSound() { } export function launchNativeNotification(notifTitle = '', options = {}) { + if (options.body !== undefined && typeof options.body !== 'string') { + throw new Error('If providing a notification body, it must be provided ' + + 'as a string.'); + } + + if (typeof notifTitle !== 'string') { + throw new Error('Please provide the notifTitle as a string.'); + } + const notifOptions = { silent: true, ...(_.omit(options || {}, 'onclick', 'onerror')), + body: options.body ? stripHtml(options.body) : '', }; const notif = new Notification(notifTitle, notifOptions);