Refactor and fix code formatting for word-processor-plugin-installer (#4579)

This commit is contained in:
Adomas Venčkauskas 2024-08-14 09:47:11 +03:00 committed by Dan Stillman
parent cff7e45d90
commit 2a0d245018

View file

@ -34,27 +34,17 @@ var { Zotero } = ChromeUtils.importESModule("chrome://zotero/content/zotero.mjs"
Components.utils.import("resource://gre/modules/Services.jsm"); Components.utils.import("resource://gre/modules/Services.jsm");
var installationInProgress = false; var installationInProgress = false;
var _runningTimers = [];
function setTimeout(func, ms) {
var timer = Components.classes["@mozilla.org/timer;1"].
createInstance(Components.interfaces.nsITimer);
var timerCallback = {notify: function() {
_runningTimers.splice(_runningTimers.indexOf(timer), 1);
func();
}};
timer.initWithCallback(timerCallback, ms, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
// add timer to global scope so that it doesn't get garbage collected before it completes
_runningTimers.push(timer);
}
var ZoteroPluginInstaller = function (addon, failSilently, force) { var ZoteroPluginInstaller = function (addon, failSilently, force) {
this._addon = addon; this._addon = addon;
this.failSilently = failSilently; this.failSilently = failSilently;
this.force = force; this.force = force;
var prefService = Components.classes["@mozilla.org/preferences-service;1"]. this.prefBranch = Services.prefs.getBranch(this._addon.EXTENSION_PREF_BRANCH);
getService(Components.interfaces.nsIPrefService); // Prefs:
this.prefBranch = prefService.getBranch(this._addon.EXTENSION_PREF_BRANCH); // version - last successfully installed version
// skipInstallation - if user cancels an attempt to automatically install the plugin, we do not
// attempt to automatically install it again until a successful installation
this.prefPaneDoc = null; this.prefPaneDoc = null;
@ -83,15 +73,18 @@ ZoteroPluginInstaller.prototype = {
.getService(Components.interfaces.nsIWindowWatcher) .getService(Components.interfaces.nsIWindowWatcher)
.openWindow(null, "chrome://zotero/content/progressWindow.xhtml", '', .openWindow(null, "chrome://zotero/content/progressWindow.xhtml", '',
"chrome,resizable=no,close=no,centerscreen", null); "chrome,resizable=no,close=no,centerscreen", null);
this._progressWindow.addEventListener("load", () => { this._firstRunListener() }, false); this._progressWindow.addEventListener("load", () => this._firstRunListener(), false);
} else { }
else {
let result = this._addon.install(this); let result = this._addon.install(this);
if (result.then) await result; if (result.then) await result;
} }
} }
} catch(e) { }
catch (e) {
Zotero.logError(e); Zotero.logError(e);
} finally { }
finally {
installationInProgress = false; installationInProgress = false;
} }
@ -100,14 +93,14 @@ ZoteroPluginInstaller.prototype = {
_errorDisplayed: false, _errorDisplayed: false,
isInstalled: function () { isInstalled: function () {
return this.prefBranch.getBoolPref("installed"); return !!this.prefBranch.getCharPref("version");
}, },
setProgressWindowLabel: function (value) { setProgressWindowLabel: function (value) {
if (this._progressWindow) this._progressWindowLabel.value = value; if (this._progressWindow) this._progressWindowLabel.value = value;
}, },
closeProgressWindow: function(value) { closeProgressWindow: function () {
if (this._progressWindow) this._progressWindow.close(); if (this._progressWindow) this._progressWindow.close();
}, },
@ -115,7 +108,7 @@ ZoteroPluginInstaller.prototype = {
installationInProgress = false; installationInProgress = false;
this.closeProgressWindow(); this.closeProgressWindow();
this.prefBranch.setCharPref("version", this._version); this.prefBranch.setCharPref("version", this._version);
this.updateInstallStatus(true); this.updateInstallStatus();
this.prefBranch.setBoolPref("skipInstallation", false); this.prefBranch.setBoolPref("skipInstallation", false);
if (this.force && !this._addon.DISABLE_PROGRESS_WINDOW) { if (this.force && !this._addon.DISABLE_PROGRESS_WINDOW) {
var addon = this._addon; var addon = this._addon;
@ -125,7 +118,7 @@ ZoteroPluginInstaller.prototype = {
addon.EXTENSION_STRING, addon.EXTENSION_STRING,
Zotero.getString("zotero.preferences.wordProcessors.installationSuccess") Zotero.getString("zotero.preferences.wordProcessors.installationSuccess")
); );
}, 0); });
} }
}, },
@ -134,7 +127,7 @@ ZoteroPluginInstaller.prototype = {
this.closeProgressWindow(); this.closeProgressWindow();
if (!notFailure) { if (!notFailure) {
this.prefBranch.setCharPref("version", this._version); this.prefBranch.setCharPref("version", this._version);
this.updateInstallStatus(false); this.updateInstallStatus();
} }
if (this.failSilently) return; if (this.failSilently) return;
if (this._errorDisplayed) return; if (this._errorDisplayed) return;
@ -153,7 +146,7 @@ ZoteroPluginInstaller.prototype = {
if (result == 1) { if (result == 1) {
Zotero.launchURL("https://www.zotero.org/support/word_processor_plugin_manual_installation"); Zotero.launchURL("https://www.zotero.org/support/word_processor_plugin_manual_installation");
} }
}, 0); });
}, },
cancelled: function (dontSkipInstallation) { cancelled: function (dontSkipInstallation) {
@ -175,26 +168,26 @@ ZoteroPluginInstaller.prototype = {
groupbox.appendChild(label); groupbox.appendChild(label);
var description = document.createXULElement("description"); var description = document.createXULElement("description");
description.appendChild(document.createTextNode( description.appendChild(document.createTextNode(isInstalled
isInstalled ? ? Zotero.getString('zotero.preferences.wordProcessors.installed', this._addon.APP)
Zotero.getString('zotero.preferences.wordProcessors.installed', this._addon.APP) : : Zotero.getString('zotero.preferences.wordProcessors.notInstalled', this._addon.APP)));
Zotero.getString('zotero.preferences.wordProcessors.notInstalled', this._addon.APP)));
groupbox.appendChild(description); groupbox.appendChild(description);
var hbox = document.createXULElement("hbox"); var hbox = document.createXULElement("hbox");
hbox.setAttribute("pack", "center"); hbox.setAttribute("pack", "center");
var button = document.createXULElement("button"), var button = document.createXULElement("button"),
addon = this._addon; addon = this._addon;
button.setAttribute("label", button.setAttribute("label", isInstalled
(isInstalled ? ? Zotero.getString('zotero.preferences.wordProcessors.reinstall', this._addon.APP)
Zotero.getString('zotero.preferences.wordProcessors.reinstall', this._addon.APP) : : Zotero.getString('zotero.preferences.wordProcessors.install', this._addon.APP));
Zotero.getString('zotero.preferences.wordProcessors.install', this._addon.APP)));
button.addEventListener("command", function () { button.addEventListener("command", function () {
Zotero.debug(`Install button pressed for ${addon.APP} plugin`); Zotero.debug(`Install button pressed for ${addon.APP} plugin`);
try { try {
var zpi = new ZoteroPluginInstaller(addon, false, true); var zpi = new ZoteroPluginInstaller(addon, false, true);
zpi.showPreferences(document); zpi.showPreferences(document);
} catch (e) { }
catch (e) {
Zotero.logError(e); Zotero.logError(e);
} }
}, false); }, false);
@ -205,41 +198,39 @@ ZoteroPluginInstaller.prototype = {
old = document.getElementById(this._addon.EXTENSION_DIR); old = document.getElementById(this._addon.EXTENSION_DIR);
if (old) { if (old) {
container.replaceChild(groupbox, old); container.replaceChild(groupbox, old);
} else { }
else {
container.appendChild(groupbox); container.appendChild(groupbox);
} }
}, },
updateInstallStatus: function(status) { updateInstallStatus: function () {
this.prefBranch.setBoolPref("installed", status);
if (!this.prefPaneDoc) return; if (!this.prefPaneDoc) return;
var isInstalled = this.isInstalled(); var isInstalled = this.isInstalled();
var description = this.prefPaneDoc.querySelector(`#${this._addon.EXTENSION_DIR} description`); var description = this.prefPaneDoc.querySelector(`#${this._addon.EXTENSION_DIR} description`);
description.replaceChild(this.prefPaneDoc.createTextNode( description.replaceChild(this.prefPaneDoc.createTextNode(isInstalled
isInstalled ? ? Zotero.getString('zotero.preferences.wordProcessors.installed', this._addon.APP)
Zotero.getString('zotero.preferences.wordProcessors.installed', this._addon.APP) : : Zotero.getString('zotero.preferences.wordProcessors.notInstalled', this._addon.APP)
Zotero.getString('zotero.preferences.wordProcessors.notInstalled', this._addon.APP)
), description.childNodes[0]); ), description.childNodes[0]);
var button = this.prefPaneDoc.querySelector(`#${this._addon.EXTENSION_DIR} button`); var button = this.prefPaneDoc.querySelector(`#${this._addon.EXTENSION_DIR} button`);
button.setAttribute("label", button.setAttribute("label", isInstalled
(isInstalled ? ? Zotero.getString('zotero.preferences.wordProcessors.reinstall', this._addon.APP)
Zotero.getString('zotero.preferences.wordProcessors.reinstall', this._addon.APP) : : Zotero.getString('zotero.preferences.wordProcessors.install', this._addon.APP));
Zotero.getString('zotero.preferences.wordProcessors.install', this._addon.APP)));
}, },
_firstRunListener: function () { _firstRunListener: function () {
this._progressWindowLabel = this._progressWindow.document.getElementById("progress-label"); this._progressWindowLabel = this._progressWindow.document.getElementById("progress-label");
this._progressWindowLabel.value = Zotero.getString('zotero.preferences.wordProcessors.installing', this._addon.EXTENSION_STRING); this._progressWindowLabel.value = Zotero.getString('zotero.preferences.wordProcessors.installing', this._addon.EXTENSION_STRING);
this._progressWindow.sizeToContent(); this._progressWindow.sizeToContent();
var me = this; setTimeout(() => {
setTimeout(function() { this._progressWindow.focus();
me._progressWindow.focus(); setTimeout(async () => {
setTimeout(async function() { this._progressWindow.focus();
me._progressWindow.focus();
try { try {
await me._addon.install(me); await this._addon.install(this);
} catch(e) { }
me.error(); catch (e) {
this.error();
throw e; throw e;
} }
}, 500); }, 500);