From 663991f9f6728e7d2a815ca67dd1e2ae48463b2a Mon Sep 17 00:00:00 2001 From: varjolintu Date: Thu, 7 Sep 2023 10:37:10 +0300 Subject: [PATCH] Fix removing notifications --- keepassxc-browser/content/ui.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/keepassxc-browser/content/ui.js b/keepassxc-browser/content/ui.js index 58104bb..e3293d3 100644 --- a/keepassxc-browser/content/ui.js +++ b/keepassxc-browser/content/ui.js @@ -251,6 +251,29 @@ kpxcUI.createNotification = function(type, message) { return; } + // Removes notification from the body element + const removeNotification = function() { + // Catch cross-domain exception + let parentBody; + try { + parentBody = window.parent.document.body; + } catch(e) { + parentBody = window.document.body; + } + + if (notificationWrapper && parentBody.contains(notificationWrapper)) { + parentBody.removeChild(notificationWrapper); + notificationWrapper = undefined; + return; + } + + // Notification is not in the parent + if (notificationWrapper && parentBody !== window.document.body && window.document.body.contains(notificationWrapper)) { + window.document.body.removeChild(notificationWrapper); + notificationWrapper = undefined; + } + }; + logDebug(message); const notification = kpxcUI.createElement('div', 'kpxc-notification kpxc-notification-' + type, {}); @@ -262,10 +285,7 @@ kpxcUI.createNotification = function(type, message) { const msg = kpxcUI.createElement('span', '', {}, message); notification.addEventListener('click', function() { - if (notificationWrapper && window.parent.document.body.contains(notificationWrapper)) { - window.parent.document.body.removeChild(notificationWrapper); - notificationWrapper = undefined; - } + removeNotification(); }); notification.appendMultiple(icon, label, msg); @@ -289,10 +309,7 @@ kpxcUI.createNotification = function(type, message) { // Destroy the banner after five seconds notificationTimeout = setTimeout(() => { - if (notificationWrapper && window.parent.document.body.contains(notificationWrapper)) { - window.parent.document.body.removeChild(notificationWrapper); - notificationWrapper = undefined; - } + removeNotification(); }, 5000); };