Bikarhêner:Balyozxane/FilterTranslations.js

Zanibe: Piştî weşandinê, ji bo dîtina guhartinan dibe ku hewce be "cache"ya geroka xwe paqij bikî.

  • Firefox / Safari: Pê li Shift û Reload bike an jî Ctrl-F5 an Ctrl-R bike (ji bo Mac: ⌘-R)
  • Google Chrome: Pê li Ctrl-Shift-R (ji bo Mac: ⌘-Shift-R) bike
  • Internet Explorer / Edge: Pê li Ctrl û Refresh bike, an jî Ctrl-F5 bike
  • Opera: Pê li Ctrl-F5 bike.
/**
 * (fr)
 * Ce gadget masque les traductions qui ne sont pas dans la liste définie
 * dans le filterTranslations.js de l’utilisateur. Voir [[Aide:Gadget-FilterTranslations]].
 * ----
 * (en)
 * This gadget filters out translations that are not listed in the user’s
 * filterTranslations.js. See [[Aide:Gadget-FilterTranslations]] (fr).
 * ----
 * [[Catégorie:JavaScript du Wiktionnaire|FilterTranslations.js]]
 */
/**
 * Returns the namespace ID of the current page.
 * @return {number} The namespace ID.
 */
wikt.page.getCurrentPageNamespaceId = function () {
  return mw.config.get("wgNamespaceNumber");
};

wikt.page.hasNamespaceIn = function (namespacesNames) {
  var authorizedNamespaces = [];

  for (var i = 0; i < namespacesNames.length; i++) {
    var ns = namespacesNames[i].toLowerCase().replace(/\s/g, "_");
    authorizedNamespaces.push(mw.config.get("wgNamespaceIds")[ns]);
  }

  return authorizedNamespaces.includes(wikt.page.getCurrentPageNamespaceId());
};
wikt.loadScripts = function (scripts) {
  var deferreds = [];

  $.each(scripts, function (i, script) {
    // External script, use $.getScript
    if (script.match(/^(https?:|\/\/)/)) {
      deferreds.push($.getScript(script));
    } // Use mw.using, convert callbacks to Deferreds
    else {
      var d = $.Deferred();
      mw.loader.using(script, d.resolve, d.reject);
      deferreds.push(d);
    }
  });

  return $.when.apply($, deferreds);
}

$(function () {
  if (wikt.page.hasNamespaceIn([""])) {
    window.wikt.gadgets.filterTranslations = {
      NAME: "Filtrer traductions",

      VERSION: "1.0",

      _translations: [],

      _hidden: false,

      _$button: null,

      /**
       * Sets the whitelisted language codes then filters the translations.
       * @param whitelistedLangs {Array<string>} Language codes to whitelist.
       */
      init: function (whitelistedLangs) {
        var self = this;

        this._$button = $('<button style="position:fixed;bottom:0;left:0"></button>');
        this._$button.click(function () {
          if (self._hidden) {
            self.show();
          } else {
            self.hide();
          }
        });
        $(document.body).append(this._$button);

        $(".translations > ul > li").each(function (_, e) {
          var $item = $(e);
          var match = /W-([\w-]+)/.exec($item.find("span").prop("class"));

          if (match && !(whitelistedLangs || []).includes(match[1])) {
            self._translations.push($item);
          }
        });
        this.hide();
      },

      hide: function () {
        this._translations.forEach(function ($item) {
          $item.hide();
        });
        this._$button.text("Afficher toutes les traductions");
        this._hidden = true;
      },

      show: function () {
        this._translations.forEach(function ($item) {
          $item.show();
        });
        this._$button.text("Filtrer les traductions");
        this._hidden = false;
      },
    };

    var username = mw.config.get("wgUserName");
    var url = "https://ku.wiktionary.org/wiki/User:{0}/filterTranslations.js?action=raw&ctype=text/javascript".format(username);
    wikt.loadScripts([url]).done(function () {
      console.log("Chargement de Gadget-FilterTranslations.js…");
      try {
        // noinspection JSUnresolvedVariable
        if (whitelist instanceof Array) {
          // noinspection JSUnresolvedVariable
          wikt.gadgets.filterTranslations.init(whitelist); // Variable should be declared in user page.
        }
      } catch (e) {
        console.log(e);
      }
    });
  }
});