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

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


שורה 38: שורה 39:
         var text = match[2] !== undefined ? match[2] : key;
         var text = match[2] !== undefined ? match[2] : key;
         if (!anchors.has(key)) {
         if (!anchors.has(key)) {
             anchors.set(key, []); // אם אין מפתח כזה עד כה, הוסף אותו למפתחות המפה
             anchors.set(key, []);
         }
         }
         anchors.get(key).push({ index: match.index, length: match[0].length, text: text }); // הוסף את המידע כערך למפתח המתאים
         anchors.get(key).push({ index: match.index, length: match[0].length, text: text });
     }
     }


שורה 48: שורה 49:
                 var replacement;
                 var replacement;
                 if (i > 0) {
                 if (i > 0) {
                     var counter = i + 1; // מספר התוקף הנוכחי
                     var counter = i + 1;
                     replacement = `{{עוגן|${pos.text}${counter}|${pos.text}}}`;
                     replacement = `{{עוגן|${pos.text}${counter}|${pos.text}}}`;
                 } else {
                 } else {
שורה 62: שורה 63:
     alert('הוחלפו עוגנים כפולים בהצלחה');
     alert('הוחלפו עוגנים כפולים בהצלחה');
}
}
// קריאה לפונקציה replaceDuplicateAnchors כאשר הדף מוכן
$(document).ready(function () {
    replaceDuplicateAnchors();
});
});
});
       });
       });

גרסה מ־18:28, 30 באפריל 2024

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

      // הוספת הכפתור לדף
      textArea.after(button);

      // הגדרת פעולת הכפתור
      button.click(function () {
mw.loader.using(['jquery.client'], function () {
  // בדיקה ש-jQuery זמין ונטען
  $(function () {
    // בדיקה שהתיבה המקורית אכן מכילה תוכן עם עוגנים
    if ($('#wpTextbox1').length > 0) {
      replaceDuplicateAnchors();
    }
  });
// פונקציה להחלפת עוגנים כפולים
function replaceDuplicateAnchors() {
    var textArea = $('#wpTextbox1');
    var content = textArea.val();
    var anchorRegex = /\{\{עוגן(?:1)?\|([^}|]+)(?:\|([^}]+))?\}\}/gi;
    var anchors = new Map();
    var match;

    while ((match = anchorRegex.exec(content)) !== null) {
        var key = match[1].toLowerCase();
        var text = match[2] !== undefined ? match[2] : key;
        if (!anchors.has(key)) {
            anchors.set(key, []);
        }
        anchors.get(key).push({ index: match.index, length: match[0].length, text: text });
    }

    anchors.forEach(function (positions) {
        if (positions.length > 1) {
            positions.forEach(function (pos, i) {
                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('הוחלפו עוגנים כפולים בהצלחה');
}

// קריאה לפונקציה replaceDuplicateAnchors כאשר הדף מוכן
$(document).ready(function () {
    replaceDuplicateAnchors();
});
});
      });
    }
  });
});