This article mainly introduces the function of reading clipboard pasting screenshots in web pages, that is, you can paste the clipboard screenshot Ctrl+V into an input box of the web page, such as QQ screenshots, Wangwang screenshots or other screenshot software. The specific code is as follows.
<!DOCTYPE HTML><html lang="en-US"><head><meta charset="UTF-8"><title>Use clipboardData to implement screenshot and paste functions in web pages</title><style type="text/css">#box{ width:200px; height:200px; border:1px solid #ddd; }</style></head><body><h1>Use clipboardData to implement screenshot and paste functions in web pages</h1> <hr /><div><input type="text" id="testInput" placeholder="Paste in the input box after screenshot" size="30" /></div><script type="text/javascript">(function(){ var imgReader = function( item ){ var blob = item.getAsFile(), reader = new FileReader(); // After reading the file, it is displayed in the web page reader.onload = function( e ){ var img = new Image(); img.src = e.target.result; document.body.appendChild( img ); }; // Read the file reader.readAsDataURL( blob ); }; document.getElementById( 'testInput' ).addEventListener( 'paste', function( e ){ // The interface to access the system clipboard var clipboardData = e.clipboardData, i = 0, items, item, types; if( clipboardData ){ items = clipboardData.items; if( !items ){ return; } item = items[0]; // The data type saved in the clipboard type = clipboardData.types || []; for( ; i < types.length; i++ ){ if( types[i] === 'Files' ){ item = items[i]; break; } } // Determine whether it is image data if( item && item.kind === 'file' && item.type.match(/^image///i) ){ imgReader( item ); } } } });})(); </script></body></html>The above is all about this article, and I hope it will be helpful for everyone to learn JavaScript programming.