MediaWiki:Gadget-site-tpl-copy.js

/** * Support "class=tpl-copy" on syntaxhighlight blocks. * * Source https://www.mediawiki.org/wiki/MediaWiki:Gadget-site-tpl-copy.js (2023-11-13) */

var hasFeature = navigator.clipboard && 'writeText' in navigator.clipboard; if (hasFeature) { var $btn = $(' ').text('Copy').on('click', function {		var btn = this;		var wrapper = btn.closest('.tpl-copy');		var preNode = wrapper && wrapper.querySelector('pre');		var content = preNode && preNode.textContent.trim;		try {			navigator.clipboard.writeText(content);			var prevLabel = btn.textContent;			btn.textContent = 'Copied!';			setTimeout(function { btn.textContent = prevLabel; }, 5000);		} catch (e) {}	}); mw.hook('wikipage.content').add(function ($content) {		$content.find('.tpl-copy:not(.tpl-copy--bound)')			.append($btn.clone(true))			.addClass('tpl-copy--bound');	}); }