User:SoledadKabocha/betterCancelLite.js

/* 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: No linkback required; talk-page post preferred instead var zzPageName = mw.config.get( 'wgPageName' ); var zzAction = mw.config.get( 'wgAction' ); var zzArticleId = mw.config.get( 'wgArticleId' );

var BetterCancelFuncs = { reportError: function( errStr ) { mw.notify( errStr ); // XXX: works because I don't use any HTML in the messages, but I may change that later if ( !window.BetterCancelLogErrors ) return; // or LogErrorsFromBetterCancel? if ( window.console !== undefined && typeof(window.console.error) == 'function' ) { window.console.error( errStr ); }       return; },

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

var cancLink = $( '#mw-editform-cancel' ); var origHref = ''; if ( !cancLink || cancLink.length == 0 ) { BetterCancelFuncs.reportError( 'betterCancel: cancel link not found on ' + zzPageName + ' (most likely cause: viewing source, when page is protected or you are blocked)' ); return; } else { origHref = cancLink.attr( 'href' ); }

if ( /^\s*#redirect(:| *)\[\[/i.test( editboxtext ) ) { if ( origHref && origHref.indexOf( 'redirect=no' ) == -1 ) { cancLink.attr( 'href', origHref + ( origHref.indexOf('?') == -1 ? '?' : '&' ) + 'redirect=no' ); }       }        else if ( window.BetterCancelAddSectionAnchor === true ) { var isEditingSection = false; var isEditingSectionException = false; if ( zzAction == 'submit' ) { var edsumbox = $( '#wpSummary' ); if ( !edsumbox || edsumbox.length == 0 ) { BetterCancelFuncs.reportError( 'betterCancel: edit summary box not found on ' + zzPageName ); return; } var edsumboxtext = edsumbox.val; if ( typeof edsumboxtext != 'string' ) { BetterCancelFuncs.reportError( 'betterCancel: edit summary text not found on ' + zzPageName ); return; } isEditingSection          = /\/\* .+ \*\//.test(  edsumboxtext ); isEditingSectionException = /\/\* top \*\//.test( edsumboxtext ); isEditingSectionException |= $( '.selected#ca-addsection' ).length > 0; // looking for section=new in URL wouldn't work // because that parameter doesn't carry over to action=submit (after preview) isEditingSectionException |= !!document.getElementById( 'mw-missingsummary' ); isEditingSectionException |= $( 'td.diff-ntitle' ).length > 0; }           else { isEditingSection         = ( window.location.search.indexOf( 'section=' ) != -1 ); isEditingSectionException = ( /section=(0|new)/.test( window.location.search ) ); }           if ( isEditingSection ) { var headlines = $( '#wikiPreview .mw-headline' ); if ( !headlines || headlines.length == 0 ) { if ( !isEditingSectionException ) BetterCancelFuncs.reportError( 'betterCancel: Supposedly editing a section but could not find headline (did you remove it while editing, or is this an edit conflict?)' ); return; }               var headlineId = headlines.attr( 'id' ); if ( !headlineId ) { BetterCancelFuncs.reportError( 'betterCancel: Section headline has no ID!?' ); return; }               if ( origHref ) cancLink.attr( 'href', origHref + '#' + headlineId ); }       }    } };

if ( zzAction == 'edit' || zzAction == 'submit' ) { if ( zzArticleId != 0 ) { $( document ).ready( BetterCancelFuncs.modifyCancelLink ); } }