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