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 19:52, 29 December 2014 (something broke it again... tracing back...). 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( $ ) {
	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 ) {
				thisLinkTitle = "";
				if ( thisLink.attr( 'title' ) !== undefined && thisLink.attr( 'title' ) !== '' ) {
					thisLinkTitle = thisLink.attr( 'title' );
				}
				thisLinkClass = "";
				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' );
		} );
	}
} );
//</nowiki>