User:Xover/EasyLinks.js

// Make sure the modules we need are loaded (will only load if not already) mw.loader.using(['mediawiki.api', 'mediawiki.util'], => {	// Wait for the page to be parsed	$( => { // TODO: Check for special pages and abort or… // TODO: …only whitelist namespaces known to make sense. // Let's at least disable Special:-pages by default to start. if (mw.config.get('wgCanonicalNamespace') === 'Special') { return; }		// And on the Main Page if (mw.config.get('wgIsMainPage')) { return; }		// Disable completely on mobile (and desktop users with Minerva) if ($('body').hasClass('skin-minerva')) { return; }		// If URL params include &diff= then this is a diff page, so add diffLink portlet if (mw.util.getParamValue('diff')) { let diffPortlet = mw.util.addPortletLink(				'p-cactions', '#', 'Get diff link', 'ca-getdiffwl', 'Get a wikilink to this diff'			); $(diffPortlet).on('click', (e) => {				e.preventDefault;				doGetDiffWL;			}); }		// Add permalink portlet unconditionally; if &oldid= is missing we use the // latest version of the page. let permalinkPortlet = mw.util.addPortletLink(			'p-cactions', '#', 'Get permalink', 'ca-getpermalink', 'Get a permanent wikilink to this page'		); $(permalinkPortlet).on('click', (e) => {			e.preventDefault;			doGetPermaWL;		}); // Add section link and permalink links to all headings (except page title) $('.mw-heading').each((i, h) => {			let headline = $(h).children('h2, h3, h4, h5, h6');			let editsection = $(h).children('.mw-editsection');

let openbracket = $(' [ '); let closebracket = $(' ] '); let divider = $(' | '); let wlspan = $('link').on('click', (e) => {				e.preventDefault;				doGetSectionWL($(headline).attr('id'));			}); let permaspan = $('permalink ').on('click', (e) => {				e.preventDefault;				doGetPermaSectionWL($(headline).attr('id'));			}); let containerspan = $(' '); $(containerspan).append(openbracket, wlspan, divider, permaspan, closebracket); if ($(editsection).length > 0) { $(containerspan).insertAfter(editsection); } else { $(containerspan).insertAfter(headline); }		});	}); // END: $(document).ready }); // END: mw.loader.using

// Common utility function to put the link(s) on the user's clipboard, // and pop up a notification that it happened. function copyAndNotify(description, data) { navigator.clipboard.writeText(data); mw.notify(	     description + ' was automatically copied to your clipboard.',	      {title: 'Link copied', type: 'info', tag: 'EasyLinksCopied'}	    ); } // END: copyAndNotify

// Get the diff id from URL params and create a diff wikilink to that rev. function doGetDiffWL { const diff = mw.config.get('wgDiffNewId'); let diffLink =  + diff + ; copyAndNotify('The wikilink for this diff', diffLink); } // END: doGetDiffWL

// Get the old rev id from URL params, if present, or the latest rev otherwise. function doGetPermaWL { let oldid; let kind = ''; if (mw.util.getParamValue('oldid')) { oldid = mw.util.getParamValue('oldid'); kind = 'this'; } else { oldid = mw.config.get('wgCurRevisionId'); kind = 'the latest'; }	let oldidLink =  + oldid + ; copyAndNotify('A permanent wikilink to ' + kind + ' revision', oldidLink); } // END: doGetPermaWL

const aliases = { 'Wikisource:Bot_requests': 'WS:BR', 'Wikisource:Proposed_deletions': 'WS:PD', 'Wikisource:Copyright_discussions': 'WS:CV', 'Wikisource:Scriptorium': 'WS:S', 'Wikisource:Scriptorium/Help': 'WS:S/H', };

// Creare a wikilink to a section, irrespective of namespace. function doGetSectionWL(headlineid) { let h = $('#' + $.escapeSelector(headlineid)); let title = $(h).text; let page = mw.config.get('wgPageName');

const alias = aliases[page]; if (alias) { title = alias + '#' + title; }

let sectionLink =  + title + ; copyAndNotify(	 'A wikilink to this section',	  sectionLink	); } // END: doGetSectionWL

// Create a permalink to a specific section. function doGetPermaSectionWL(headlineid) { let h = $('#' + $.escapeSelector(headlineid)); let title = $(h).text; let oldid; // If url contains &oldid= then this is a specific revision of the page, // otherwise grab the latest revision and use that. let kind = ''; if (mw.util.getParamValue('oldid')) { oldid = mw.util.getParamValue('oldid'); kind = 'this'; } else { oldid = mw.config.get('wgCurRevisionId'); kind = 'the latest'; }

const alias = aliases[mw.config.get('wgPageName')]; if (alias) { title = alias + '#' + title; }

let permaSectionLink =  + title + ; copyAndNotify(	 'A wikilink to this section at ' + kind + ' revision',	  permaSectionLink	); } // END: doGetPermaSectionWL