summaryrefslogtreecommitdiff
path: root/assets/test/clipboard.html
blob: 3f17b9cbbc74faff990a3110d50647687881b7e3 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
<body></body>


<script src="/assets/javascripts/math/util.js"></script>
<script>

document.body.addEventListener('copy', function (e) {
  if (disabled) { return }
  if (e.clipboardData) {
    e.preventDefault();
    e.clipboardData.setData("text/plain", canvas.irssi());
  }
  if (window.clipboardData) {
    e.returnValue = false;
    window.clipboardData.setData("text/plain", canvas.irssi());
  }
}, false);


document.body.addEventListener('paste', function (e) {
  toArray(e.clipboardData.items).forEach(function(item,i){
    console.log(item.kind, item.type)
    if (item.kind == 'file' && item.type.match('image/')) {
      var blob = item.getAsFile();
      window.URL = window.URL || window.webkitURL;
      var blobUrl = window.URL.createObjectURL(blob);

      var img = document.createElement('img');
      img.onerror = function(){
        // error!
        console.log("error! bad image!")
      }
      img.onload = function(){
        // load!
        document.body.appendChild(img);
      }
      img.src = blobUrl;
    }
    // this can be text/plain or text/html..
    else if (item.kind == 'string') {
      item.getAsString(function(text){
        var node = document.createElement('span')
        node.innerHTML = text
        document.body.appendChild(node)
      });
    }
    else {
      console.error("unknown type!", item.type)
    }
  })
})
</script>