From 1d13810f6454300f6ee5eb9ae7e644671fdbe46c Mon Sep 17 00:00:00 2001 From: Ximi1970 Date: Tue, 5 Jul 2022 13:48:58 +0200 Subject: [PATCH] Revert old "fix" --- webext/js/windowEvent.js | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/webext/js/windowEvent.js b/webext/js/windowEvent.js index 9ffc3f2..e02d47a 100644 --- a/webext/js/windowEvent.js +++ b/webext/js/windowEvent.js @@ -104,6 +104,7 @@ var windowListener = new (class extends ExtensionCommon.EventEmitter { windowListener.onCloseButton, true ); + windowListener.hijackTitlebarCloseButton(window); console.log("Close listener added"); } }, @@ -140,4 +141,58 @@ var windowListener = new (class extends ExtensionCommon.EventEmitter { if (event) event.preventDefault(); return true; } + + hijackTitlebarCloseButton(window) { + if ( + windowListener.replaceCommand(window, "titlebar-close", function () { + return windowListener.onCloseButton(null); + }) + ) { + console.log("replaced command= " + "titlebar-close"); + } + } + + replaceCommand(window, eltId, gotHidden) { + let elt = window.document.getElementById(eltId); + if (!elt) { + console.log("Element '" + eltId + "' not found. Command not replaced."); + return false; + } + + let prevent = null; + if (elt.command) { + prevent = { + event: "click", + func: function (e) { + e.preventDefault(); + }, + }; + } else if (elt.getAttribute("oncommand")) { + prevent = { + event: "command", + func: function (e) { + e.stopPropagation(); + }, + }; + } else { + console.warn("Could not replace oncommand on " + eltId); + return false; + } + + let callback = function (event) { + if (event.target.id === eltId) { + console.debug(prevent.event + " on " + eltId); + if (gotHidden()) prevent.func(event); + } + }; + + /* We put listeners on the "titlebar" parent node, because: + - we can hardly short-circuit command/oncommand (probably because they are + registered first) + - we'd have otherwise to alter "oncommand"/"command" attribute and use + Function(), which do not pass review nowadays. */ + elt.parentNode.addEventListener(prevent.event, callback, true); + + return true; + } })();