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

מתוך אוצר הספרים היהודי השיתופי
קפיצה לניווט קפיצה לחיפוש
(ניסוי)
(בדיקה)
תגית: שוחזרה
(17 גרסאות ביניים של אותו משתמש אינן מוצגות)
שורה 19: שורה 19:
       var content = textArea.val();
       var content = textArea.val();


       var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}/gi;
       var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
       var anchors = {};
       var anchors = {};
      // אסוף את כל הופעות של {{עוגן|}} ואת הערך שלהן
       var match;
       var match;
       while ((match = anchorRegex.exec(content)) !== null) {
       while ((match = anchorRegex.exec(content)) !== null) {
         var key = match[2].toLowerCase(); // מנרמל את הערכים להשוואה
         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].count++;
         anchors[key].push({ index: match.index, length: match[0].length });
        anchors[key].template = template;
         anchors[key].replacements.push(match[0]);
       }
       }


       $.each(anchors, function (key, positions) {
      var isFirstReplacement = true; // מסמן אם זו ההחלפה הראשונה
         if (positions.length > 1) {
 
           var counter = 1;
       $.each(anchors, function (key, data) {
           positions.forEach(function (pos, i) {
         if (data.count > 1) {
             var replacement = `{{עוגן|${key}${counter}|${key}}}`;
          // אם יש יותר מהופעה אחת של אותו ערך
            content = content.slice(0, pos.index) + replacement + content.slice(pos.index + pos.length);
           var counter = 2; // מתחילים מהמספר השני
             counter++;
          var templateToUse = (data.template === 'עוגן1' || data.template === 'anchor1') ? 'עוגן' : data.template;
           data.replacements.slice(1).forEach(function (replacement) { // נתקן מההופעה השנייה והלאה
             if (!isFirstReplacement) { // רק אם אינו ההופעה הראשונה
              var newKey = key + counter;
              var newReplacement = '{{' + templateToUse + '|' + key + counter + '|' + key + '}}';
              content = content.replace(replacement, newReplacement);
              counter++;
             } else {
              isFirstReplacement = false;
            }
           });
           });
         }
         }
       });
       });


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

גרסה מ־23:41, 30 באפריל 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]);
      }

      var isFirstReplacement = true; // מסמן אם זו ההחלפה הראשונה

      $.each(anchors, function (key, data) {
        if (data.count > 1) {
          // אם יש יותר מהופעה אחת של אותו ערך
          var counter = 2; // מתחילים מהמספר השני
          var templateToUse = (data.template === 'עוגן1' || data.template === 'anchor1') ? 'עוגן' : data.template;
          data.replacements.slice(1).forEach(function (replacement) { // נתקן מההופעה השנייה והלאה
            if (!isFirstReplacement) { // רק אם אינו ההופעה הראשונה
              var newKey = key + counter;
              var newReplacement = '{{' + templateToUse + '|' + key + counter + '|' + key + '}}';
              content = content.replace(replacement, newReplacement);
              counter++;
            } else {
              isFirstReplacement = false;
            }
          });
        }
      });

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