משתמש:מהדורה קמא/בדיקת עוגנים.js: הבדלים בין גרסאות בדף

מתוך אוצר הספרים היהודי השיתופי
קפיצה לניווט קפיצה לחיפוש
(עוד ניסוי)
(M)
שורה 1: שורה 1:
// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי
// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי
$(document).ready(function () {
mw.loader.using(['jquery.client'], function () {
     if (mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit') {
  $(function () {
        var textArea = $('#wpTextbox1');
     // יצירת הכפתור
    var button = $('<button type="button">בדוק ותקן עוגנים כפולים</button>');
    button.css({
      marginTop: '10px',
      padding: '5px 10px',
      fontSize: '1rem',
      cursor: 'pointer'
    });


        // יצירת כפתור
    // הוספת הכפתור לדף
        var button = $('<button type="button">בדוק ותקן עוגנים כפולים</button>');
    $('#editform').prepend(button);
        button.css({
            marginTop: '10px',
            padding: '5px 10px',
            fontSize: '1rem',
            cursor: 'pointer'
        });


        // הוספת הכפתור לדף
    // פעולה שתתבצע עם לחיצה על הכפתור
        textArea.after(button);
    button.click(function () {
      var textArea = $('#wpTextbox1');
      var content = textArea.val();


        // הגדרת פעולת הכפתור
      var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}/gi;
        button.click(function () {
      var anchors = {};
            replaceDuplicateAnchors();
      var match;
        });
    }


    // פונקציה להחלפת עוגנים כפולים
      while ((match = anchorRegex.exec(content)) !== null) {
    function replaceDuplicateAnchors() {
         var key = match[2].toLowerCase(); // מנרמל את הערכים להשוואה
         var content = textArea.val();
         if (!anchors[key]) {
         var anchorRegex = /\{\{עוגן(?:1)?\|([^}|]+)(?:\|([^}]+))?\}\}/gi;
          anchors[key] = [];
         var anchors = new Map();
        }
        var match;
        // שמירת מיקום ואורך המופע
         anchors[key].push({ index: match.index, length: match[0].length });
      }


         while ((match = anchorRegex.exec(content)) !== null) {
      $.each(anchors, function (key, positions) {
             var key = match[1].toLowerCase();
         if (positions.length > 1) {
             var text = match[2] !== undefined ? match[2] : key;
          var offset = 0; // משתנה לשינוי נקודת ההתחלה בהתאם להחלפות שבוצעו
             if (!anchors.has(key)) {
          positions.forEach(function (pos, i) {
                anchors.set(key, []);
             var replacement = `{{עוגן|${key}${i + 1}|${key}}}`;
             }
             var startIndex = pos.index + offset; // התחלה מהמיקום המתוקן
            anchors.get(key).push({ index: match.index, length: match[0].length, text: text });
             content = content.slice(0, startIndex) + replacement + content.slice(startIndex + pos.length);
             offset += replacement.length - pos.length; // מתקן את ההיסט להחלפות הבאות
          });
          textArea.val(content); // מעדכן את התוכן בתיבת הטקסט
         }
         }
      });


        anchors.forEach(function (positions) {
      if ($.isEmptyObject(anchors)) {
            if (positions.length > 1) {
        alert('אין עוגנים כפולים');
                positions.forEach(function (pos, i) {
      } else {
                    var replacement;
                    if (i > 0) {
                        var counter = i + 1;
                        replacement = `{{עוגן|${pos.text}${counter}|${pos.text}}}`;
                    } else {
                        replacement = `{{עוגן|${pos.text}|${pos.text}}}`;
                    }
                    var startIndex = pos.index + (replacement.length - pos.length) * i;
                    content = content.slice(0, startIndex) + replacement + content.slice(startIndex + pos.length);
                });
            }
        });
 
        textArea.val(content);
         alert('הוחלפו עוגנים כפולים בהצלחה');
         alert('הוחלפו עוגנים כפולים בהצלחה');
     }
      }
     });
  });
});
});

גרסה מ־19:03, 30 באפריל 2024

// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי
mw.loader.using(['jquery.client'], function () {
  $(function () {
    // יצירת הכפתור
    var button = $('<button type="button">בדוק ותקן עוגנים כפולים</button>');
    button.css({
      marginTop: '10px',
      padding: '5px 10px',
      fontSize: '1rem',
      cursor: 'pointer'
    });

    // הוספת הכפתור לדף
    $('#editform').prepend(button);

    // פעולה שתתבצע עם לחיצה על הכפתור
    button.click(function () {
      var textArea = $('#wpTextbox1');
      var content = textArea.val();

      var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}/gi;
      var anchors = {};
      var match;

      while ((match = anchorRegex.exec(content)) !== null) {
        var key = match[2].toLowerCase(); // מנרמל את הערכים להשוואה
        if (!anchors[key]) {
          anchors[key] = [];
        }
        // שמירת מיקום ואורך המופע
        anchors[key].push({ index: match.index, length: match[0].length });
      }

      $.each(anchors, function (key, positions) {
        if (positions.length > 1) {
          var offset = 0; // משתנה לשינוי נקודת ההתחלה בהתאם להחלפות שבוצעו
          positions.forEach(function (pos, i) {
            var replacement = `{{עוגן|${key}${i + 1}|${key}}}`;
            var startIndex = pos.index + offset; // התחלה מהמיקום המתוקן
            content = content.slice(0, startIndex) + replacement + content.slice(startIndex + pos.length);
            offset += replacement.length - pos.length; // מתקן את ההיסט להחלפות הבאות
          });
          textArea.val(content); // מעדכן את התוכן בתיבת הטקסט
        }
      });

      if ($.isEmptyObject(anchors)) {
        alert('אין עוגנים כפולים');
      } else {
        alert('הוחלפו עוגנים כפולים בהצלחה');
      }
    });
  });
});