Jump to content

User:Novem Linguae/Scripts/VisualEditorEverywhere.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Novem Linguae (talk | contribs) at 05:39, 27 January 2021 (add more namespaces). 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>

/*
	PURPOSE:
	- Displays the Visual Editor "Edit" tab and "Edit" section link in Talk, Wikipedia, and Template namespaces.
	
	TO INSTALL:
	- Copy paste the below into your User:YourUserName/common.js file
			importScript('User:Novem Linguae/Scripts/VisualEditorEverywhere.js'); // Backlink: [[User:Novem Linguae/Scripts/VisualEditorEverywhere.js]]
			
	TEST PAGES:
	- Regular page - https://en.wikipedia.org/wiki/Lauren_Chamberlain
	- Wikipedia namespace - https://en.wikipedia.org/wiki/Wikipedia:Notability
	- Wikipedia namespace, fully protected - https://en.wikipedia.org/wiki/Wikipedia:Wiki_Ed/tour/example_bio_template
	- Template namespace - https://en.wikipedia.org/wiki/Template:Noticeboard_links
	- Template talk namespace (good for testing section links) - https://en.wikipedia.org/wiki/Template_talk:Noticeboard_links
	
	TODO:
	- restore the section [ edit | ] link as well
*/

if( jQuery !== undefined && mediaWiki !== undefined ) {
    let currNamespace = mw.config.get('wgNamespaceNumber');
	let articleName = mw.config.get('wgPageName');
	let buttonIsPresent = $('#ca-ve-edit').length;
	// let namespaceNeedsButton = ( currNamespace == 4 || currNamespace == 5 || currNamespace == 10 || currNamespace == 11 || currNamespace == 3 );
	console.log('currNamespace: ' + currNamespace);
	let blacklistedNamespaces = [-2, -1, 6]; // media, special, file
	let blacklistedNamespace = blacklistedNamespaces.includes(currNamespace);
	
	if ( ! buttonIsPresent && ! blacklistedNamespace ) {
		// Insert Edit tab at top of page
		let htmlToInsert = '<li id="ca-ve-edit" class="collapsible"><a href="/w/index.php?title='+articleName+'&amp;veaction=edit" title="Edit this page [alt-shift-v]" accesskey="v">Edit</a></li>';
		$('#ca-edit').before(htmlToInsert);
		$('#ca-ve-edit').show();
		
		// Insert [ edit ] by each section
		$('.mw-editsection').each(function(i, obj) {
			htmlToInsert = '<a href="/w/index.php?title='+articleName+'&amp;veaction=edit&amp;section='+(i+1)+'" class="mw-editsection-visualeditor">edit</a>    <span class="mw-editsection-divider"> | </span>';
			$('.mw-editsection').eq(i).children('span:first-of-type').after(htmlToInsert); // Inline tags such as <span> do not work with :nth-child, .before(), etc. Must use :first-of-type.
		});
		$('.mw-editsection-visualeditor, .mw-editsection-divider').show();
	}
}

//</nowiki>