משתמש:מהדורה קמא/בדיקת עוגנים.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');
        // פונקציה להחלפת עוגנים כפולים
     
        function replaceDuplicateAnchors() {
      // יצירת כפתור
            var textArea = $('#wpTextbox1');
      var button = $('<button type="button">בדוק ותקן עוגנים כפולים</button>');
            var content = textArea.val();
      button.css({
            var anchorRegex = /\{\{עוגן(?:1)?\|([^}|]+)(?:\|([^}]+))?\}\}/gi;
        marginTop: '10px',
            var anchors = new Map();
        padding: '5px 10px',
            var match;
        fontSize: '1rem',
        cursor: 'pointer'
      });


      // הוספת הכפתור לדף
            // מציאת כל העוגנים הכפולים ואחסון מידע עליהם
      textArea.after(button);
            while ((match = anchorRegex.exec(content)) !== null) {
 
                var key = match[1].toLowerCase();
      // הגדרת פעולת הכפתור
                var text = match[2] !== undefined ? match[2] : key;
      button.click(function () {
                if (!anchors.has(key)) {
mw.loader.using(['jquery.client'], function () {
                    anchors.set(key, []);
  // בדיקה ש-jQuery זמין ונטען
                }
  $(function () {
                anchors.get(key).push({ index: match.index, length: match[0].length, text: text });
    // בדיקה שהתיבה המקורית אכן מכילה תוכן עם עוגנים
            }
    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) {
            anchors.forEach(function (positions) {
            positions.forEach(function (pos, i) {
                if (positions.length > 1) {
                var replacement;
                    positions.forEach(function (pos, i) {
                if (i > 0) {
                        var replacement;
                    var counter = i + 1;
                        if (i > 0) {
                    replacement = `{{עוגן|${pos.text}${counter}|${pos.text}}}`;
                            var counter = i + 1;
                } else {
                            replacement = `{{עוגן|${pos.text}${counter}|${pos.text}}}`;
                    replacement = `{{עוגן|${pos.text}|${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);
                    });
                 }
                 }
                var startIndex = pos.index + (replacement.length - pos.length) * i;
                content = content.slice(0, startIndex) + replacement + content.slice(startIndex + pos.length);
             });
             });
            // החזרת התוכן המתוקן לתיבת הטקסט
            textArea.val(content);
            // הודעה למשתמש על הסיום
            alert('הוחלפו עוגנים כפולים בהצלחה');
         }
         }
    });


    textArea.val(content);
        // קריאה לפונקציה replaceDuplicateAnchors כאשר הדף מוכן
    alert('הוחלפו עוגנים כפולים בהצלחה');
        replaceDuplicateAnchors();
}
 
// קריאה לפונקציה replaceDuplicateAnchors כאשר הדף מוכן
$(document).ready(function () {
    replaceDuplicateAnchors();
});
});
      });
     }
     }
  });
});
});

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

// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי
$(document).ready(function () {
    // בדיקה שאנו בעמוד עריכה או מצב הגשה
    if (mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit') {
        // פונקציה להחלפת עוגנים כפולים
        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 כאשר הדף מוכן
        replaceDuplicateAnchors();
    }
});