summaryrefslogtreecommitdiff
path: root/assets/test/clipboard.html
blob: c1c3294b6a28a57af6dc2d92f8df5fbd08286706 (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
54
<body></body>


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

var copy_disabled = true

document.body.addEventListener('copy', function (e) {
  if (copy_disabled) { return }
  var clipboardData = e.clipboardData || window.clipboardData
  if (! clipboardData) return
  e && e.preventDefault();
  clipboardData.setData("text/plain", clipboardFn);
}, false);


document.body.addEventListener('paste', function (e) {
  // firefox does not have e.clipboardData.items,
  // to get paste data use e.clipboardData.getData("text/html")
  // only works with text..
  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>