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

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


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


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


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


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


    // בדיקה שהתוכן של העוגנים בלבד השתנה
      $.each(anchors, function (key, data) {
    var replacedContent = content;
         if (data.count > 1) {
    $.each(anchors, function (key, positions) {
          // אם יש יותר מהופעה אחת של אותו ערך
         if (positions.length > 1) {
          var counter = 1;
            var offset = 0;
          var templateToUse = (data.template === 'עוגן1' || data.template === 'anchor1') ? 'עוגן' : data.template;
            var counter = 1; // מספר התוקף הנוכחי
          data.replacements.forEach(function (replacement, index) {
            positions.forEach(function (pos, i) {
            var replacementCount = index + 1;
                var replacement;
            var newKey = key + replacementCount;
                if (i > 0) { // מתחיל למספר מהעוגן השני
            var newReplacement = '{{' + templateToUse + '|' + key + counter + '|' + key + '}}';
                    counter++;
            content = content.replace(replacement, newReplacement);
                    replacement = `{{עוגן|${key}${counter}|${pos.text}}}`;
            counter++;
                } else {
          });
                    replacement = `{{עוגן|${key}|${pos.text}}}`; // העוגן הראשון ללא מספור
                }
                var startIndex = pos.index + offset;
                replacedContent = replacedContent.slice(0, startIndex) + replacement + replacedContent.slice(startIndex + pos.length);
                offset += replacement.length - pos.length;
            });
         }
         }
      });
  // חילוף כל המופעים
content = content.replace(/\{\{עוגן\|[^|]+?1/g, '{{עוגן1');
    // מעדכן את תיבת הטקסט עם השינויים
      textArea.val(content);
      alert('הוחלפו עוגנים כפולים בהצלחה');
     });
     });
    // בדיקה שהתוכן של התיבה שונה והודעה למשתמש
    if (replacedContent !== content) {
        var userContent = content.replace(/{{[^{}]+}}/g, ''); // הוצאת כל העוגנים מתוך התוכן המקורי
        var replacedUserContent = replacedContent.replace(/{{[^{}]+}}/g, ''); // הוצאת כל העוגנים מתוך התוכן שהוחלף
        if (userContent === replacedUserContent) {
            textArea.val(replacedContent);
            alert('הוחלפו עוגנים כפולים בהצלחה');
        } else {
            alert('יש שינויים בתוכן מעבר לעוגנים ולכן לא ניתן להחליף את העוגנים.');
        }
    } else {
        alert('אין עוגנים כפולים');
    }
}
});
      });
    }
   });
   });
});
});

גרסה אחרונה מ־02:07, 1 במאי 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().trim();
        var template = match[1].toLowerCase().trim();
        if (!anchors[key]) {
          anchors[key] = { count: 0, template: '', replacements: [] };
        }
        anchors[key].count++;
        anchors[key].template = template;
        anchors[key].replacements.push(match[0]);
      }

      $.each(anchors, function (key, data) {
        if (data.count > 1) {
          // אם יש יותר מהופעה אחת של אותו ערך
          var counter = 1;
          var templateToUse = (data.template === 'עוגן1' || data.template === 'anchor1') ? 'עוגן' : data.template;
          data.replacements.forEach(function (replacement, index) {
            var replacementCount = index + 1;
            var newKey = key + replacementCount;
            var newReplacement = '{{' + templateToUse + '|' + key + counter + '|' + key + '}}';
            content = content.replace(replacement, newReplacement);
            counter++;
          });
        }
      });
   // חילוף כל המופעים
content = content.replace(/\{\{עוגן\|[^|]+?1/g, '{{עוגן1');

    // מעדכן את תיבת הטקסט עם השינויים
      textArea.val(content);
      alert('הוחלפו עוגנים כפולים בהצלחה');
    });
  });
});