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.
/******************************************************************************
 *                                                                            *
 * [[Help:Substitution|Substituted]] from [[Template:Deltab]], which was      *
 * apparently based on code by [[User:Raylu|Raylu]]. Includes generalizations *
 * from [[User:Pb30/Delsort tab/delsort.js]] and code snippets from           *
 * [[Wikipedia:AutoEd|AutoEd]] (and random stuff from other places,           *
 * identified inline).                                                        *
 *                                                                            *
 * Some discussion can be found at [[User talk:Ioeth/Archive 6#JS request]]). *
 *                                                                            *
 * This script adds a "sort deletion" tab to the top of XfD pages.            *
 *                                                                            *
 *****************************************************************************/

function addDelsortMessage() {
	
	var action = mw.config.get( "wgAction" );

    if( action == 'view' ){
        window.location.href = mw.config.get( "wgServer" ) + mw.config.get( "wgScript" ) + '?title=' + encodeURIComponent( mw.config.get( "wgPageName" ) ) + '&action=edit';
    }

    //prompt for 'type', the name of the delsort list
    if( action == 'edit' || action == 'submit' ){
        var type = prompt('Enter category');
        type = type.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); //trim1 from [http://blog.stevenlevithan.com/archives/faster-trim-javascript]
        if( type === null || type.length === 0 || type === 'undefined' ){ return; }

        //grab page elements and define vars
        var txt = document.forms.editform.wpTextbox1;
        var newTxt = '\n\{\{subst:deletion sorting|' + type + '|— \~\~\~\~\}\}\n';
        var desc = document.forms.editform.wpSummary;
        var newDesc = 'tagged as a ' + type + '-related XfD discussion ([[User:Dinoguy1000/scripts/delsort.js|script-assisted]]).';

        //add notice to text
        if( txt.value.indexOf(newTxt) == -1 && txt.value.indexOf('This debate has been included in the [[Wikipedia:WikiProject Deletion sorting\/' + type) == -1 ) {
            txt.value = txt.value.rtrim();
            txt.value += newTxt;
        } else {
            alert('This debate has already been tagged as a ' + type + '-related XfD discussion.');
            return;
        }

        //add notice to summary
        if( newDesc.match(/[^\s]/) ) {
            if( desc.value.indexOf(newDesc) == -1 ) {
                if( desc.value.match(/[^\*\/\s][^\/\s]?\s*$/) ){ desc.value += " | "; }
                desc.value += newDesc;
            }
        }

        //minor edit checked, but don't autosubmit
        document.forms.editform.wpMinoredit.checked = true;
        //document.forms.editform.submit();
    }
}


jQuery( function( $ ) {
	
	var action = mw.config.get( "wgAction" ),
	    ns     = mw.config.get( "wgNamespaceNumber" ),
	    title  = mw.config.get( "wgTitle" );
/*
    if( ns == 4 && title.match( / for d(elet|iscuss)ion/ ) &&
          ( ( title.match( /(Articles|Miscellany) for deletion/ ) != -1 && action == 'view' ) ||
          action == 'edit' || action == 'submit' ) ) {
        if( typeof( document.forms.editform ) != 'undefined' ) {
            mw.util.addPortletLink( 'p-cactions', 'javascript:addDelsortMessage()', 'Sort deletion', 'ca-SortDeletion', 'Sort Deletion', '', document.getElementById('ca-move'));
        } else if (wgIsArticle && $('#ca-edit') && action == "view") {
            var url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=edit';
            mw.util.addPortletLink( 'p-cactions', url + '&DelSort=true', 'Sort deletion', 'ca-SortDeletion', 'Sort Deletion', '', $('#ca-move'));
        }
    }
*/
	
    if( ns == 4 && title.match( / for d(elet|iscuss)ion/ ) &&
          ( ( title.match( /(Articles|Miscellany) for deletion/ ) != -1 && action == 'view' ) ||
          action == 'edit' || action == 'submit' ) ) {
//        if( !$( '.xfd-closed' ) ||
//              ( ( action == 'edit' || action == 'submit' ) &&
//              document.forms.editform.wpTextbox1.value.indexOf( 'xfd-closed' ) == -1 ) ) {
            mw.util.addPortletLink( 'p-cactions', 'javascript:addDelsortMessage()', 'Sort deletion', 'ca-Sortdeletion', 'Sort Deletion', '' );
//        }
    }
} );