javascript 在 chrome 中,使用 window.Clipboard 对象,有没有办法捕获粘贴的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13825987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:53:27  来源:igfitidea点击:

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

javascriptgoogle-chromecopyclipboardpaste

提问by user1895546

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.Clipboardobject isn't part of the v8 engine, it's a part of the chrome browser and I can't find official documentation for it.

还有这个东西的参考吗?window.Clipboardobject 不是 v8 引擎的一部分,它是 chrome 浏览器的一部分,我找不到它的官方文档。

回答by Malk

In the code you linked there is a pasteHandlerfunction with the following:

在您链接的代码中,有一个pasteHandler具有以下功能的函数:

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

Chrome 开发者框架告诉我 items[i] 是一个DataTransferItem(参考)

On the reference page I see a kindproperty 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:

在参考页面上,我看到了一个kind属性和一个getAsString()方法。后者似乎需要一个接收文本作为参数的回调函数。因此,要使用上述脚本处理文本值,您可以修改我链接的部分,如下所示:

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