משתמש:Yonidebest/monobook.js/replaceText.js

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
// Adds a replace option to toolbar in edit mode
// Created by [[user:Yonidebest]]

function replaceText() {
 var toThis = document.getElementById('toReplace').value;
 var statusReplace = document.getElementById('statusReplace');
 if (document.getElementById('cbReplace').checked) { // as regex
   var fromThisRx = new RegExp(document.getElementById('fromReplace').value, 'g');
   var tempText = document.editform.wpTextbox1.value.replace(fromThisRx, toThis);
   if (tempText != document.editform.wpTextbox1.value) {
     document.editform.wpTextbox1.value = tempText;
     statusReplace.innerHTML = 'בוצע!';
   } else statusReplace.innerHTML = 'לא נמצאו מופעים.';
 } else { // as string
   var fromThis = document.getElementById('fromReplace').value;
   var tempText = document.editform.wpTextbox1.value;
   var replacedText = tempText.replace(fromThis, toThis);
   while (tempText != replacedText) {
     tempText = replacedText;
     replacedText = replacedText.replace(fromThis, toThis);
   }
   if (tempText != document.editform.wpTextbox1.value) {
     document.editform.wpTextbox1.value = tempText;
     statusReplace.innerHTML = 'בוצע!';
   } else statusReplace.innerHTML = 'לא נמצאו מופעים.';
 }
}

function toggleReplaceText() {
 var divBox = document.getElementById('divReplace');
 var aReplace = document.getElementById('aReplace');
 if (divBox.style.display == 'none') {
   divBox.style.display = 'block';
   aReplace.innerHTML = '[הסתר]';
 } else {
   divBox.style.display = 'none';
   aReplace.innerHTML = 'בצע החלפה';
 }
}

function addReplaceText() {
 var toolbar = document.getElementById('toolbar');
 if (!toolbar) return;

 try { // for IE
    var fromReplace = document.createElement('<INPUT ID="fromReplace"></INPUT>');
 } catch (e) { // for FF
    var fromReplace = document.createElement('INPUT');
    fromReplace.id = 'fromReplace';
 };
 fromReplace.type = 'text';
 fromReplace.onchange = function() {javascript:document.getElementById("statusReplace").innerHTML = "מחכה...";};

 try { // for IE
    var toReplace = document.createElement('<INPUT ID="toReplace"></INPUT>');
 } catch (e) { // for FF
    var toReplace = document.createElement('INPUT');
    toReplace.id = 'toReplace';
 };
 toReplace.type = 'text';
 toReplace.onchange = function() {javascript:document.getElementById("statusReplace").innerHTML = "מחכה...";};
 var submitReplace = document.createElement('A');
 submitReplace.href = 'javascript:replaceText();';
 submitReplace.appendChild(document.createTextNode('החלף'));

 try { // for IE
    var statusRaplce = document.createElement('<SPAN ID="statusReplace"></SPAN>');
 } catch (e) { // for FF
    var statusRaplce = document.createElement('SPAN');
    statusRaplce.id = 'statusReplace';
 };
 statusRaplce.appendChild(document.createTextNode('מחכה...'));

 try { // for IE
    var cbReplace = document.createElement('<INPUT ID="cbReplace"></INPUT>');
 } catch (e) { // for FF
    var cbReplace = document.createElement('INPUT');
    cbReplace.id = 'cbReplace';
 };
 cbReplace.type = 'checkbox';
 cbReplace.checked = false;
 var aCheckBox = document.createElement('A');
 aCheckBox.href = 'http://he.wikipedia.org/wiki/%D7%91%D7%99%D7%98%D7%95%D7%99_%D7%A8%D7%92%D7%95%D7%9C%D7%A8%D7%99';
 aCheckBox.title = 'הסבר אודות ביטויים רגולריים בוויקיפדיה';
 aCheckBox.appendChild(document.createTextNode('ביטוי רגולרי'));

 try { // for IE
    var divBox = document.createElement('<DIV ID="divReplace"></DIV>');
 } catch (e) { // for FF
    var divBox = document.createElement('DIV');
    divBox.id = 'divReplace';
 };
 divBox.appendChild(document.createTextNode('החלף את '));
 divBox.appendChild(fromReplace);
 divBox.appendChild(document.createTextNode(' ב '));
 divBox.appendChild(toReplace);
 divBox.appendChild(document.createTextNode(' '));
 divBox.appendChild(submitReplace);
 divBox.appendChild(document.createTextNode(' | '));
 divBox.appendChild(cbReplace);
 divBox.appendChild(document.createTextNode(' החלף כ'));
 divBox.appendChild(aCheckBox);
 divBox.appendChild(document.createTextNode(' | סטטוס: '));
 divBox.appendChild(statusRaplce);
 divBox.appendChild(document.createElement('BR'));

 try { // for IE
    var aReplace = document.createElement('<A ID="aReplace"></A>');
 } catch (e) { // for FF
    var aReplace = document.createElement('A');
    aReplace.id = 'aReplace';
 };
 aReplace.href = 'javascript:toggleReplaceText();';
 aReplace.appendChild(document.createTextNode('בצע החלפה'));

 toolbar.appendChild(aReplace);
 toolbar.appendChild(document.createTextNode(' '));
 toolbar.appendChild(divBox);
 toggleReplaceText();
}

$(addReplaceText);