MediaWiki:Gadget-site-tpl-copy.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.
/**
 * Support "class=tpl-copy" on syntaxhighlight blocks.
 *
 * See also: [[MediaWiki:Gadget-site-tpl-copy.css]]
 * Source: https://www.mediawiki.org/wiki/MediaWiki:Gadget-site-tpl-copy.js
 */

var hasFeature = navigator.clipboard && 'writeText' in navigator.clipboard;
if (hasFeature) {
	// Add type=button to avoid form submission in preview ([[Special:Permalink/6335352]])
	var $btn = $('<button>').attr('type', 'button').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);
		} catch (e) {
			return;
		}
		var prevLabel = btn.textContent;
		btn.textContent = 'Copied!';
		setTimeout(function() {
			btn.textContent = prevLabel;
		}, 5000);
	});
	
	mw.hook('wikipage.content').add(function ($content) {
		$content.find('.tpl-copy:not(.tpl-copy--bound)')
			.append($btn.clone(true))
			.addClass('tpl-copy--bound');
	});
}