xref: /plugin/copy2clipboard/script.js (revision b16ffdae4bcf411ed482bf3bea52f0348c3d54d0)
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 iswin = (navigator.appVersion.indexOf("Win") != -1);
28  var response=async function(event) {
29    try {
30      let text='';
31      // when line numbers are on, geshi uses <li> tag for each line
32      let lis=event.target.parentElement.getElementsByTagName('li');
33      if( lis.length ) {
34        for( let li of lis ) {
35          text += li.textContent + '\n';
36        }
37      }
38      // no line numbers, whole text is directely in <pre> tag
39      else {
40        text = event.target.parentElement.textContent;
41        text = text.replace( /\r\n/g, '\n' ); // can happen if page files are prepared on win and dropped in doku tree...
42      }
43      // Why replace \u00A0 ??? geshi adds an NBSP on each empty line. This is an issue
44      // with python, perl... when you want to run copied code, you get a
45      // syntax error "unexpected \xC2 character" or similar... So remove this
46      // crap. And yes it could remove a legitimate NBSP ; chances are low though.
47      text = text.replace(/^\u00A0$/gm, "");
48      // if you paste \n separated lines with right button in powershell, the lines are
49      // fed in reverse order ! Most stupidly funny bug by MS ever. Anyway, just make sure
50      // we use \r\n separated lines under windows. It just makes sense.
51      // see powershell bugs https://github.com/PowerShell/PowerShell/issues/3816 and
52      // https://github.com/PowerShell/PSReadLine/issues/496 or
53      // https://github.com/PowerShell/PSReadLine/issues/579 , they're all the same...
54      if( iswin ) {
55        text=text.replace(/\n/g, '\r\n' );
56      }
57      await navigator.clipboard.writeText(text);
58      // console.log( "copié >>>" + text + "<<<" );
59      messageBox('cp2clipok', LANG.plugins.copy2clipboard.copied);
60    } catch (err) {
61      // console.error('Failed to copy!', err);
62      messageBox('cp2clipnok', LANG.plugins.copy2clipboard.error);
63    }
64  };
65
66  document.querySelectorAll('pre.code,pre.file').forEach(function(elem) {
67    elem.classList.add('cp2clip');
68    let cp = document.createElement('button');
69    cp.setAttribute( 'title', LANG.plugins.copy2clipboard.title);
70    //cp.appendChild(document.createTextNode('Copy to clipboard')) ;
71    cp.classList.add('cp2clip');
72    elem.appendChild(cp); // pre.appendChild
73    cp.addEventListener('click', response)
74  })
75});
76
77