"use strict";

const myAddonId = 'rspamd-spamness@alexander.moisseev'

var optionObserver = {
    observe: function (aSubject, aTopic, aData) {
        if (aTopic !== "addon-options-displayed" || aData !== myAddonId)
            return;
        aSubject.getElementById("advanced-options-button")
            .addEventListener("command", this.openAdvancedOptions, false);
        aSubject.getElementById("default-action-menupopup")
            .addEventListener("popuphidden", this.setBtnCmdLabels, false);
    },
    openAdvancedOptions: function () {
        const previousSpamnessHeader = Services.prefs.getCharPref("extensions.rspamd-spamness.header").toLowerCase();
        window.openDialog(
            "chrome://rspamd-spamness/content/advancedOptions.xul", "",
            "chrome,modal,dialog,centerscreen",
            previousSpamnessHeader
        );
    },
    setBtnCmdLabels: function () {
        RspamdSpamness.setBtnCmdLabels();
    }
};

RspamdSpamness.onLoad = function() {
    const prefs = Services.prefs;

    RspamdSpamness.previousSpamnessHeader = prefs.getCharPref("extensions.rspamd-spamness.header").toLowerCase();
    RspamdSpamness.syncHeaderPrefs(RspamdSpamness.previousSpamnessHeader);
    RspamdSpamness.setBtnCmdLabels();

    // whether this column gets default status
    var defaultCol = prefs.getBoolPref("extensions.rspamd-spamness.isDefaultColumn");
    if (defaultCol) {
        RspamdSpamness.addSpamnessColumn();
    }
    
    // first time info, should only ever show once
    var greet = prefs.getBoolPref("extensions.rspamd-spamness.installationGreeting");
    if (greet) {
        RspamdSpamness.greet();
        prefs.setBoolPref("extensions.rspamd-spamness.installationGreeting", false);
        prefs.savePrefFile(null);
    }
    
    Services.obs.addObserver(optionObserver, "addon-options-displayed", false);
};

RspamdSpamness.onUnload = function() {
    Services.obs.removeObserver(optionObserver, "addon-options-displayed", false);
    window.removeEventListener('load', RspamdSpamness.onLoad, false);
    window.removeEventListener('unload', RspamdSpamness.onUnload, false);
};

window.addEventListener("load", RspamdSpamness.onLoad, false);
window.addEventListener("unload", RspamdSpamness.onUnload, false);

@@ -946,24 +946,315 @@
 ;%0A    %7D%0A%7D;%0A%0A
+var toolbarObserver = %7B%0A    observe: function (aSubject, aTopic, aData) %7B%0A        document.getElementById(%22header-view-toolbar%22)%0A            .addEventListener(%22drop%22, this.setBtnCmdLabels, false);%0A    %7D,%0A    setBtnCmdLabels: function () %7B%0A        RspamdSpamness.setBtnCmdLabels();%0A    %7D%0A%7D;%0A%0A
 RspamdSpamne
@@ -2143,24 +2143,105 @@
 d%22, false);%0A
+    Services.obs.addObserver(toolbarObserver, %22mail:updateToolbarItems%22, false);%0A
 %7D;%0A%0ARspamdSp

Update training buttons labels on toolbar change