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

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


        // יצירת כפתור
    // הוספת הכפתור לדף
        var button = $('<button type="button">בדוק ותקן עוגנים כפולים</button>');
    $('#editform').prepend(button);
        button.css({
            marginTop: '10px',
            padding: '5px 10px',
            fontSize: '1rem',
            cursor: 'pointer'
        });


        // הוספת הכפתור לדף
    // פעולה שתתבצע עם לחיצה על הכפתור
        textArea.after(button);
    button.click(function () {
      var textArea = $('#wpTextbox1');
      var content = textArea.val();


        // הגדרת פעולת הכפתור
      var anchorRegex = /\{\{(עוגן1?|anchor1?)\|([^}]+)\}\}/gi;
        button.click(function () {
      var anchors = {};
            replaceDuplicateAnchors();
        });
    }


    // פונקציה להחלפת עוגנים כפולים
      // אסוף את כל ההופעות של {{עוגן|}} ואת הערך שלהן
    function replaceDuplicateAnchors() {
      var match;
         var content = textArea.val();
      while ((match = anchorRegex.exec(content)) !== null) {
         var anchorRegex = /\{\{עוגן(?:1)?\|([^}|]+)(?:\|([^}]+))?\}\}/gi;
         var key = match[2].toLowerCase().trim();
         var anchors = new Map();
         var template = match[1].toLowerCase().trim();
         var match;
        if (!anchors[key]) {
          anchors[key] = { count: 0, template: '', replacements: [] };
        }
        anchors[key].count++;
         anchors[key].template = template;
         anchors[key].replacements.push(match[0]);
      }


         while ((match = anchorRegex.exec(content)) !== null) {
      $.each(anchors, function (key, data) {
            var key = match[1].toLowerCase();
         if (data.count > 1) {
            var text = match[2] !== undefined ? match[2] : key;
          // אם יש יותר מהופעה אחת של אותו ערך
            if (!anchors.has(key)) {
          var counter = 1;
                anchors.set(key, []);
          var templateToUse = (data.template === 'עוגן1' || data.template === 'anchor1') ? 'עוגן' : data.template;
             }
          data.replacements.forEach(function (replacement, index) {
             anchors.get(key).push({ index: match.index, length: match[0].length, text: text });
            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');


        anchors.forEach(function (positions) {
    // מעדכן את תיבת הטקסט עם השינויים
            if (positions.length > 1) {
      textArea.val(content);
                positions.forEach(function (pos, i) {
      alert('הוחלפו עוגנים כפולים בהצלחה');
                    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('הוחלפו עוגנים כפולים בהצלחה');
     }
});
});

גרסה אחרונה מ־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('הוחלפו עוגנים כפולים בהצלחה');
    });
  });
});