Jump to content

User:Nardog/CopySectLink.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nardog (talk | contribs) at 19:42, 3 February 2022 (escape percents). 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.
mw.config.get('wgNamespaceNumber') >= 0 &&
(function copySectLink() {
	let css;
	switch (mw.config.get('skin')) {
		case 'minerva':
			css = '.collapsible-heading:not(.open-block) .copysectlink{visibility:hidden} .mw-editsection{white-space:nowrap}';
			break;
		case 'timeless':
			css = '.copysectlink{background-color:#fff;margin-left:-1em} .copysectlink:first-of-type{margin-left:-20px;padding-left:0} .copysectlink::before{content:"";display:inline-block;width:16px;height:16px;background-size:16px 16px;vertical-align:bottom;opacity:0.5}';
	}
	if (css) {
		mw.loader.addStyleTag(`.copysectlink::before{background-image:url("data:image/svg+xml,%3Csvg width='20' height='20' version='1.1' viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%2354595d'%3E%3Cpath d='M16 0H5v2h11v14h2V2a2 2 0 0 0-2-2z'/%3E%3Cpath d='m4 20h9c1.1 0 2-0.9 2-2v-13c0-1.1-0.9-2-2-2h-9c-1.1 0-2 0.9-2 2v13c0 1.1 0.9 2 2 2zm0-15h9v13h-9z'/%3E%3C/g%3E%3C/svg%3E")} ${css}`);
	}
	let handler = function (e) {
		e.preventDefault();
		let text = (mw.config.get('wgPageName') + (this.hash ? decodeURI(this.hash).replace(
			/[\[\]\{\|\}]/g,
			s => '&#' + s.codePointAt(0) + ';'
		) : '')).replace(/_/g, ' ');
		navigator.clipboard.writeText(text).then(() => {
			mw.notify(`Copied "${text}"`);
		}, () => {
			let $input = $('<input>').attr({
				type: 'text',
				value: text,
				readonly: '',
				style: 'position:fixed;top:-100%'
			}).appendTo(document.body);
			$input[0].select();
			document.execCommand('copy');
			$input.remove();
			mw.notify(`Probably copied "${text}"`);
		});
	};
	let addButton = function (isFirst) {
		isFirst = isFirst === true;
		let hn = isFirst ? this : this.parentElement;
		if (hn.querySelector('.copysectlink')) return;
		let url = mw.util.getUrl();
		if (!isFirst) {
			let id = this.id || $(this).data('id');
			if (!id) return;
			url += '#' + encodeURI(id);
		}
		let $button = $('<a>').attr({
			class: 'copysectlink',
			href: url,
			role: 'button'
		}).text('copy').click(handler);
		if (mw.config.get('skin') === 'minerva') {
			$button.addClass('mw-ui-icon mw-ui-icon-element').attr('title', 'Copy');
			let wrapper = hn.querySelector('.mw-editsection');
			if (wrapper) {
				$button.prependTo(wrapper);
			} else {
				$button.appendTo(hn);
			}
		} else {
			let bracket = hn.querySelector('.mw-editsection-bracket:last-child');
			if (bracket) {
				bracket.insertAdjacentText('beforebegin', ' | ');
			} else {
				bracket = $('<span>').addClass('mw-editsection-bracket').text(']')[0];
				$('<span>').addClass('mw-editsection').append(
					$('<span>').addClass('mw-editsection-bracket').text('['),
					bracket
				).appendTo(hn);
			}
			$button.insertBefore(bracket);
		}
	};
	let dependencies = ['mediawiki.util'];
	if (['view', 'purge'].includes(mw.config.get('wgAction'))) {
		['ext.gadget.edittop', 'ext.gadget.edit0'].forEach(m => {
			let state = mw.loader.getState(m);
			if (state && state !== 'registered') dependencies.push(m);
		});
	}
	$.when($.ready, mw.loader.using(dependencies)).then(() => {
		addButton.call(document.getElementById('firstHeading'), true);
		if (mw.config.get('wgAction') !== 'view') return;
		mw.hook('wikipage.content').add($content => {
			$content.find('.mw-headline').each(addButton);
		});
	});
}());