מדיה ויקי:Gadget-EditReplace.js

מתוך ויקיטקסט, מאגר הטקסטים החופשי

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

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
// סקריפט 24: לקוח מ[[ויקיפדיה:סקריפטים/24]]
// Adds a replace option to toolbar in edit mode
// Created by [[user:Yonidebest]]
 
function replaceText()
{
 try {
    if ( document.getElementById('cbReplace').checked )
        var fromThis = new RegExp ( document.getElementById('fromReplace').value, 'g');                             // as regex
       else
        var fromThis = new RegExp ( document.getElementById('fromReplace').value.replace(/\\n/g,"\n")
                                            .replace(/(\^|\[|\.|\$|\{|\*|\(|\\|\+|\)|\||\?|\<|\>)/g,"\\$1"), 'g');  // as string
    var toThis = document.getElementById('toReplace').value.replace(/\\n/g,"\n");
    var statusReplace = document.getElementById('statusReplace');
 
    var outputText = document.editform.wpTextbox1.value.replace ( fromThis, toThis );
 
    if ( outputText != document.editform.wpTextbox1.value )
    {
        document.editform.wpTextbox1.value = outputText;
        statusReplace.innerHTML = 'בוצע!'; // 'done!'
    }
    else statusReplace.innerHTML = 'לא נמצאו מופעים.'; // 'not found.'
  }
  catch(e)
  {
    return;      // lets just ignore what's happened
  }
}
 
 
function toggleReplaceText()
{
 try {
    var divBox = document.getElementById('divReplace');
    var aReplace = document.getElementById('aReplace');
 
    if ( divBox.style.display == 'none' )
    {
        divBox.style.display = 'block';
        aReplace.innerHTML = '[הסתר]'; // hide
    }
    else
    {
        divBox.style.display = 'none';
        aReplace.innerHTML = '[החלף]'; // replace
    }
  }
  catch(e)
  {
    return;      // lets just ignore what's happened
  }
}
 
 
function createReplaceTextBox()
{
 try {
    var toolbar = document.getElementById('toolbar');
    if (!toolbar) {
		var editform = document.getElementById("editform");
		if (editform)
			toolbar = editform.previousSibling;		
	}
	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.tabIndex = 21;
    fromReplace.onchange = function() { javascript:document.getElementById("statusReplace").innerHTML = "מחכה..."; }; // 'waiting...'
 
    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.tabIndex = 22;
    toReplace.onchange = function() { javascript:document.getElementById("statusReplace").innerHTML = "מחכה..."; }; // 'waiting...'
 
    var submitReplace = document.createElement('A');
    submitReplace.href = 'javascript:replaceText();';
    submitReplace.appendChild(document.createTextNode('החלף')); // 'replace'
 
    try { // for IE
        var statusReplace = document.createElement('<SPAN ID="statusReplace"></SPAN>');
    } catch (e) { // for FF
        var statusReplace = document.createElement('SPAN');
        statusReplace.id = 'statusReplace';
    };
    statusReplace.appendChild(document.createTextNode('מחכה...')); // 'waiting...'
 
    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');
    // a link to the article [[Regular expression]]
    aCheckBox.href = '//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';
    // a title for the above link
    aCheckBox.title = 'הסבר אודות ביטויים רגולריים בוויקיפדיה';
    aCheckBox.appendChild(document.createTextNode('ביטוי רגולרי')); // 'regular expression'
 
    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('החלף את ')); // 'replace ' (the search string)
    divBox.appendChild(fromReplace);
    divBox.appendChild(document.createTextNode(' ב ')); // ' with ' (the replacement)
    divBox.appendChild(toReplace);
    divBox.appendChild(document.createTextNode(' '));
    divBox.appendChild(submitReplace);
    divBox.appendChild(document.createTextNode(' | '));
    divBox.appendChild(cbReplace);
    divBox.appendChild(document.createTextNode(' החלף כ')); // 'replace as '
    divBox.appendChild(aCheckBox);
    divBox.appendChild(document.createTextNode(' | סטטוס: ')); // ' | status: '
    divBox.appendChild(statusReplace);
    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();';
	 
	if ( typeof $ != 'undefined' ) {
		toolbar.parentNode.insertBefore(aReplace, toolbar);
		toolbar.parentNode.insertBefore(document.createTextNode(' '), toolbar);
		toolbar.parentNode.insertBefore(divBox, toolbar);
	}
	else {
		toolbar.appendChild(aReplace);
		toolbar.appendChild(document.createTextNode(' '));
		toolbar.appendChild(divBox);
	}
    toggleReplaceText();
  }
  catch(e)
  {
    return;      // lets just ignore what's happened
  }
}
 
$( createReplaceTextBox );
// עד כאן סקריפט 24