User:Connel MacKenzie/editbarbuttons.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

 MyChar = new Array();
 MyCharText = new Array();
 MyCharEnd = new Array();
 MyCharDefault = new Array();


//The cleaner way to add an editbar button.

  function addEditButton(imageFile, tagOpen, sampleText, tagClose, speedTip)
    {
        mwCustomEditButtons[mwCustomEditButtons.length] = {
         "imageFile": "http://upload.wikimedia.org/wikipedia/" + 
                        imageFile,
                "tagOpen": tagOpen,
                "sampleText": sampleText,
                "tagClose": tagClose,
                "speedTip": speedTip
        };
    }

function insertTagsValue(uchar, ucharend, uchardefault, ucharnr) { 
        var txtarea = document.editform.wpTextbox1; 
        // IE
        if (document.selection && !is_gecko) {
                var theSelection = document.selection.createRange().text; 
        // Mozilla
        } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
                var replaced = false;
                var startPos = txtarea.selectionStart;
                var endPos = txtarea.selectionEnd;
                if (endPos-startPos)
                        replaced = true;
                var scrollTop = txtarea.scrollTop;
                var theSelection = (txtarea.value).substring(startPos, endPos);
        }
        if (theSelection == "") {
                if(!(lastVal = GetCookie("CharDefaultValue-"+ ucharnr))) lastVal ="0";
                theSelection= parseInt(lastVal)+parseInt(uchardefault.substr(1));
                theSelection= theSelection.toString();
        } 
        setCookie("CharDefaultValue-"+ ucharnr,theSelection);
        insertTags(uchar,ucharend,theSelection);
 }

function addEditButtons() {

addEditButton( 'commons/3/30/Btn_toolbar_rayer.png', 
        '<s>', 'strikeout', '</s>', 'Strikeout');

addEditButton( 'commons/5/59/Button_template.png', 
        '<' + '!--', 'sample comment', '-->', 'Comment');

addEditButton( 'en/c/c8/Button_redirect.png', 
        '#REDIRECT [[', '{{PAGENAME}}', 
        ']]', '#redirect');

addEditButton( 'en/5/5f/Button_center.png', 
        '<center>', 'Centered text', '</center>', 'Center');

addEditButton( 'en/e/e9/Button_headline2.png', 
        '\n===', 'Sub-heading', '===', 'Sub-heading');

addEditButton( 'en/8/8e/Button_shifting.png', ':', 
        'Indented text', ':', 'Indent');

addEditButton( 'commons/5/58/Button_small.png', '<small>', 
        'Small text', '</small>', 'Small text');

addEditButton( 'en/8/80/Button_upper_letter.png', '<sup>', 
        'superscript', '</sup>', 'superscript');

addEditButton( 'en/9/93/Button_sub_link.png', 
        '[[{{' + 'PAGENAME}}#', 'subheading', ']]', 'subheading');

addEditButton( 'commons/2/23/Button_code.png', 
        '<tt><nowiki>', 'nowiki\'d text', '</nowiki></tt>', 'tt+nowiki');
}

addEditButtons();


function stringReplaceBox() {
        Valreplace=getCookie ("StrReplace");
        Valreplacement=getCookie ("StrReplacement");
        PromptWin = window.open("about:blank", "PromptWin", "width=280,height=120,dependent=yes,location=no,menubar=no,resizable=yes,status=no,toolbar=no");
        PromptWin.document.open();
        PromptWin.document.write("<title>Text ersetzen</title>");
        PromptWin.document.write("<body style=\"background-color: threedface;\">");
        PromptWin.document.write("<form id=\"replaceform\" name=\"replaceform\" method=POST action=\"javascript:void(0)\">");
        PromptWin.document.write("<center><table border=\"0\">");
        PromptWin.document.write("<tr><td style=\"font: message-box\">Replace:</td> <td><input type=TEXT name=\"valreplace\" value=\"" + Valreplace + "\" size=25 tabindex=\"1\"></td></tr>");
        PromptWin.document.write("<tr><td style=\"font: message-box\">With:</td> <td><input type=TEXT name=\"valreplacement\" value=\"" + Valreplacement + "\" size=25 tabindex=\"2\"></td></tr></table>");
        PromptWin.document.write("<p><input type=SUBMIT value=\" Replace all \" tabindex=\"3\" onclick=\"opener.stringReplace(document.replaceform.valreplace.value,document.replaceform.valreplacement.value); self.close()\"> ");
        PromptWin.document.write("<input type=RESET value=\"Cancel\" tabindex=\"4\" onclick=\"self.close()\"></p>");
        PromptWin.document.write("</center></form></body>");
        PromptWin.document.close();
        PromptWin.document.replaceform.valreplace.select();
 }

 function stringReplace(inReplace,inReplacement) {
        setCookie ("StrReplace",inReplace);
        setCookie ("StrReplacement",inReplacement);
        var StrReplace = new RegExp(inReplace, "g");
        var StrReplacement=inReplacement;
        if (document.editform)
                var txtarea = document.editform.wpTextbox1;
        else {
                // some alternate form? take the first one we can find
                var areas = document.getElementsByTagName('textarea');
                var txtarea = areas[0];
        }
        txtarea.value=txtarea.value.replace(StrReplace,StrReplacement);
 }

function bsImprovedToolbar()
{
 tooly = document.getElementById('toolbar');
 if ( (!tooly) ) return;

 var buttonAB = document.createElement("img");
        buttonAB.width = 23;
        buttonAB.height = 22;
        buttonAB.src = 'http://upload.wikimedia.org/wikipedia/commons/5/59/Button_replace.png';
        buttonAB.border = 0;
        buttonAB.alt = 'Replace text';
        buttonAB.title = 'Replace text'
        buttonAB.style.cursor = "pointer";
        buttonAB.onclick = function() {
                stringReplaceBox();
                return false;
        }
  tooly.appendChild(buttonAB);
}

 addOnloadHook(bsImprovedToolbar);