lstu.js.ep 1.43 KB
Newer Older
Luc Didry's avatar
Luc Didry committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
var j = document.getElementById('js');
j.classList.remove('hidden');
// Code from http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript
function copyTextToClipboard(text) {
    var textArea              = document.createElement("textarea");
    textArea.style.position   = 'fixed';
    textArea.style.top        = 0;
    textArea.style.left       = 0;
    textArea.style.width      = '2em';
    textArea.style.height     = '2em';
    textArea.style.padding    = 0;
    textArea.style.border     = 'none';
    textArea.style.outline    = 'none';
    textArea.style.boxShadow  = 'none';
    textArea.style.background = 'transparent';
    textArea.value            = text;

    document.body.appendChild(textArea);
    textArea.select();

    try {
      var successful = document.execCommand('copy');
      var msg = successful ? 'successful' : 'unsuccessful';
      console.log('Copying text command was ' + msg);
    } catch (err) {
      var myInput = document.getElementById('input-short');
      myInput.focus();
      myInput.setSelectionRange(0, 9999);
      alert('<%= l('Hit Enter, then Ctrl+C to copy the short link') %>');
    }

    document.body.removeChild(textArea);
}

var copyBtn = document.getElementById('clipboard');

copyBtn.addEventListener('click', function(event) {
    event.preventDefault();
    copyTextToClipboard(this.getAttribute('data-short'));
});
frju365's avatar
idem    
frju365 committed
42
43