diff --git a/src/client/state/Notifications.js b/src/client/state/Notifications.js index c3ec90c..90f9b4a 100644 --- a/src/client/state/Notifications.js +++ b/src/client/state/Notifications.js @@ -37,6 +37,8 @@ class Notifications extends EventEmitter { constructor(roomList) { super(); + this.initialized = false; + this.favicon = LogoSVG; this.matrixClient = roomList.matrixClient; this.roomList = roomList; @@ -50,6 +52,7 @@ class Notifications extends EventEmitter { } async _initNoti() { + this.initialized = false; this.roomIdToNoti = new Map(); const addNoti = (roomId) => { @@ -63,6 +66,8 @@ class Notifications extends EventEmitter { }; [...this.roomList.rooms].forEach(addNoti); [...this.roomList.directs].forEach(addNoti); + + this.initialized = true; this._updateFavicon(); } @@ -136,6 +141,7 @@ class Notifications extends EventEmitter { } async _updateFavicon() { + if (!this.initialized) return; let unread = false; let highlight = false; [...this.roomIdToNoti.values()].find((noti) => { @@ -146,11 +152,16 @@ class Notifications extends EventEmitter { if (unread && highlight) return true; return false; }); - if (!unread) { - setFavicon(LogoSVG); - return; + let newFavicon = LogoSVG; + if (unread && !highlight) { + newFavicon = LogoUnreadSVG; } - setFavicon(highlight ? LogoHighlightSVG : LogoUnreadSVG); + if (unread && highlight) { + newFavicon = LogoHighlightSVG; + } + if (newFavicon === this.favicon) return; + this.favicon = newFavicon; + setFavicon(this.favicon); } _setNoti(roomId, total, highlight) { diff --git a/src/util/common.js b/src/util/common.js index c2a17cb..87b0b7e 100644 --- a/src/util/common.js +++ b/src/util/common.js @@ -120,12 +120,7 @@ export function cssVar(name) { } export function setFavicon(url) { - const oldFav = document.querySelector('[rel=icon]'); - oldFav.parentElement.removeChild(oldFav); - const fav = document.createElement('link'); - fav.rel = 'icon'; - fav.href = url; - document.head.appendChild(fav); + document.querySelector('[rel=icon]').href = url; } export function copyToClipboard(text) {