Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In chrome, using the window.Clipboard object, is there a way to capture pasted text?

You can capture an image. I am trying to figure out how to capture text. I'm guessing there isn't, for security reasons, but I wanted to make sure.

Also is there a reference for this stuff? window.Clipboard object isn't part of the v8 engine, it's a part of the chrome browser and I can't find official documentation for it.

like image 863
user1895546 Avatar asked Dec 11 '12 18:12

user1895546


People also ask

How do I find clipboard data in Chrome?

This hidden feature is available as a flag. To find it, open a new tab, paste chrome://flags into Chrome's Omnibox and then press the Enter key. Search for “Clipboard” in the search box.

How do I view the contents of my clipboard?

To get to your clipboard history at any time, press Windows logo key + V. From the clipboard history, you can paste and pin frequently used items by choosing an individual item from your clipboard menu. Pinning an item keeps it from being removed from the clipboard history to make room for new items.

How do I copy text to clipboard?

Open the file that you want to copy items from. Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.

How do I paste from clipboard in JavaScript?

clipboard to get access to the clipboard. Use writeText() to copy text into the clipboard. Use readText() to paste the text. Make sure you have given browser permissions for Clipboard to avoid Promise rejections.


1 Answers

In the code you linked there is a pasteHandler function with the following:

// 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);
                }
            }
        }

Chrome developer frame is telling me that items[i] is a DataTransferItem (reference)

On the reference page I see a kind property and a getAsString() method. The latter seems to require a callback function that receives the text as a parameter. So to handle text values using the above script you might modify the section I linked as follows:

// 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);
                    });
                }
            }
        }
like image 144
Malk Avatar answered Oct 08 '22 04:10

Malk