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

מתוך אוצר הספרים היהודי השיתופי
קפיצה לניווט קפיצה לחיפוש
(תיקון)
(אולי כך)
שורה 24: שורה 24:
     // בדיקה שהתיבה המקורית אכן מכילה תוכן עם עוגנים
     // בדיקה שהתיבה המקורית אכן מכילה תוכן עם עוגנים
     if ($('#wpTextbox1').length > 0) {
     if ($('#wpTextbox1').length > 0) {
       replaceDuplicateAnchorsWithinTemplate();
       replaceDuplicateAnchors();
     }
     }
   });
   });


   function replaceDuplicateAnchorsWithinTemplate() {
   function replaceDuplicateAnchors() {
     // קביעת משתנים והבניית הביטויים הרגולריים
     // קביעת משתנים והבניית הביטויים הרגולריים
     var textArea = $('#wpTextbox1');
     var textArea = $('#wpTextbox1');
שורה 36: שורה 36:
     var match;
     var match;


     // חיפוש והחלפת העוגנים הכפולים רק בתוך שטח התבנית
     // חיפוש והחלפת העוגנים הכפולים
    var templateStart = content.indexOf("{{עוגן");
     while ((match = anchorRegex.exec(content)) !== null) {
    var templateEnd = content.indexOf("}}", templateStart);
    var templateContent = content.substring(templateStart, templateEnd + 2);
   
     while ((match = anchorRegex.exec(templateContent)) !== null) {
       var key = match[1].toLowerCase();
       var key = match[1].toLowerCase();
       var text = match[2] !== undefined ? match[2] : key; // אם אין טקסט חופשי, השתמש בפרמטר העוגן
       var text = match[2] !== undefined ? match[2] : key; // אם אין טקסט חופשי, השתמש בפרמטר העוגן
שורה 52: שורה 48:
     $.each(anchors, function (key, positions) {
     $.each(anchors, function (key, positions) {
       if (positions.length > 1) {
       if (positions.length > 1) {
         var offset = 0;
         var counter = 1; // מספר התוקף הנוכחי
         positions.forEach(function (pos, i) {
         positions.forEach(function (pos, i) {
           var replacement;
           var replacement;
           if (i > 0) { // מתחיל למספר מהעוגן השני
           if (i > 0) { // מתחיל למספר מהעוגן השני
             var counter = i + 1;
             counter++;
             replacement = `{{עוגן|${key}${counter}|${pos.text}}}`;
             replacement = `{{עוגן|${key}${counter}|${pos.text}}}`;
           } else {
           } else {
             replacement = `{{עוגן|${key}|${pos.text}}}`; // העוגן הראשון ללא מספור
             replacement = `{{עוגן|${key}|${pos.text}}}`; // העוגן הראשון ללא מספור
           }
           }
           var startIndex = pos.index + offset;
           var startIndex = pos.index + (replacement.length - pos.length) * i;
           templateContent = templateContent.slice(0, startIndex) + replacement + templateContent.slice(startIndex + pos.length);
           content = content.slice(0, startIndex) + replacement + content.slice(startIndex + pos.length);
          offset += replacement.length - pos.length;
         });
         });
        textArea.val(content);
       }
       }
     });
     });


     // החלפת התבנית המתוקנת בתוך הטקסט המקורי
     // הודעה למשתמש
    content = content.substring(0, templateStart) + templateContent + content.substring(templateEnd + 2);
     alert('הוחלפו עוגנים כפולים בהצלחה');
 
    // בדיקה שהתוכן של התיבה שונה והודעה למשתמש
     if (textArea.val() !== content) {
      textArea.val(content);
      alert('הוחלפו עוגנים כפולים בתוך התבנית בהצלחה');
    } else {
      alert('אין עוגנים כפולים בתוך התבנית');
    }
   }
   }
});
});

גרסה מ־18:07, 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 = {};
    var match;

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

    $.each(anchors, function (key, positions) {
      if (positions.length > 1) {
        var counter = 1; // מספר התוקף הנוכחי
        positions.forEach(function (pos, i) {
          var replacement;
          if (i > 0) { // מתחיל למספר מהעוגן השני
            counter++;
            replacement = `{{עוגן|${key}${counter}|${pos.text}}}`;
          } else {
            replacement = `{{עוגן|${key}|${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('הוחלפו עוגנים כפולים בהצלחה');
  }
});
      });
    }
  });
});