1jQuery(function() { 2 // Copyright (C) 2020 Schplurtz le Déboulonné. 3 // Free to use for any purpose except dismantle the sky. 4 if(!navigator.clipboard) 5 return; 6 var messageBox=function( id, txt ) { 7 const body=document.getElementsByTagName('body')[0]; 8 const msg=document.createElement('div'); 9 msg.setAttribute('id', id ); 10 msg.classList.add('cp2clipmsg'); 11 const content = document.createTextNode(txt); 12 msg.appendChild(content); 13 body.appendChild(msg); 14 window.setTimeout(function() { 15 jQuery("#"+id).fadeTo(500, 0).slideUp(500, function(){ 16 jQuery(this).remove(); 17 }); 18 }, 1500); 19 }; 20 var response=async function(event) { 21 try { 22 let text='' 23 let lis=event.target.parentElement.getElementsByTagName('li'); 24 if( lis.length ) { 25 for( let li of lis ) { 26 text += li.textContent + '\n'; 27 } 28 } 29 else { 30 text = event.target.parentElement.textContent; 31 } 32 // Why replace ??? geshi adds an NBSP on each empty line. This is an issue 33 // with python, perl... when you want to run copied code, you get a 34 // syntax error "unexpected \xC2 character" or similar... So remove this 35 // crap. And yes it could remove a legitimate NBSP ; chances are low though. 36 text = text.replace(/^\u00A0$/gm, ""); 37 await navigator.clipboard.writeText(text); 38 // console.log( "copié >>>" + text + "<<<" ); 39 messageBox('cp2clipok', LANG.plugins.copy2clipboard.copied); 40 } catch (err) { 41 // console.error('Failed to copy!', err); 42 messageBox('cp2clipnok', LANG.plugins.copy2clipboard.error); 43 } 44 }; 45 46 document.querySelectorAll('pre.code,pre.file').forEach(function(elem) { 47 elem.classList.add('cp2clip'); 48 let cp = document.createElement('button'); 49 cp.setAttribute( 'title', LANG.plugins.copy2clipboard.title); 50 //cp.appendChild(document.createTextNode('Copy to clipboard')) ; 51 cp.classList.add('cp2clip'); 52 elem.appendChild(cp); // pre.appendChild 53 cp.addEventListener('click', response) 54 }) 55}); 56 57