Files
SysTray-X/webext/js/windowEvent.js

385 lines
13 KiB
JavaScript
Raw Normal View History

2023-11-13 20:14:30 +01:00
/* eslint-disable object-shorthand */
2020-08-02 16:59:53 +02:00
2023-11-13 20:14:30 +01:00
"use strict";
2020-05-10 20:55:27 +02:00
2023-11-13 20:14:30 +01:00
// Using a closure to not leak anything but the API to the outside world.
(function (exports) {
// Get various parts of the WebExtension framework that we need.
var { ExtensionCommon } = ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
// You probably already know what this does.
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
// A helpful class for listening to windows opening and closing.
var { ExtensionSupport } = ChromeUtils.import("resource:///modules/ExtensionSupport.jsm");
/**
* This object is just what we're using to listen for toolbar clicks. The implementation
* isn't what this example is about, but you might be interested as it's a common pattern.
* We count the number of callbacks waiting for events so that we're only listening if we
* need to be.
*
* An EventEmitter has the following basic functions:
*
* EventEmitter.on(emitterName, callback)
* Registers a callback for a custom emitter.
*
* EventEmitter.off(emitterName, callback)
* Unregisters a callback for a custom emitter.
*
* EventEmitter.emit(emitterName)
* Emit a custom emitter, all provided parameters will be forwarded to the registered callbacks.
*/
let windowListener;
class WindowListener extends ExtensionCommon.EventEmitter {
constructor(extension) {
super();
this.extension = extension;
this.onNewWindowCallbackCount = 0;
this.onCloseButtonClickCallbackCount = 0;
this.MESSAGE_CLOSE_TYPE_DEFAULT = 0;
this.MESSAGE_CLOSE_TYPE_MIN_MAIN_TRAY_CLOSE_CHILDREN = 1;
this.MESSAGE_CLOSE_TYPE_MIN_ALL_TRAY = 2;
this.MESSAGE_CLOSE_TYPE_MIN_MAIN_CLOSE_CHILDREN = 3;
this.MESSAGE_CLOSE_TYPE_MIN_ALL = 4;
2020-08-02 16:59:53 +02:00
this.closeType = this.MESSAGE_CLOSE_TYPE_DEFAULT;
2023-11-13 20:14:30 +01:00
this.mainWindowId = 0;
this.activeWindows = {};
}
get listenerIdNewWindow() {
return `window_event_listener_new_window_${this.extension.uuid}_${this.extension.instanceId}`;
}
get listenerIdCloseButton() {
return `window_event_listener_close_button_${this.extension.uuid}_${this.extension.instanceId}`;
}
2023-12-10 16:52:04 +01:00
resetAllListeners() {
}
2023-11-13 20:14:30 +01:00
setCloseType(closeType) {
2023-12-10 16:52:04 +01:00
// console.log("Close type change: " + closeType);
2023-11-13 20:14:30 +01:00
if (closeType === 0) {
2023-12-10 16:52:04 +01:00
if( this.closeType !== this.MESSAGE_CLOSE_TYPE_DEFAULT )
{
this.resetAllListeners();
}
2023-11-13 20:14:30 +01:00
this.closeType = this.MESSAGE_CLOSE_TYPE_DEFAULT;
} else if (closeType === 1) {
2023-12-10 16:52:04 +01:00
if( this.closeType !== this.MESSAGE_CLOSE_TYPE_MIN_MAIN_TRAY_CLOSE_CHILDREN )
{
this.resetAllListeners();
}
2023-11-13 20:14:30 +01:00
this.closeType = this.MESSAGE_CLOSE_TYPE_MIN_MAIN_TRAY_CLOSE_CHILDREN;
} else if (closeType === 2) {
2023-12-10 16:52:04 +01:00
if( this.closeType !== this.MESSAGE_CLOSE_TYPE_MIN_ALL_TRAY )
{
this.resetAllListeners();
}
2023-11-13 20:14:30 +01:00
this.closeType = this.MESSAGE_CLOSE_TYPE_MIN_ALL_TRAY;
} else if (closeType === 3) {
2023-12-10 16:52:04 +01:00
if( this.closeType !== this.MESSAGE_CLOSE_TYPE_MIN_MAIN_CLOSE_CHILDREN )
{
this.resetAllListeners();
}
2023-11-13 20:14:30 +01:00
this.closeType = this.MESSAGE_CLOSE_TYPE_MIN_MAIN_CLOSE_CHILDREN;
} else if (closeType === 4) {
2023-12-10 16:52:04 +01:00
if( this.closeType !== this.MESSAGE_CLOSE_TYPE_MIN_ALL )
{
this.resetAllListeners();
}
2023-11-13 20:14:30 +01:00
this.closeType = this.MESSAGE_CLOSE_TYPE_MIN_ALL;
} else console.log("Unknown close type: " + closeType);
}
setMainWindowId(id) {
this.mainWindowId = id;
}
forceClose(id) {
if (this.activeWindows[id] !== undefined) {
2023-12-10 16:52:04 +01:00
// console.log("Force Close clicked: " + id);
2023-11-13 20:14:30 +01:00
this.activeWindows[id].window.close = this.activeWindows[id].close;
this.activeWindows[id].window.close();
2023-12-10 16:52:04 +01:00
const closeWindow = this.activeWindows[id].window;
delete this.activeWindows[id];
closeWindow.close();
2023-11-13 20:14:30 +01:00
}
}
addOnNewWindow(callback, context) {
// Registering the callback for "new-window".
this.on("new-window", callback);
this.onNewWindowCallbackCount++;
2023-12-10 16:52:04 +01:00
// console.log("New window add");
2023-11-13 20:14:30 +01:00
if (this.onNewWindowCallbackCount === 1) {
ExtensionSupport.registerWindowListener(this.listenerIdNewWindow, {
context,
chromeURLs: [
"chrome://messenger/content/messenger.xhtml",
"chrome://messenger/content/messenger.xul",
],
onLoadWindow: function (window) {
// Get current window id
const id = context.extension.windowManager.getWrapper(window).id;
windowListener.emit("new-window", id);
2023-12-10 16:52:04 +01:00
// console.log("New window added: " + id);
2020-08-02 16:59:53 +02:00
}
2023-11-13 20:14:30 +01:00
});
}
2020-05-10 20:55:27 +02:00
}
2023-11-13 20:14:30 +01:00
removeOnNewWindow(callback, context) {
// Un-Registering the callback for "new-window".
this.off("new-window", callback);
this.onNewWindowCallbackCount--;
2020-05-10 20:55:27 +02:00
2023-12-10 16:52:04 +01:00
// console.log("New window remove");
2023-11-13 20:14:30 +01:00
if (this.onNewWindowCallbackCount === 0) {
for (let window of ExtensionSupport.openWindows) {
if ([
2020-05-10 20:55:27 +02:00
"chrome://messenger/content/messenger.xhtml",
"chrome://messenger/content/messenger.xul",
2023-11-13 20:14:30 +01:00
].includes(window.location.href)) {
// Get current window id
const id = context.extension.windowManager.getWrapper(window).id;
2023-12-10 16:52:04 +01:00
// console.log("New window removed: " + id);
2023-11-13 20:14:30 +01:00
}
2020-05-10 20:55:27 +02:00
}
2023-11-13 20:14:30 +01:00
ExtensionSupport.unregisterWindowListener( this.listenerIdNewWindow );
2020-05-10 20:55:27 +02:00
}
}
2023-11-13 20:14:30 +01:00
addOnCloseButtonClick(callback, context) {
// Registering the callback for "close-clicked".
this.on( "close-clicked", callback );
this.onCloseButtonClickCallbackCount++;
2023-12-10 16:52:04 +01:00
// console.log("New Close add");
2023-11-13 20:14:30 +01:00
if (this.onCloseButtonClickCallbackCount === 1) {
ExtensionSupport.registerWindowListener(this.listenerIdCloseButton, {
context,
chromeURLs: [
"chrome://messenger/content/messenger.xhtml",
"chrome://messenger/content/messenger.xul",
],
onLoadWindow: function ( window ) {
// Get current window id
const id = context.extension.windowManager.getWrapper(window).id
if (id === windowListener.mainWindowId ||
windowListener.closeType === windowListener.MESSAGE_CLOSE_TYPE_MIN_ALL_TRAY ||
windowListener.closeType === windowListener.MESSAGE_CLOSE_TYPE_MIN_ALL) {
function onCloseButton(event) {
// console.log("Close clicked: " + event);
// console.log("Close clicked: " + id);
2023-12-10 16:52:04 +01:00
if ( event ) event.preventDefault();
windowListener.emit("close-clicked", id, false);
2023-11-13 20:14:30 +01:00
return true;
}
// Store the window data
windowListener.activeWindows[id] = {
window,
close : window.close,
listener: onCloseButton
};
// Intercept the close event (triggered when clicking the close button)
window.addEventListener(
"close",
onCloseButton,
true
);
// Pre TB115 close menu, now same as close button
window.close = () => onCloseButton(null);
2023-12-10 16:52:04 +01:00
// console.log("Close listener added for: " + id);
2023-11-13 20:14:30 +01:00
} else {
function onCloseButton2(event) {
2023-12-10 16:52:04 +01:00
// console.log("Real Close clicked: " + id);
2023-11-13 20:14:30 +01:00
windowListener.emit("close-clicked", id, true);
window.close = windowListener.activeWindows[id].close;
2023-12-10 16:52:04 +01:00
2023-11-13 20:14:30 +01:00
window.close();
return false;
}
// Store the window data
windowListener.activeWindows[id] = {
window,
close : window.close,
listener: onCloseButton2
};
// Intercept the close event (triggered when clicking the close button)
window.addEventListener(
"close",
onCloseButton2,
true
);
// Pre TB115 close menu, now same as close button
window.close = () => onCloseButton2(null);
2023-11-14 22:43:51 +01:00
2023-12-10 16:52:04 +01:00
// console.log("Real Close listener added for: " + id);
2023-11-13 20:14:30 +01:00
}
},
});
}
}
removeOnCloseButtonClick(callback, context) {
// Un-Registering the callback for "close-clicked".
this.off( "close-clicked", callback );
this.onCloseButtonClickCallbackCount--;
2023-12-10 16:52:04 +01:00
// console.log("Close remove");
2023-11-13 20:14:30 +01:00
if (this.onCloseButtonClickCallbackCount === 0) {
for (let window of ExtensionSupport.openWindows) {
if ([
"chrome://messenger/content/messenger.xhtml",
"chrome://messenger/content/messenger.xul",
].includes(window.location.href)) {
// Get current window id
const id = context.extension.windowManager.getWrapper(window).id
2023-12-10 16:52:04 +01:00
// Release the close event (triggered when clicking the close button)
window.removeEventListener(
"close",
windowListener.activeWindows[id].listener,
true
);
2023-11-13 20:14:30 +01:00
2023-12-10 16:52:04 +01:00
window.close = windowListener.activeWindows[id].close;
2023-11-13 20:14:30 +01:00
2023-12-10 16:52:04 +01:00
// Clean the storage
delete windowListener.activeWindows[id];
2023-11-13 20:14:30 +01:00
2023-12-10 16:52:04 +01:00
// console.log("Close listener removed for: " + id);
2023-11-13 20:14:30 +01:00
}
}
ExtensionSupport.unregisterWindowListener(this.listenerIdCloseButton);
}
2022-07-05 13:48:58 +02:00
}
2023-11-13 20:14:30 +01:00
};
2022-07-05 13:48:58 +02:00
2023-11-13 20:14:30 +01:00
// This is the important part. It implements the functions and events defined
// in the schema.json. The name must match what you've been using so far,
// "windowEvent" in this case.
class windowEvent extends ExtensionCommon.ExtensionAPI {
// An alternative to defining a constructor here, is to use the onStartup
// event. However, this causes the API to be instantiated directly after the
// add-on has been loaded, not when the API is first used. Depends on what is
// desired.
constructor(extension) {
super(extension);
windowListener = new WindowListener(extension);
2022-07-05 13:48:58 +02:00
}
2023-11-13 20:14:30 +01:00
getAPI(context) {
console.log("windowEvent API started");
return {
// This key must match the class name.
windowEvent: {
setCloseType: async function (type) {
windowListener.setCloseType(type);
},
setMainWindowId: async function (id) {
windowListener.setMainWindowId(id);
},
forceClose: async function (id) {
windowListener.forceClose(id);
},
// An event. Most of this is boilerplate you don't need to worry about, just copy it.
onNewWindow: new ExtensionCommon.EventManager({
context,
name: "windowEvent.onNewWindow",
// In this function we add listeners for any events we want to listen to, and return a
// function that removes those listeners. To have the event fire in your extension,
// call fire.async.
register(fire) {
function callback(event, id) {
return fire.async(id);
}
windowListener.addOnNewWindow(callback,context);
return function () {
windowListener.removeOnNewWindow(callback,context);
};
},
}).api(),
// An event. Most of this is boilerplate you don't need to worry about, just copy it.
onCloseButtonClick: new ExtensionCommon.EventManager({
context,
name: "windowEvent.onCloseButtonClick",
// In this function we add listeners for any events we want to listen to, and return a
// function that removes those listeners. To have the event fire in your extension,
// call fire.async.
register(fire) {
function callback(event, id, quit) {
return fire.async(id, quit);
}
windowListener.addOnCloseButtonClick(callback,context);
return function () {
windowListener.removeOnCloseButtonClick(callback,context);
};
},
}).api(),
2022-07-05 13:48:58 +02:00
},
};
}
2023-11-13 20:14:30 +01:00
onShutdown(isAppShutdown) {
// This function is called if the extension is disabled or removed, or Thunderbird closes.
// We usually do not have to do any cleanup, if Thunderbird is shutting down entirely
if (isAppShutdown) {
return;
2022-07-05 13:48:58 +02:00
}
2023-11-13 20:14:30 +01:00
console.log("windowEvent API closed");
}
};
// Export the api by assigning in to the exports parameter of the anonymous closure
// function, which is the global this.
exports.windowEvent = windowEvent;
})(this)