Jump to content

User:Awesome Aasim/copycodeblock.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Awesome Aasim (talk | contribs) at 02:29, 19 August 2024 (add script for copying code blocks I originally worked on Fandom). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
$(document).ready(function() {
	$('.mw-highlight').prepend($('<a href="javascript:void(0)">Copy code</a>').click(function(e) {
		e.preventDefault();
		var value = $(this).parent().find('pre').text();
		var $temp = $("<textarea>");
		$("body").append($temp);
		$temp.val(value).select();
		document.execCommand("copy");
		$temp.remove();
		$(this).text("Copied!");
		var that = this;
		window.setTimeout(function() {
			$(that).text("Copy code");
		}, 3000);
	}))
});