1add26278SSchplurtz le Déboulonné/* 2add26278SSchplurtz le Déboulonné * Copy2clipboard - copy <pre> text to clipboard. 3add26278SSchplurtz le Déboulonné * Copyright (C) 2020, 2021 Schplurtz le Déboulonné <Schplurtz@laposte.net> 4add26278SSchplurtz le Déboulonné * 5add26278SSchplurtz le Déboulonné * This program is free software; you can redistribute it and/or modify 6add26278SSchplurtz le Déboulonné * it under the terms of the CECILL 2.1 free license. See files 7add26278SSchplurtz le Déboulonné * LICENSE and LICENSE-fr for the details in the distribution directory or 8add26278SSchplurtz le Déboulonné * http://cecill.info/licences/Licence_CeCILL_V2.1-en.html 9add26278SSchplurtz le Déboulonné */ 104a476730SSchplurtz le DéboulonnéjQuery(function() { 114a476730SSchplurtz le Déboulonné if(!navigator.clipboard) 124a476730SSchplurtz le Déboulonné return; 134a476730SSchplurtz le Déboulonné var messageBox=function( id, txt ) { 144a476730SSchplurtz le Déboulonné const body=document.getElementsByTagName('body')[0]; 154a476730SSchplurtz le Déboulonné const msg=document.createElement('div'); 164a476730SSchplurtz le Déboulonné msg.setAttribute('id', id ); 174a476730SSchplurtz le Déboulonné msg.classList.add('cp2clipmsg'); 184a476730SSchplurtz le Déboulonné const content = document.createTextNode(txt); 194a476730SSchplurtz le Déboulonné msg.appendChild(content); 204a476730SSchplurtz le Déboulonné body.appendChild(msg); 214a476730SSchplurtz le Déboulonné window.setTimeout(function() { 224a476730SSchplurtz le Déboulonné jQuery("#"+id).fadeTo(500, 0).slideUp(500, function(){ 234a476730SSchplurtz le Déboulonné jQuery(this).remove(); 244a476730SSchplurtz le Déboulonné }); 254a476730SSchplurtz le Déboulonné }, 1500); 2695208031SSchplurtz le Déboulonné }; 27*86783ee6SSchplurtz le Déboulonné var iswin = (navigator.appVersion.indexOf("Win") != -1); 28eeae6715SSchplurtz le Déboulonné var response=async function(event) { 29eeae6715SSchplurtz le Déboulonné try { 30eeae6715SSchplurtz le Déboulonné let text='' 31*86783ee6SSchplurtz le Déboulonné // when line numbers are on, geshi uses <li> tag for each line 32eeae6715SSchplurtz le Déboulonné let lis=event.target.parentElement.getElementsByTagName('li'); 33eeae6715SSchplurtz le Déboulonné if( lis.length ) { 34eeae6715SSchplurtz le Déboulonné for( let li of lis ) { 35eeae6715SSchplurtz le Déboulonné text += li.textContent + '\n'; 36eeae6715SSchplurtz le Déboulonné } 37eeae6715SSchplurtz le Déboulonné } 38*86783ee6SSchplurtz le Déboulonné // no line numbers, whole text is directely in <pre> tag 39eeae6715SSchplurtz le Déboulonné else { 40eeae6715SSchplurtz le Déboulonné text = event.target.parentElement.textContent; 41*86783ee6SSchplurtz le Déboulonné text = text.replace( /\r\n/g, '\n' ); // can happen if page files are prepared on win and dropped in doku tree... 42eeae6715SSchplurtz le Déboulonné } 43*86783ee6SSchplurtz le Déboulonné // Why replace \u00A0 ??? geshi adds an NBSP on each empty line. This is an issue 44eeae6715SSchplurtz le Déboulonné // with python, perl... when you want to run copied code, you get a 45eeae6715SSchplurtz le Déboulonné // syntax error "unexpected \xC2 character" or similar... So remove this 46eeae6715SSchplurtz le Déboulonné // crap. And yes it could remove a legitimate NBSP ; chances are low though. 47eeae6715SSchplurtz le Déboulonné text = text.replace(/^\u00A0$/gm, ""); 48*86783ee6SSchplurtz le Déboulonné // if you paste \n separated lines with right button in powershell, the lines are 49*86783ee6SSchplurtz le Déboulonné // fed in reverse order ! Most stupidly funny bug by MS ever. Anyway, just make sure 50*86783ee6SSchplurtz le Déboulonné // we use \r\n separated lines under windows. It just makes sense. 51*86783ee6SSchplurtz le Déboulonné // see powershell bugs https://github.com/PowerShell/PowerShell/issues/3816 and 52*86783ee6SSchplurtz le Déboulonné // https://github.com/PowerShell/PSReadLine/issues/496 or 53*86783ee6SSchplurtz le Déboulonné // https://github.com/PowerShell/PSReadLine/issues/579 , they're all the same... 54*86783ee6SSchplurtz le Déboulonné if( iswin ) { 55*86783ee6SSchplurtz le Déboulonné text=text.replace(/\n/g, '\r\n' ); 56*86783ee6SSchplurtz le Déboulonné } 57eeae6715SSchplurtz le Déboulonné await navigator.clipboard.writeText(text); 58f20dc62bSSchplurtz le Déboulonné // console.log( "copié >>>" + text + "<<<" ); 59eeae6715SSchplurtz le Déboulonné messageBox('cp2clipok', LANG.plugins.copy2clipboard.copied); 60eeae6715SSchplurtz le Déboulonné } catch (err) { 61f20dc62bSSchplurtz le Déboulonné // console.error('Failed to copy!', err); 62eeae6715SSchplurtz le Déboulonné messageBox('cp2clipnok', LANG.plugins.copy2clipboard.error); 63eeae6715SSchplurtz le Déboulonné } 64eeae6715SSchplurtz le Déboulonné }; 65eeae6715SSchplurtz le Déboulonné 664a476730SSchplurtz le Déboulonné document.querySelectorAll('pre.code,pre.file').forEach(function(elem) { 674a476730SSchplurtz le Déboulonné elem.classList.add('cp2clip'); 684a476730SSchplurtz le Déboulonné let cp = document.createElement('button'); 694a476730SSchplurtz le Déboulonné cp.setAttribute( 'title', LANG.plugins.copy2clipboard.title); 704a476730SSchplurtz le Déboulonné //cp.appendChild(document.createTextNode('Copy to clipboard')) ; 714a476730SSchplurtz le Déboulonné cp.classList.add('cp2clip'); 724a476730SSchplurtz le Déboulonné elem.appendChild(cp); // pre.appendChild 73eeae6715SSchplurtz le Déboulonné cp.addEventListener('click', response) 744a476730SSchplurtz le Déboulonné }) 7595208031SSchplurtz le Déboulonné}); 764a476730SSchplurtz le Déboulonné 77