Zum Inhalt springen

Gespräch:Umschrift altgriechische Schriftzeichen/Java Script Seiteninhalt

aus AufEinWort, der freien Forschung
mw.loader.using(['mediawiki.api'], function () {
  $(function () {
    const categories = mw.config.get("wgCategories") || [];
    if (!categories.includes("Weiterleitungseintrag")) return;
    if (!location.href.includes("veaction=edit")) return;

    const zielText = $('#umschriftErgebnis').text().trim();
    if (!zielText) return;

    // Observer zur Erkennung des VisualEditor-Popups
    const observer = new MutationObserver((mutations, obs) => {
      const zielContainer = $('.oo-ui-window-content .oo-ui-fieldLayout-body').first();

      if (zielContainer.length && !$('#btn-ersetzen').length) {
        obs.disconnect(); // nur einmal ausführen

        // Button einfügen
        $('<a href="#" id="btn-ersetzen" style="display:inline-block; margin-top:10px; font-weight:bold;">Weiterleitung ersetzen</a><br>')
          .appendTo(zielContainer)
          .click(function (e) {
            e.preventDefault();

            const api = new mw.Api();
            const title = mw.config.get('wgPageName');

            api.get({
              action: 'query',
              prop: 'revisions',
              rvprop: 'content',
              titles: title,
              formatversion: 2
            }).done(function (data) {
              const page = data.query.pages[0];
              const content = page.revisions[0].content.trim();
              const match = content.match(/^#WEITERLEITUNG\s*\[\[([^\]]+)\]\]/i);

              if (match) {
                const neuerInhalt = content.replace(match[0], `#WEITERLEITUNG [[${zielText}]]`);
                api.postWithToken('csrf', {
                  action: 'edit',
                  title: title,
                  text: neuerInhalt,
                  summary: `Weiterleitung geändert auf [[${zielText}]]`,
                  minor: true
                }).done(function () {
                  location.reload();
                });
              } else {
                const neuerInhalt = `#WEITERLEITUNG [[${zielText}]]\n` + content;
                api.postWithToken('csrf', {
                  action: 'edit',
                  title: title,
                  text: neuerInhalt,
                  summary: `Weiterleitung eingefügt auf [[${zielText}]]`,
                  minor: true
                }).done(function () {
                  location.reload();
                });
              }
            });
          });
      }
    });

    // Starte Observer auf das gesamte Body-Element
    observer.observe(document.body, { childList: true, subtree: true });
  });
});