Project

General

Profile

paste_example.html

Marius Gligor, 07/04/2014 09:26 AM

Download (1.81 KB)

 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8" />
5
<title>Paste from clipboard</title>
6
<script>
7
window.onload=function()
8
{
9
   document.onkeydown = function(e) {return on_keyboard_action(e); }
10
   document.onkeyup = function(e) {return on_keyboardup_action(e); }
11

12
   var ctrl_pressed = false;
13

14
   function on_keyboard_action(event)
15
   {
16
      k = event.keyCode;  
17
      //ctrl
18
      if(k==17)
19
      {
20
      if(ctrl_pressed == false)
21
          ctrl_pressed = true;
22
      if (!window.Clipboard)
23
          pasteCatcher.focus();
24
      }
25
   }
26

27
   function on_keyboardup_action(event)
28
   {
29
      //ctrl
30
      if(k==17)
31
        ctrl_pressed = false;
32
   }
33

34
   //=== Clipboard ================================================================
35
   //firefox
36
   if (!window.Clipboard)
37
   {
38
       var pasteCatcher = document.createElement("div");
39
       pasteCatcher.setAttribute("id", "paste_ff");
40
       pasteCatcher.setAttribute("contenteditable", "");
41
       pasteCatcher.style.cssText = 'opacity:0;position:fixed;top:0px;left:0px;';
42
       pasteCatcher.style.marginLeft = "-20px";
43
       document.body.appendChild(pasteCatcher);
44
       pasteCatcher.focus();
45
   }
46

47
   // ie, chrome, firefox
48
   window.addEventListener("paste", function pasteHandler(e)
49
   {
50
      if(window.clipboardData) 
51
      {   
52
         var txt = window.clipboardData.getData("Text");
53
         alert("ie: " + txt);
54
      }
55
      else if(e.clipboardData) 
56
      {
57
         // get text representation of clipboard
58
         var txt = e.clipboardData.getData("text/plain");
59
         alert("chrome & firefox & opera: " + txt);
60
      }
61
      
62
      e.preventDefault();
63
   });
64
   //=== Clipboard ================================================================
65
};
66
</script>
67
</head>
68
<body>
69
<div>Copy some text on system clipboard than press CTRL-V on this page.</div>
70
</body>
71
</html>