User:Xiplus/js/section-link.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * Some codes are copied from https://gerrit.wikimedia.org/g/mediawiki/core/+/a43542143016fadf670625b82832ee917aaded83/resources/src/mediawiki.widgets/mw.widgets.CopyTextLayout.js#73
 */

(function() {

	mw.util.addCSS(`
	.sectionlink {
		background-color: transparent;
		border: 1px solid #808080;
		float: right;
		font-size: 70%;
		width: 64px;
	}
	`);

	$('.mw-headline').each(function(i, sec) {
		var pagename = mw.config.get('wgPageName')
			.replaceAll('_', ' ');

		var secname = $(sec).attr('id')
			.replaceAll('_', ' ')
			.replaceAll('{', '{')
			.replaceAll('}', '}');
		secname = decodeURIComponent(secname);

		var value = pagename + '#' + secname;

		$('<input>')
			.addClass('sectionlink')
			.attr({
				readonly: ''
			})
			.val(value)
			.on('click', function() {
				this.select();
				var copied;
				try {
					copied = document.execCommand('copy');
				} catch (e) {
					copied = false;
				}
				if (copied) {
					mw.notify('Copied to clipboard.');
				} else {
					mw.notify('Failed to copy to clipboard.', { type: 'error' });
				}
			})
			.insertBefore(sec);

	});

})();