Jump to content

User:Technical 13/Scripts/Gadget-veditLinks.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Technical 13 (talk | contribs) at 20:55, 29 December 2014 (Per mw:Manual:Coding conventions/JavaScript#Closure). 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.
//<nowiki>
( function( mw, $ ) {
	$( function( ) {
		var namespaceNames = [], visualEditorNamespaces = [];
		$.each( mw.config.get( 'wgNamespaceIds' ), function ( i, v ) {
			namespaceNames[namespaceNames.length] = i;
		} );
		$.each( mw.config.get( 'wgNamespaceIds' ), function ( i, v ) {
			if ( $.inArray( v, wgVisualEditorConfig.namespaces ) !== -1 ) {
				visualEditorNamespaces[visualEditorNamespaces.length] = i;
			}
		} );
		if ( mw.user.options.get( 'visualeditor-enable' ) == '1' ) {
			$( 'span.lx' ).each( function ( ) {
				var thisLinkNS = '', thisLink = '', thisLinkTitle = '', thisLinkClass = '', thisLinkVE = '';
				if ( $( this ).find( 'a:first' ).html() !== undefined ) {
					thisLinkNS = $( this ).find( 'a:first' ).attr( 'href' ).match( /\/w(iki\/|\/index.php\?title=)([\w]*)(:.*)?/i );
				} else if ( $( this ).prev( 'a' ).html() !== undefined ) {
					thisLinkNS = $( this ).prev( 'a' ).attr( 'href' ).match( /\/w(iki\/|\/index.php\?title=)([\w]*)(:.*)?/i );
				}
				thisLinkNS = thisLinkNS[2].toLowerCase();
				if ( $.inArray( thisLinkNS, namespaceNames ) === -1 ) {
					thisLinkNS = '';
				}
				thisLink = $( this ).find( 'a:contains("edit")' );
				if ( $.inArray( thisLinkNS, visualEditorNamespaces ) !== -1 ) {
					if ( thisLink.attr( 'title' ) !== undefined && thisLink.attr( 'title' ) !== '' ) {
						thisLinkTitle = thisLink.attr( 'title' );
					}
					if ( thisLink.attr( 'class' ) !== undefined && thisLink.attr( 'class' ) !== '' ) {
						thisLinkClass = thisLink.attr( 'class' );
					}
					if ( thisLink.attr( 'href' ) !== undefined && thisLink.attr( 'href' ) !== '' ) {
						thisLinkVE = thisLink.attr( 'href' ).replace( '&action=edit', '&veaction=edit' );
						thisLink.after( '&nbsp;| <a title="' + thisLinkTitle + '" class="' + thisLinkClass + '" href="' + thisLinkVE + '">edit beta</a>' );
					} else {
						console.warn( 'No href found for:\n\t%s', $( this ).parent().html() );
					}
				}
				thisLink.text( 'edit source' );
			} );
		}
	} );
} ( mediaWiki, jQuery ) );
//</nowiki>