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 18:47, 29 December 2014 (I think it should still change the "edit" to "edit source" even if not adding a "edit beta" link...). 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 = [];
	$.each(mw.config.get('wgNamespaceIds'), function( i, v ){ namespaceNames[namespaceNames.length] = i; });
	var visualEditorNamespaces = [];
	$.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"){
		var thisLinkNS = '';
		$('span.lx').each(function(){
			var thisLink = '', thisLinkTitle = '', thisLinkClass = '', thisLinkVE = '';
			if(typeof($(this).find('a:first').html()) !== 'undefined'){
				thisLinkNS = $(this).find('a:first').attr('href').match(/\/w(iki\/|\/index.php\?title=)([\w]*)(:.*)?/i);
			} else if(typeof($(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 = '';
			}
			if($.inArray(thisLinkNS, visualEditorNamespaces) !== -1){
				thisLink = $(this).find('a:contains("edit")');
				thisLinkTitle = "";
				if(typeof(thisLink.attr('title')) != 'undefined' && thisLink.attr('title') != ''){
					thisLinkTitle = thisLink.attr('title');
				}
				thisLinkClass = "";
				if(typeof(thisLink.attr('class')) != 'undefined' && thisLink.attr('class') != ''){
					thisLinkClass = thisLink.attr('class');
				}
				if(typeof(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>