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

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


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


      // הגדרת פעולת הכפתור
        // הוספת הכפתור לדף
      button.click(function () {
        textArea.after(button);
mw.loader.using(['jquery.client'], function () {
 
  // בדיקה ש-jQuery זמין ונטען
        // הגדרת פעולת הכפתור
  $(function () {
        button.click(function () {
    // בדיקה שהתיבה המקורית אכן מכילה תוכן עם עוגנים
            replaceDuplicateAnchors();
    if ($('#wpTextbox1').length > 0) {
        });
      replaceDuplicateAnchors();
     }
     }
  });


  function replaceDuplicateAnchors() {
     // פונקציה להחלפת עוגנים כפולים
     // קביעת משתנים והבניית הביטויים הרגולריים
     function replaceDuplicateAnchors() {
     var textArea = $('#wpTextbox1');
        var content = textArea.val();
    var content = textArea.val();
        var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
    var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
        var anchors = new Map();
    var anchors = {};
        var match;
    var match;


    // חיפוש והחלפת העוגנים הכפולים
        while ((match = anchorRegex.exec(content)) !== null) {
    while ((match = anchorRegex.exec(content)) !== null) {
            var key = match[1].toLowerCase();
      var key = match[1].toLowerCase();
            var text = match[2] !== undefined ? match[2] : key;
      var text = match[2] !== undefined ? match[2] : key; // אם אין טקסט חופשי, השתמש בפרמטר העוגן
            if (!anchors.has(key)) {
      if (!anchors[key]) {
                anchors.set(key, []);
        anchors[key] = [];
            }
      }
            anchors.get(key).push({ index: match.index, length: match[0].length, text: text });
      anchors[key].push({ index: match.index, length: match[0].length, text: text });
        }
    }


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