Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
stripping html from native notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
rmisio committed Oct 25, 2017
1 parent 0f2cdab commit 931205a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 14 additions & 0 deletions js/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '';
}
10 changes: 9 additions & 1 deletion js/utils/notification.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'underscore';
import { ipcRenderer } from 'electron';
import { stripHtml } from './dom';

let unreadNotifCount = 0;
let unreadChatMsgCount = 0;
Expand Down Expand Up @@ -65,12 +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.');
}

const notifOptions = {
silent: true,
...(_.omit(options || {}, 'onclick', 'onerror')),
body: options.body ? stripHtml(options.body) : '',
};

const notif = new Notification(notifTitle, notifOptions);
const title = typeof notifTitle === 'string' ? stripHtml(notifTitle) : notifTitle;
const notif = new Notification(title, notifOptions);

if (typeof options.onclick === 'function') {
notif.addEventListener('click', notifOptions.onclick);
Expand Down

0 comments on commit 931205a

Please sign in to comment.