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

מתוך אוצר הספרים היהודי השיתופי
קפיצה לניווט קפיצה לחיפוש
(ניסוי חדש)
(עוד ניסוי)
שורה 1: שורה 1:
// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי
// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי
$(document).ready(function () {
$(document).ready(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 content = textArea.val();
            var anchorRegex = /\{\{עוגן(?:1)?\|([^}|]+)(?:\|([^}]+))?\}\}/gi;
            var anchors = new Map();
            var match;


            // מציאת כל העוגנים הכפולים ואחסון מידע עליהם
        // יצירת כפתור
            while ((match = anchorRegex.exec(content)) !== null) {
        var button = $('<button type="button">בדוק ותקן עוגנים כפולים</button>');
                var key = match[1].toLowerCase();
        button.css({
                var text = match[2] !== undefined ? match[2] : key;
            marginTop: '10px',
                if (!anchors.has(key)) {
            padding: '5px 10px',
                    anchors.set(key, []);
            fontSize: '1rem',
                }
            cursor: 'pointer'
                anchors.get(key).push({ index: match.index, length: match[0].length, text: text });
        });
            }
 
        // הוספת הכפתור לדף
        textArea.after(button);
 
        // הגדרת פעולת הכפתור
        button.click(function () {
            replaceDuplicateAnchors();
        });
    }


            // החלפת העוגנים הכפולים
    // פונקציה להחלפת עוגנים כפולים
            anchors.forEach(function (positions) {
    function replaceDuplicateAnchors() {
                if (positions.length > 1) {
        var content = textArea.val();
                    positions.forEach(function (pos, i) {
        var anchorRegex = /\{\{עוגן(?:1)?\|([^}|]+)(?:\|([^}]+))?\}\}/gi;
                        var replacement;
        var anchors = new Map();
                        if (i > 0) {
        var match;
                            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);
                    });
                }
            });


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


         // קריאה לפונקציה replaceDuplicateAnchors כאשר הדף מוכן
         anchors.forEach(function (positions) {
         replaceDuplicateAnchors();
            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('הוחלפו עוגנים כפולים בהצלחה');
     }
     }
});
});

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

// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי
$(document).ready(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 () {
            replaceDuplicateAnchors();
        });
    }

    // פונקציה להחלפת עוגנים כפולים
    function replaceDuplicateAnchors() {
        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('הוחלפו עוגנים כפולים בהצלחה');
    }
});