2012-12-11 12 views
9

You can capture an image. Sto cercando di capire come catturare il testo. Immagino che non ci sia, per ragioni di sicurezza, ma volevo essere sicuro.In chrome, usando la finestra. Oggetto lavagna, c'è un modo per catturare il testo incollato?

Inoltre c'è un riferimento per questa roba? L'oggetto window.Clipboard non fa parte del motore V8, fa parte del browser Chrome e non riesco a trovare la documentazione ufficiale per esso.

+2

Ecco le specifiche per gli elementi degli Appunti si sta lavorando con: http://www.w3.org/TR /2011/WD-html5-20110113/dnd.html#the-datatransferitem-interface. – pimvdb

risposta

13

Nel codice si è collegato v'è una funzione pasteHandler con il seguente:

// Get the items from the clipboard 
     var items = e.clipboardData.items; 
     if (items) { 
      // Loop through all items, looking for any kind of image 
      for (var i = 0; i < items.length; i++) { 
       if (items[i].type.indexOf("image") !== -1) { 
        // We need to represent the image as a file, 
        var blob = items[i].getAsFile(); 
        // and use a URL or webkitURL (whichever is available to the browser) 
        // to create a temporary URL to the object 
        var URLObj = window.URL || window.webkitURL; 
        var source = URLObj.createObjectURL(blob); 

        // The URL can then be used as the source of an image 
        createImage(source); 
       } 
      } 
     } 

Telaio cromato sviluppatore mi sta dicendo che gli elementi [i] è un DataTransferItem(reference)

Nella pagina di riferimento vedo una proprietà kind e un metodo getAsString(). Quest'ultimo sembra richiedere una funzione di callback che riceve il testo come parametro. Quindi, per gestire i valori di testo utilizzando lo script di cui sopra si potrebbe modificare la sezione ho collegato come segue:

// Get the items from the clipboard 
     var items = e.clipboardData.items; 
     if (items) { 
      // Loop through all items, looking for any kind of image 
      for (var i = 0; i < items.length; i++) { 
       if (items[i].type.indexOf("image") !== -1) { 
        // We need to represent the image as a file, 
        var blob = items[i].getAsFile(); 
        // and use a URL or webkitURL (whichever is available to the browser) 
        // to create a temporary URL to the object 
        var URLObj = window.URL || window.webkitURL; 
        var source = URLObj.createObjectURL(blob); 

        // The URL can then be used as the source of an image 
        createImage(source); 
       } 
       if (items[i].kind === "string"){ 
        items[i].getAsString(function(s) { 
         alert(s); 
        }); 
       } 
      } 
     } 
+1

+1 L'ho trovato esattamente nello stesso modo :) Ecco una demo: http://jsfiddle.net/8kEAF/1/. – pimvdb

+0

Ho appena provato. Funziona. Grazie mille. – user1895546