最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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.

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.

Share Improve this question edited Dec 11, 2012 at 19:28 JSW189 6,32512 gold badges46 silver badges73 bronze badges asked Dec 11, 2012 at 18:08 user1895546user1895546 1011 gold badge1 silver badge5 bronze badges 2
  • 2 Here are the specs for the clipboard items you're working with: w3/TR/2011/WD-html5-20110113/…. – pimvdb Commented Dec 11, 2012 at 19:08
  • if the url in the question stays broken, try: gist.github./summerblue/5611058 – George Birbilis Commented Jul 24, 2022 at 17:51
Add a ment  | 

1 Answer 1

Reset to default 13

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论