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

Commit

Permalink
Merge pull request #1063 from OpenBazaar/1045
Browse files Browse the repository at this point in the history
stripping html from native notifications
  • Loading branch information
jjeffryes authored Oct 25, 2017
2 parents 44d4934 + bcb274b commit 817f5b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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 || '';
}
11 changes: 11 additions & 0 deletions 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,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);
Expand Down

0 comments on commit 817f5b8

Please sign in to comment.