Zum Inhalt springen

Benutzer:Forrester/düp-monobook.js

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 28. Oktober 2007 um 02:01 Uhr durch D (Diskussion | Beiträge) (aufgeräumt). Sie kann sich erheblich von der aktuellen Version unterscheiden.

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
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
// <nowiki>

// DÜP-monobook.js, Version 2.0

//==============================================================================
//## functionality

// Dateiüberprüfung-Portletlink
addOnloadHook(function() {
    addPortletLink(
        "p-personal", 
        Wiki.pageURL("User:" + wgUserName + "/Dateiüberprüfung"), 
        "Dateiüberprüfung",
        "pt-Dateiüberprüfung",
        "Dateiüberprüfung",
        "b",
        document.getElementById("pt-userpage")
    );
});

// Add DÜP-Buttons
addOnloadHook(function() {
    //------------------------------------------------------------------------------
    //## button actions
    
    /** Cat-Aufschub */
    function Aufschub() {
        var reason = promptReason("Bitte gib unbedingt eine Begründung für den Aufschub an. Das Leerzeichen (damit dein Kommentar eine Box bekommt) wird automatisch vor deine Eingabe hier gesetzt. Auch die Unterschrift wird automatisch eingefügt.");
        if (reason==null) return;
     
        Wiki.replaceText(
                wgPageName,
                /\{\{Dateiüberprüfung\/benachrichtigt \(Kategorie\)\s*\|[^\}]*\}\}/,
                "\{\{Dateiüberprüfung/benachrichtigt (Kategorie)\|\{\{subst:LOCALYEAR\}\}\|\{\{subst:LOCALMONTH\}\}\|\{\{subst:LOCALDAY2\}\}\}\}\n " + reason + " ~~~~\n", 
                "Aufschub",
                reload);
    }
     
    /** - Cat + LA */
    function LA() {
        var reason = promptReason("Bitte gib unbedingt eine Begründung für den Löschantrag an. Diese Begründung wird auch für die Bilderlöschkandidaten genutzt, die Unterschrift wird automatisch erstellt.");
        if (reason == null) return;
     
        function phase1() {
            Wiki.newSection(
                    "Wikipedia:Löschkandidaten/Bilder/" + wpDatum(), 
                    reason + " ~~~~\n",
                    '[[:' + wgPageName + ']]',
                    phase2);
        }
        function phase2() {
            Wiki.replaceText(
                    wgPageName,
                    /\{\{Dateiüberprüfung\/benachrichtigt \(Kategorie\)\s*\|[^\}]*\}\}/,
                    "{{subst:Löschantrag}}<br />\n " + reason + " ~~~~\n",
                    "BildLA+",
                    reload);
        }
        phase1();
    }
     
    /** +SLA */
    function SLA() {
        Wiki.appendText(
                wgPageName,
                "\n{{SLA}} Die Beschreibungsseite für diese Datei enthält nicht die ausreichenden Informationen, um sie in der Wikipedia behalten zu können. Der Uploader wurde informiert, aber es hat sich nach 2 Wochen immer noch nichts verändert. ~~~~", 
                "SLA+",
                reload);
    }
    
    //------------------------------------------------------------------------------
    //## helper functions
    
    /** returns the current date usable in a page title */
    function wpDatum() {
        var months  = [ 
                "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", 
                "August", "September", "Oktober", "November", "Dezember" ];
        var now     = new Date();
        var year    = now.getYear(); 
        if (year < 999) year += 1900;
        return now.getDate() + '._' + months[now.getMonth()] + '_' + year;
    }
    
    /** decode url-parameters into a map */
    var urlParams = (function() {
        if (location.search == "")  return {};
        var out = {};
        location.search.substring(1).split("&")
        .forEach(function(param) {
            var parts   = param.split("=");
            if (parts.length != 2)  return;
            var key     = decodeURIComponent(parts[0]);
            var code    = parts[1].replace(/\+/g, "%20");
            out[key]    = decodeURIComponent(code)
        });
        return out;
    })();
	
	/** create a link executing a function when clicked */
	function jsLink(label, clicked) {
		var a   = document.createElement("a");
        a.textContent   = label;
        a.onclick       = clicked;
        a.textContent   = label;
        a.style.cursor  = "default";
		return a;
	}
    
    /** adds a tab executing a function when clicked */
    function addJsTab(label, clicked) {
		var	a	= jsLink(label, clicked);
        var li  = document.createElement("li");
        li.appendChild(a);
		var watch = document.getElementById('ca-watch') ||
               		document.getElementById('ca-unwatch');
		if (!watch) return;
        watch.parentNode.insertBefore(li, watch);
    }
    
    /** asks for a reason, and asks again if empty */
    function promptReason(text) {
        for (;;) {
            var reason = prompt(text);
            if (reason != "")   return reason;
            if (reason == null) return null;
        }
    }
    
    /** reload the current page */
    function reload() {
        window.location.href    = Wiki.pageURL(wgPageName);
    }

    //------------------------------------------------------------------------------
    //## init
	
	var	imagePage	= wgCanonicalNamespace == "Image" &&
						urlParams["action"] != "edit" &&
						urlParams["action"] != "submit";
						
	if (imagePage) {
		addJsTab("Aufschub",    Aufschub);
		addJsTab("LA",          LA);
		addJsTab("SLA",         SLA);
	}
});
 
//==============================================================================
//## helper objects

/** edits wiki pages */
Wiki = {
    /** returns the url of a page in read-mode */
    pageURL: function(title) {
        return wgArticlePath.replace(/\$1/, this.encodeTitle(title));
    },
    
    /** returns the url of an arbitrary page */
    scriptURL: function(params) {
        return wgScript + "?" + Ajax.urlEncode(params);
    },
    
    /** encode a page title so it can be used in an URL */
    encodeTitle: function(title) {
        return encodeURIComponent(title.replace(/ /g, "_"))
                .replace(/%%/g,     "\u0000")
                .replace(/%3a/gi,   ":"     )
                .replace(/%2f/gi,   "/"     )
                .replace(/%23/gi,   "#"     )                           
                .replace(/\u0000/,  "%%"    );
    },
    
    //------------------------------------------------------------------------------
    

    /** add text to a page at the end and calls the finishedFunc if sucessful */
    appendText: function(title, text, subject, finishedFunc) {
        function change(form) {
            form.wpSummary  = subject;
            form.wpTextbox1 += text;
            form.wpSave     = true;
            return true;
        }
        this.editPage(title, null, change, finishedFunc);
    },
    
    /** add text to a page at the start and calls the finishedFunc if sucessful */
    prependText: function(title, text, subject, finishedFunc) {
        function change(form) {
            form.wpSummary  = subject;
            form.wpTextbox1 = text + form.wpTextbox1;
            form.wpSave     = true;
            return true;
        }
        this.editPage(title, null, change, finishedFunc);
    },
    
    /** replace a regexp on a page and calls the finishedFunc if sucessful */
    replaceText: function(title, search, replace, subject, finishedFunc) {
        function change(form) {
            form.wpSummary  = subject;
            form.wpTextbox1 = form.wpTextbox1.replace(search, replace);
            form.wpSave     = true;
            return true;
        }
        this.editPage(title, null, change, finishedFunc);
    },
    
    /** append a new section to a page and calls the finishedFunc if sucessful */
    newSection: function(title, text, subject, finishedFunc) {
        function change(form) {
            form.wpSummary  = subject;
            form.wpTextbox1 = text;
            form.wpSave     = true;
            return true;
        }
        this.editPage(title, "new", change, finishedFunc);
    },
    
    /** arbitrary page editing, calls the finishedFunc if sucessful */
    editPage: function(title, section, formChangeFunc, finishedFunc) {
        var args    = { 
            title: title, 
            action: "edit", 
            section: section 
        };
        Ajax.get(wgScript, args, function(client) {
            if (client.status != 200)   throw "expected status 200, got: " + client.status;
            
            // fetch editform
            var xml         = Ajax.parseXML(client.responseText);
            var editform    = xml.getElementById("editform");
            if (!editform)  throw "editform not found";
            
            // extract editform
            var action      = editform.action;
            var formData    = {
                wpTextbox1:     editform.elements["wpTextbox1"].value,
                wpSummary:      editform.elements["wpSummary"].value,
                wpMinoredit:    editform.elements["wpMinoredit"].checked,
                wpWatchthis:    editform.elements["wpWatchthis"].checked,
                wpSection:      editform.elements["wpSection"].value,
                wpEdittime:     editform.elements["wpEdittime"].value,
                wpEditToken:    editform.elements["wpEditToken"].value,
                wpStarttime:    editform.elements["wpStarttime"].value,
                wpScrolltop:    editform.elements["wpScrolltop"].value,
                wpAutoSummary:  editform.elements["wpAutoSummary"].value,
                wpSave:         false,
                wpPreview:      false,
                wpDiff:         false,
            };
            
            // change editform
            var save    = formChangeFunc(formData);
            if (!save)  return;
                
            // store editform
            Ajax.post(action, formData, function(client) {
                if (client.status != 200)   throw "expected status 200, got: " + client.status;
                if (finishedFunc)   finishedFunc(client.responseText);
            });
        });
    }
};

/** simple ajax helper */
Ajax = {
    /** GET from an URL, calls the okFunc if successful */
    get: function(url, params, okFunc) {
        var getURL  = url 
                    + (url.indexOf("?") == -1 ? "?" : "&")
                    + this.urlEncode(params, false);
        var client  = new XMLHttpRequest();
        client.open("GET", getURL, true);
        client.onreadystatechange = function() {
            if (client.readyState != 4) return;
            okFunc(client);
        }
        client.send(null);
    },
    
    /** POST to an URL, calls the okFunc if successful */
    post: function(url, params, okFunc) {
        var postBody    = this.urlEncode(params, true);
        var client      = new XMLHttpRequest();
        client.open("POST", url, true);
        client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        client.onreadystatechange = function() {
            if (client.readyState != 4) return;
            okFunc(client);
        }
        client.send(postBody);
    },
    
    /** encodes a Map or Pair-Array into a query string, optionally for use with application/x-www-form-urlencoded  */
    urlEncode: function(params, form) {
             if (!params)                       params  = [];
        else if (params.constructor != Array)   params  = this.mapToPairs(params);
        var encodeComponent = form ? this.encodeFormValue : encodeURIComponent;
        function encodePair(pair) { return pair.map(encodeComponent).join("="); }
        return params.map(encodePair).join("&");
    },
    
    /** encodes a single value for application/x-www-form-urlencoded */
    encodeFormValue: function(value) {
        return encodeURIComponent(value
                .replace(/\r\n|\n|\r/g, "\r\n"))
                .replace(/(^|[^%])(%%)*%20/g, "$1$2+");
    },
    
    /** convert a map into an Array for 2-element Arrays */
    mapToPairs: function(params) {
        var out = [];
        for (key in params) {
            var val = params[key];
            if (val === null)   continue;
            if (val === false)  continue;
            if (val === true)   val = "1";
            val = val.toString();
            out.push([ key, val ]);
        }
        return out;
    },
    
    /** parse XML and XHTML content */
    parseXML: function(text) {
        var xml = new DOMParser().parseFromString(text, "text/xml");
        var doc = xml.documentElement;
        if (doc.tagName == "parserError")   throw "XML parser error: " + doc.textContent;
        return xml;
    },
};

// </nowiki>