Jump to content

User:SoledadKabocha/betterCancel-vector.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SoledadKabocha (talk | contribs) at 01:48, 15 March 2014 (forgot to do type safety check on that config variable). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/* This script currently has no effect on VisualEditor

If you want to use this script, simply add the following line to your common.js or skin.js:
 
importScript('User:SoledadKabocha/betterCancel-vector.js'); // Linkback: [[User:SoledadKabocha/betterCancel-vector.js]]
 
* (Please keep the comment so I can see how many people use this).
*/
var BetterCancelFuncs = {
    reportError:function( errStr ) {
        if ( !window.BetterCancelLogErrors ) return; // or LogErrorsFromBetterCancel?
        if ( window.console === undefined || typeof(window.console.error) != 'function' )
            throw new Error( errStr );
        window.console.error( errStr );
        return;
    },

    modifyCancelLink:function( ) {
        var editbox = $( '#wpTextbox1' );
        if ( !editbox || editbox.length == 0 ) { BetterCancelFuncs.reportError( 'betterCancel: edit box not found on ' + wgPageName ); return; }
        var editboxtext = editbox.val( );
        if ( typeof editboxtext != 'string' ) { BetterCancelFuncs.reportError( 'betterCancel: edit box text not found on ' + wgPageName ); return; }

        var cancLink = $( '#mw-editform-cancel' );
        var origHref = '';
        if ( !cancLink || cancLink.length == 0 ) { BetterCancelFuncs.reportError( 'betterCancel: cancel link not found on ' + wgPageName ); return; }
        else { origHref = cancLink.attr( 'href' ); }

        if ( /^\s*#redirect(:| *)\[\[/i.test( editboxtext ) ) {
            if ( origHref ) cancLink.attr( 'href', origHref + ( origHref.indexOf('?') == -1 ? '?' : '&' ) + 'redirect=no' );
        }
        else if ( window.BetterCancelAddSectionAnchor === true ) {
            var isEditingSection = false;
            if ( wgAction == 'submit' ) {
                var edsumbox = $( '#wpSummary' );
                if ( !edsumbox || edsumbox.length == 0 ) { BetterCancelFuncs.reportError( 'betterCancel: edit summary box not found on ' + wgPageName ); return; }
                var edsumboxtext = edsumbox.val( );
                if ( typeof edsumboxtext != 'string' ) { BetterCancelFuncs.reportError( 'betterCancel: edit summary text not found on ' + wgPageName ); return; }
                isEditingSection = /\/\* .+ \*\//.test( edsumboxtext );
            }
            else {
                isEditingSection = ( window.location.search.indexOf( 'section=' ) != -1 );
            }
            if ( isEditingSection ) {
                var headlines = $( '#wikiPreview .mw-headline' );
                if ( !headlines || headlines.length == 0 ) return;
                var headlineId = headlines.attr( 'id' );
                if ( !headlineId ) return;
                if ( origHref ) cancLink.attr( 'href', origHref + '#' + headlineId );
            }
        }
    }
};

if ( ( wgAction == 'edit' || wgAction == 'submit' ) ) {
    var cancLink = $( '#mw-editform-cancel' );
    if ( cancLink ) {
        cancLink.attr( 'onclick', 'window.onbeforeunload = function (){};' );
    }
    else {
        BetterCancelFuncs.reportError( 'betterCancel: Could not disable unsaved changes warning; cancel link not found while editing ' + wgPageName );
    }

    if ( wgArticleId != 0 ) {
        $( document ).ready( BetterCancelFuncs.modifyCancelLink );
    }
    else if ( window.BetterCancelDisableWarningNewpage ) {
        window.onbeforeunload = function (){};
    }
}