176,460
עריכות
מהדורה קמא (שיחה | תרומות) (עוד אחד) תגית: שוחזרה |
מהדורה קמא (שיחה | תרומות) (בדיקה) תגית: שוחזרה |
||
שורה 14: | שורה 14: | ||
$('#editform').prepend(button); | $('#editform').prepend(button); | ||
// פעולה שתתבצע עם לחיצה על הכפתור | // פעולה שתתבצע עם לחיצה על הכפתור | ||
button.click(function () { | 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('הוחלפו עוגנים כפולים בהצלחה'); | |||
}); | }); | ||
}); | }); | ||
}); | }); |