SoFunction
Updated on 2025-03-02

js uses clipboardData to implement screenshot and paste function

This example shares the implementation code for clipboardData screenshot pasting for your reference. The specific content is as follows

<!DOCTYPE html> 
<html> 
<head lang="en"> 
  <meta charset="UTF-8"> 
  <title>copyimg</title> 
 
  <style type="text/css"> 
    #box{ width:200px; height:200px; border:1px solid #ddd; } 
  </style> 
 
</head> 
 
<script language="JavaScript"> 
 
 
</script> 
 
<body> 
<h1>use clipboardData Implement screenshot and paste functions in web pages</h1> 
<hr /> 
<div><input type="text"  placeholder="Paste the screenshot into the input box" size="30" /></div> 
<script type="text/javascript"> 
  (function(){ 
    var imgReader = function( item ){ 
      var blob = (), 
          reader = new FileReader(); 
 
       = function( e ){ 
        var img = new Image(); 
 
         = ; 
 
        ( img ); 
      }; 
 
      ( blob ); 
    }; 
    ( 'testInput' ).addEventListener( 'paste', function( e ){ 
      var clipboardData = , 
          i = 0, 
          items, item, types; 
 
      if( clipboardData ){ 
        items = ; 
 
        if( !items ){ 
          return; 
        } 
 
        item = items[0]; 
        types =  || []; 
 
        for( ; i < ; i++ ){ 
          if( types[i] === 'Files' ){ 
            item = items[i]; 
            break; 
          } 
        } 
 
        if( item &&  === 'file' && (/^image\//i) ){ 
          imgReader( item ); 
        } 
      } 
    }); 
  })(); 
</script> 
 
</body> 
</html> 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.