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

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


    // הוספת הכפתור לדף
  var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
    $('#editform').prepend(button);
  var anchors = {};


    // פעולה שתתבצע עם לחיצה על הכפתור
  // אסוף את כל ההופעות של {{עוגן|}} ואת הערך שלהן
     button.click(function () {
  var match;
       var textArea = $('#wpTextbox1');
  while ((match = anchorRegex.exec(content)) !== null) {
      var content = textArea.val();
     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 anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
  $.each(anchors, function (key, data) {
      var anchors = {};
    if (data.count > 1) {
 
      // אם יש יותר מהופעה אחת של אותו ערך
      // אסוף את כל ההופעות של {{עוגן|}} ואת הערך שלהן
      var counter = 1;
      var match;
      var templateToUse = (data.template === 'עוגן1' || data.template === 'anchor1') ? 'עוגן' : data.template;
      while ((match = anchorRegex.exec(content)) !== null) {
      data.replacements.slice(1).forEach(function (replacement, index) { // נתקן רק מההופעה השנייה והלאה
        var key = match[2].toLowerCase().trim();
        var replacementCount = index + 2;
        var template = match[1].toLowerCase().trim();
        var newKey = key + replacementCount;
        if (!anchors[key]) {
        var newReplacement = '{{' + templateToUse + '|' + key + counter + '|' + key + '}}';
          anchors[key] = { count: 0, template: '', replacements: [] };
        content = content.replace(replacement, newReplacement);
        }
        counter++;
        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++;
          });
        }
       });
       });
    }
  });


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

גרסה מ־22:31, 30 באפריל 2024

// פעולה שתתבצע עם לחיצה על הכפתור
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.slice(1).forEach(function (replacement, index) { // נתקן רק מההופעה השנייה והלאה
        var replacementCount = index + 2;
        var newKey = key + replacementCount;
        var newReplacement = '{{' + templateToUse + '|' + key + counter + '|' + key + '}}';
        content = content.replace(replacement, newReplacement);
        counter++;
      });
    }
  });

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