Benutzer:Everywhere West/monobook.js

aus Wikipedia, der freien Enzyklopädie
< Benutzer:Everywhere West
Dies ist die aktuelle Version dieser Seite, zuletzt bearbeitet am 30. April 2010 um 14:00 Uhr durch imported>Everywhere West(377813).
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
document.write('<script type="text/javascript" src="' 
             + 'http://de.wikipedia.org/w/index.php?title=Benutzer:D/monobook/user.js' 
             + '&action=raw&ctype=text/javascript&dontcountme=s"><\/script>');
document.write('<link rel="stylesheet" type="text/css" href="'
             + 'http://de.wikipedia.org/w/index.php?title=Benutzer:D/monobook/user.css'
             + '&action=raw&ctype=text/css&dontcountme=s"><\/link>');

/** adds buttons for some predefined search'n'replace-operations to the editor */
window.addEventListener("DOMContentLoaded", function() {
    var searchAndReplace    = [
        { 
            label:  "cleanEntities",
            title:  "converts html, hex and decimal entities into characters",
            func:   function(s) {
                        return s.replace(   // hex entities
                            /&#x([0-9a-f]{1,4});/gi,    
                            function(dollar0, dollar1) { 
                                return String.fromCharCode(parseInt(dollar1, 16)); 
                            }
                        ).replace(          // decimal entities
                            /&#([0-9]{1,5});/g,     
                            function(dollar0, dollar1) {
                                return String.fromCharCode(parseInt(dollar1, 10)); 
                            }
                        )/*.replace(        // named entities
                            /&([a-z]+);/gi,         
                            function(dollar0, dollar1) {
                                return dollar1 in html4Entities
                                        ? String.fromCharCode(html4Entities[dollar1])
                                        : dollar0;
                            }
                        )*/;
                    }
        },
        {
            label:  "cleanTags",
            title:  "cleans up some HTML-tags and replaces others with wiki-markup",
            func:   function(s) {
                        return s.replace(/[ \t]*(<br *>|<\/br *>)/gi,       "<br />")
                                .replace(/[ \t]*(<hr *>|<\/hr *>)/gi,       "<hr />")
                                .replace(/<font size="-1">(.*)<\/font>/gi,  "<small>$1</small>")
                                .replace(/<font size="+1">(.*)<\/font>/gi,  "<big>$1</big>")
                                .replace(/<i>(.*)<\/i>/gi,                  "''$1''")
                                .replace(/<b>(.*)<\/b>/gi,                  "'''$1'''");
                    }
        },
        {
            label:  "noSelfLinks",
            title:  "removes links from a page to itself",
            func:   function(s) {
                        var head    = wgPageName.substring(0, 1);
                        var tail    = wgPageName.substring(1);
                        var title   = "[" + escapeRE(head.toLowerCase() + head.toUpperCase()) + "]"
                                    + escapeRE(tail).replace(/[_ ]/g, "[_ ]");
                        return s.replace(   // [[ title ]] => title
 
                            new RegExp("\\[\\[\\s*(" + title + ")\\s*\\]\\]", "g"),
                            "$1"
                        ).replace(          // [[ title | caption ]] => caption
                            new RegExp("\\[\\[\\s*(" + title + ")\\s*\\|\\s*([^\\]]+?)\\s*\\]\\]", "g"),
                            "$2"
                        );
                    }
        },
        {
            label:  "shortLinks",
            title:  "shortens wikilinks from [[link|linktext]] to [[link]]text",
            func:   function(s) {
                        return s.replace(
                            /\[\[\s*([^\]|]+)\s*\|\s*\1([^\]|]*)\s*\]\]/,
                            "[[$1]]$2"
                        );
                    }
        },
        {
            label:  "noLinks",
            title:  "removes all links",
            func:   function(s) {
                        return s.replace(
                            /\[\[\s*([^\]|]*)\s*\]\]/g,
                            "$1"
                        ).replace(
                            /\[\[\s*([^\]|]*)\s*\|\s*([^\]|]*)\s*\]\]/g,
                            "$2"
                        );
                    }
        },
        {
            label:  "noStyle",
            title:  "removes italics and bold style",
            func:   function(s) {
                        return s.replace(/('''?|''''')(.*?)(\1)/g, "$2");
                    }
        },
        {
            label:  "quoteRight",
            title:  "typographical quote characters for dewp",
            func:   function(s) {
                        return s.replace(   // save HTML attributes
                            /="([^"]*)"/g,
                            '=§§§$1§§§'
                        ).replace(          // replace starting quotes
                            /(^[*:;#]+|\s|[\(\[\|'>])"('*\[*\w)/mg, 
                            "$1\u201e$2"
                        ).replace(          // replace ending quotes
                            /(\w\]*'*.?)"($|\s|[\)\]\|',;.:<-])/mg, 
                            "$1\u201c$2"
                        ).replace(          // rebuild HTML attributes
                            /=§§§([^§]*)§§§/g, 
                            '="$1"'
                        );
                    }
        },
        {
            label:  "unDate",
            title:  "removes links to date pages on dewp",
            func:   function(s) {
                        return s.replace(
                            /\[\[\s*(\d+(er)?( v. Chr.)?)\s*\]\]/g,
                            "$1"
                        ).replace(
                            /\[\[\s*(\d+\. (Jahrhundert|Januar|Februar|März|April|Mai|Juni|Juli|August|September|Oktober|November|Dezember))\s*\]\]/g, 
                            "$1"
                        );
                    }
        },
    ];
 
    //------------------------------------------------------------------------------
 
    /** escapes characters to make them usable as a literal in a RegExp */
    function escapeRE(s) { 
        return s.replace(/([{}()|.?*+^$\[\]\\])/g, "\\$1");
    };
 
    /** replaces text in the textarea and resets the cursor */
    function replace(func) {
        var text    = tb.value;
        var start   = tb.selectionStart;
        var end     = tb.selectionEnd;
        if (start == 0 && end == text.length) {
            tb.value    = func(text);
            tb.select();
        }
        else if (start == end) {
            tb.value    = func(text);
            tb.setSelectionRange(start, end);
        }
        else {
            var prefix  = text.substring(0, start);
            var select  = text.substring(start, end);
            var suffix  = text.substring(end);
            var repl    = func(select);
            tb.value    = prefix + repl + suffix;
            tb.setSelectionRange(start, start+repl.length);
        }
        tb.focus();
    }
 
    /** adds a new replace-button */
    function button(def) {
        var btn = document.createElement("input");
        btn.type    = "button";
        btn.value   = def.label;
        btn.onclick = function() { replace(def.func); }
        if (div.firstChild) div.appendChild(document.createTextNode("\u00a0"));
        div.appendChild(btn);
    }
 
    /** adds a new replace-link */
    function link(def) {
        var btn = document.createElement("a");
        btn.textContent = def.label;
        btn.title       = def.title;
        btn.onclick     = function() { replace(def.func); }
        btn.style.cursor    = "pointer";
        if (div.firstChild) div.appendChild(document.createTextNode("\u00a0|\u00a0"));
        div.appendChild(btn);
    }
 
    var form    = document.forms['editform'];
    if (!form)  return;
    var tb      = form.elements['wpTextbox1'];
    if (!tb)    return;
    tb.value    = tb.value;
 
    var div = document.createElement("div");
    div.id  = "searchAndReplaceButtons";
    div.style.fontSize  = "85%";
 
    searchAndReplace.forEach(link);
 
    var toolbar = document.getElementById('toolbar');
    if (toolbar) {
        div.style.cssFloat  = "right";
        toolbar.parentNode.insertBefore(div, toolbar);
    }
    else {
        form.parentNode.insertBefore(div, form);
    }
}, false);