MediaWiki:Gadget-site-tpl-copy.js

蓮花祖泉(讨论 | 贡献)2024年3月4日 (一) 21:00的版本 (创建页面,内容为“→‎* * 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 () {…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
  • Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:按 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');
	});
}