176,202
עריכות
מהדורה קמא (שיחה | תרומות) (עוד ניסוי) |
מהדורה קמא (שיחה | תרומות) (M) |
||
שורה 1: | שורה 1: | ||
// סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי | // סקריפט לאיתור ותיקון עוגנים כפולים במדיה ויקי | ||
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(); // מנרמל את הערכים להשוואה | |||
var | if (!anchors[key]) { | ||
anchors[key] = []; | |||
} | |||
// שמירת מיקום ואורך המופע | |||
anchors[key].push({ index: match.index, length: match[0].length }); | |||
} | |||
$.each(anchors, function (key, positions) { | |||
var key | if (positions.length > 1) { | ||
var | var offset = 0; // משתנה לשינוי נקודת ההתחלה בהתאם להחלפות שבוצעו | ||
positions.forEach(function (pos, i) { | |||
var replacement = `{{עוגן|${key}${i + 1}|${key}}}`; | |||
var startIndex = pos.index + offset; // התחלה מהמיקום המתוקן | |||
content = content.slice(0, startIndex) + replacement + content.slice(startIndex + pos.length); | |||
offset += replacement.length - pos.length; // מתקן את ההיסט להחלפות הבאות | |||
}); | |||
textArea.val(content); // מעדכן את התוכן בתיבת הטקסט | |||
} | } | ||
}); | |||
if ($.isEmptyObject(anchors)) { | |||
alert('אין עוגנים כפולים'); | |||
} else { | |||
alert('הוחלפו עוגנים כפולים בהצלחה'); | alert('הוחלפו עוגנים כפולים בהצלחה'); | ||
} | } | ||
}); | |||
}); | |||
}); | }); |