Jump to content

User:SoledadKabocha/cancelRedirectNo.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
/* If you want to use this script, simply add the following line to your common.js or skin.js:
 
importScript('User:SoledadKabocha/cancelRedirectNo.js'); // Linkback: [[User:SoledadKabocha/cancelRedirectNo.js]]
 
* (Please keep the comment so I can see how many people use this).
*/
var CancelRedirectNoFuncs = {
    reportError:function( errStr ) {
        if ( !window.logErrorsFromCancelRedirectNo ) return; // hopefully less confusing than "CancelRedirectNoLogErrors"
        if ( window.console === undefined || typeof(window.console.error) != 'function' )
            throw new Error( errStr );
        window.console.error( errStr );
        return;
    },

    checkIfRedirect:function( r, sts, xhr ) {
        if ( !r.query ) {
            CancelRedirectNoFuncs.reportError( 'cancelRedirectNo: API error while editing ' + wgPageName );
            return;
        }
        if ( !r.query.pages ) {
            CancelRedirectNoFuncs.reportError( 'cancelRedirectNo: API did not find page titled ' + wgPageName );
            return;
        }
        if ( !r.query.pages[wgArticleId+''] ) {
            CancelRedirectNoFuncs.reportError( 'cancelRedirectNo: API did not find page with title ' + wgPageName + ' and id ' + wgArticleId );
            return;
        }
        if ( r.query.pages[wgArticleId+''].redirect !== undefined ) {
            var cancLink = $( '#mw-editform-cancel' );
            var origHref = '';
            if ( cancLink ) { origHref = cancLink.attr( 'href' ); }
            else { CancelRedirectNoFuncs.reportError( 'cancelRedirectNo: Cancel link not found while editing ' + wgPageName ); }
            if ( origHref ) cancLink.attr( 'href', origHref + ( origHref.indexOf('?') == -1 ? '?' : '&' ) + 'redirect=no' );
        }
    }
};

if ( ( wgAction == 'edit' || wgAction == 'submit' ) && wgArticleId != 0 ) {
    $( document ).ready( function( ) {
        var q = {
            format:'json',
            action:'query',
            titles:wgPageName.replace(/_/g, ' '),
            prop:'info'
        };
        $.ajax( {
            url:mw.util.wikiScript('api'),
            dataType:'json',
            type:'POST',
            data:q,
            rawdata:q,
            success:CancelRedirectNoFuncs.checkIfRedirect,
            error:function( xhr, textStatus, errorThrown ) {
                CancelRedirectNoFuncs.reportError( 'cancelRedirectNo: AJAX error: ' + textStatus + ' ' + errorThrown + ', while editing ' + wgPageName );
            }
        } );
    } );
}