See the input frame of a website to support the function of screen pasting, feel a little meaning, so the code out to share.
Unfortunately, only a high version of the Chrome browser to support this direct paste, other browsers so far have not been able to paste (IE11 not tested), of course, this enhanced user experience is always better than no good.
The structure code for the input box:
Copy Code code as follows:
<input type= "text" id= "Testinput"/>
To bind a paste event for an input box:
Copy Code code as follows:
var input = document.getElementById (' testinput ');
Input.addeventlistener (' Paste ', function (event) {
DoSomething ...
});
The event interface object for pasting events provides an Clipboarddata interface that holds data from the system Clipboard, as described above, where only the newer Chrome browsers have direct access to the system Clipboard data. This provides a portal for directly interacting with pictures that are saved to the Clipboard after the screen is cut.
Here the screen, which is provided by the QQ screen or the system with the PRTSCN key screen function, or other third-party software to provide the screen-cutting function.
Copy Code code as follows:
Input.addeventlistener (' Paste ', function (event) {
interface to the Access system Clipboard that is added to the event object
var clipboarddata = Event.clipboarddata,
i = 0,
Items, item, types;
if (clipboarddata) {
Items = Clipboarddata.items;
if (!items) {
Return
}
item = Items[0];
Data types saved on the Clipboard
Types = Clipboarddata.types | | [];
for (; i < types.length; i++) {
if (types[i] = = ' Files ') {
item = Items[i];
Break
}
}
Determine if the picture data
if (item && item.kind = = ' file ' && item.type.match (/^image\//i)) {
Read the picture
Imgreader (item);
}
}
});
The picture data is taken from the Clipboard and can be read with FileReader.
Copy Code code as follows:
var imgreader = function (item) {
var file = Item.getasfile (),
reader = new FileReader ();
To display a file in a Web page after reading it
Reader.onload = function (e) {
var img = new Image ();
IMG.SRC = E.target.result;
Document.body.appendChild (IMG);
};
Reading files
Reader.readasdataurl (file);
};
Very short code is achieved, you can use the following source to see the demo.
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "UTF-8"
<title> Exploit Clipboarddata in the Web page to achieve the function of screen-pasting </title>
<style type= "Text/css"
#box {width:200px; border:1px solid #ddd;
</style>
<body>
<div><input type= "text" id= "Testinput" placeholder= "screenshot and paste into the input box" size= "30"/ ></div>
<script type= "Text/javascript" >
(function () {
var imgreader = function (item) {
var blob = Item.getasfile (),
reader = new FileReader ();
Reader.onload = function (e) {
var img = new Image ();
IMG.SRC = E.target.result;
Document.body.appendChild (IMG);
};
Reader.readasdataurl (BLOB);
};
document.getElementById (' Testinput '). AddEventListener (' Paste ', function (e) {
var clipboarddata = E.clipboarddata,
i = 0,
Items, item, types;
if (clipboarddata) {
Items = Clipboarddata.items;
if (!items) {
Return
}
item = Items[0];
Types = Clipboarddata.types | | [];
for (; i < types.length; i++) {
if (types[i] = = ' Files ') {
item = Items[i];
Break
}
}
if (item && item.kind = = ' file ' && item.type.match (/^image\//i)) {
Imgreader (item);
}
}
});
})();
</script>
</body>