summaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/settings-controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bg/js/settings/settings-controller.js')
-rw-r--r--ext/bg/js/settings/settings-controller.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/ext/bg/js/settings/settings-controller.js b/ext/bg/js/settings/settings-controller.js
index 87dea408..6479714d 100644
--- a/ext/bg/js/settings/settings-controller.js
+++ b/ext/bg/js/settings/settings-controller.js
@@ -26,6 +26,8 @@ class SettingsController extends EventDispatcher {
super();
this._profileIndex = profileIndex;
this._source = yomichan.generateId(16);
+ this._pageExitPreventions = new Set();
+ this._pageExitPreventionEventListeners = new EventListenerCollection();
}
get source() {
@@ -109,6 +111,16 @@ class SettingsController extends EventDispatcher {
return {index: this._profileIndex};
}
+ preventPageExit() {
+ const obj = {end: null};
+ obj.end = this._endPreventPageExit.bind(this, obj);
+ if (this._pageExitPreventionEventListeners.size === 0) {
+ this._pageExitPreventionEventListeners.addEventListener(window, 'beforeunload', this._onBeforeUnload.bind(this), false);
+ }
+ this._pageExitPreventions.add(obj);
+ return obj;
+ }
+
// Private
_setProfileIndex(value) {
@@ -147,4 +159,21 @@ class SettingsController extends EventDispatcher {
targets = this._setupTargets(targets, extraFields);
return await api.modifySettings(targets, this._source);
}
+
+ _onBeforeUnload(e) {
+ if (this._pageExitPreventions.size === 0) {
+ return;
+ }
+
+ e.preventDefault();
+ e.returnValue = '';
+ return '';
+ }
+
+ _endPreventPageExit(obj) {
+ this._pageExitPreventions.delete(obj);
+ if (this._pageExitPreventions.size === 0) {
+ this._pageExitPreventionEventListeners.removeAllEventListeners();
+ }
+ }
}