snapdrop

A Progressive Web App for local file sharing https://github.com/RobinLinus/snapdrop snapdrop.net
git clone http://git.hanabi.in/repos/snapdrop.git
Log | Files | Refs | README | LICENSE

clipboard.js (1164B)


      1 // Polyfill for Navigator.clipboard.writeText
      2 if (!navigator.clipboard) {
      3     navigator.clipboard = {
      4         writeText: text => {
      5 
      6             // A <span> contains the text to copy
      7             const span = document.createElement('span');
      8             span.textContent = text;
      9             span.style.whiteSpace = 'pre'; // Preserve consecutive spaces and newlines
     10 
     11             // Paint the span outside the viewport
     12             span.style.position = 'absolute';
     13             span.style.left = '-9999px';
     14             span.style.top = '-9999px';
     15 
     16             const win = window;
     17             const selection = win.getSelection();
     18             win.document.body.appendChild(span);
     19 
     20             const range = win.document.createRange();
     21             selection.removeAllRanges();
     22             range.selectNode(span);
     23             selection.addRange(range);
     24 
     25             let success = false;
     26             try {
     27                 success = win.document.execCommand('copy');
     28             } catch (err) {
     29                 return Promise.error();
     30             }
     31 
     32             selection.removeAllRanges();
     33             span.remove();
     34 
     35             return Promise.resolve();
     36         }
     37     }
     38 }