1/**
2
3 * Javascript src code for DokuWiki plugin 'codebuttonmod2'
4
5 *
6 * GitHub fork https://github.com/david-it/dokuwiki_plugin_codebutton
7 * @author david-it (Davide Rolando)
8 */
9
10jQuery(document).ready( function() {
11
12	urlgets = jQuery(location).attr('search');
13	if(urlgets == "" || urlgets.search("do=") > 0){
14		// Ignore plugin
15		return false;
16	} else {
17		jQuery("pre").wrap( "<div class='pre_wrap'></div>" );
18		jQuery("div.pre_wrap").prepend('<a class="copybtn o-tooltip--left" style="background-color: rgba(0, 0, 0, 0)" data-tooltip="Copy"><img src="lib/plugins/codebuttonmod1/image/copy-button.svg" alt="Copy to clipboard"></a>');
19		jQuery("div.pre_wrap").css({"position":"relative","overflow":"auto"});
20	}
21
22	jQuery( ".copybtn" ).click( function() {
23
24		var ret = jQuery( this ).parent().children('pre').text();
25
26		// Generate a random id
27		let randomID = "id_" + Math.random().toString(36).substring(2, 15);
28
29		// Add aux input to document
30		jQuery( this ).parent().append(jQuery('<textarea></textarea>').attr('id',randomID).html(ret));
31
32		// Copy the content (original code here: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_copy_clipboard2)
33		var copyText = document.getElementById(randomID);
34		copyText.select();
35		copyText.setSelectionRange(0, 99999);
36		document.execCommand("copy");
37
38
39		var copy_a = jQuery( this ).parent().children('a');
40		copy_a.attr('data-tooltip','Copied!').delay( 500 ).queue(function() { copy_a.attr('data-tooltip','Copy') });
41
42		// Remove the aux input from document
43		jQuery( "textarea#" + randomID ).remove();
44
45	});
46
47});
48
49