Safari是否正确处理粘贴事件? [英] Does Safari handle the paste event correctly?

查看:140
本文介绍了Safari是否正确处理粘贴事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Safari编写一些用于处理'粘贴'事件的代码,但它似乎无法正常工作。根据WebKit DOM参考, oncut onpaste oncopy 或多或少都像W3C Clipboard API建议的那样处理。但是,它不像我期望的那样工作。我正在粘贴图像数据,但据我所知,我遇到的问题适用于任何类型的粘贴。这个 jsfiddle 在Chrome中运行得很好,但在OSX上的Safari 6.0.4中却不行。

I'm trying to write some code for Safari for handling the 'paste' event, but it doesn't seem to work right. According to the WebKit DOM reference, oncut, onpaste, and oncopy are all handled more-or-less like the W3C Clipboard API suggests. However, it doesn't work like I expect. I'm pasting image data, but as far as I've been able to tell, the problem I'm having applies to any kind of paste. This jsfiddle works just fine in Chrome, but not in Safari 6.0.4 on OSX.

$(function () {
    console.log("ready");
    $("#pastearea").on("paste", function (e) {
        e.preventDefault();
        console.debug("testing paste in safari");
        var blob = e.originalEvent.clipboardData.items[0].getAsFile();
        console.debug(blob);
        var reader = new FileReader();
        reader.onload = readerLoaded;

        reader.readAsDataURL(blob);
    });
});

function readerLoaded(e) {
    $("#dest").attr("src", e.target.result);
}

我再次尝试使用普通JS 。仍然没有快乐:

I tried again using just plain JS. Still no joy:

<div id="pastearea" onpaste="plainjsOnPaste()" style="width: 100px; height: 100px; background-color: blue;"/>

function plainjsOnPaste(e) {
    console.log("blahblahblah");
    console.log(e);   
}

如果Safari出现问题,那么显然我不应该期待jQuery工作。据我所知,在第二次尝试(简单)中,我正在完成WebKit参考建议我应该做的事情,但它根本不起作用。这是Safari的一些已知限制,还是椅子和键盘之间的问题?

If there's some issue with Safari, then obviously I shouldn't expect jQuery to work. As far as I can tell, in the second attempt (plain) I'm doing exactly what the WebKit reference suggests I should do, but it doesn't work at all. Is this some known limitation of Safari, or is the problem between the chair and keyboard?

更新:似乎Safari没有实现W3C的剪贴板API工作草案。我正在研究解决方法,但如果有人知道我会喜欢听到它。

Update: it seems that Safari doesn't implement the W3C's working draft of the Clipboard APIs. I'm researching workarounds, but if anyone knows one I'd love to heard it.

推荐答案

我认为答案是,尽管不满意,但是不。请参阅此WebKit错误:

I think the answer, as unsatisfying as it might be, is "no". See this WebKit bug:

https ://bugs.webkit.org/show_bug.cgi?id = 75891

如果您打算将粘贴数据接收到不是contentEditable的内容,文本输入或文本区域,我不知道有什么方法可以使当前版本的Safari做到这一点。

If your intention is to receive paste data into something that's not contentEditable, a text input or a text area, I don't know of any method to make the current version of Safari do this.

更新:在这个JSFiddle ,简化为只处理文本,在Safari 6.0.5中不起作用。它尝试一种解决方法,在按下Cmd-V时自动聚焦隐藏文本字段,只是为了在Safari中启用粘贴。它确实阻止了你不能粘贴蜂鸣声,但没有发送粘贴事件,也没有任何东西被粘贴到秘密输入中。

Update: an attempted work-around in this JSFiddle, simplified to only deal with text, does not work in Safari 6.0.5. It attempts a work-around where a hidden text field is automatically focused when Cmd-V is pressed, just in order to enable pasting in Safari. It does prevent the "you can't paste beep", but no paste event is sent and nothing is pasted into the secret input.

$(function () {
    $(window).bind('keydown', function (e) {
        // Cmd-V
        if (e.which == 86 && e.metaKey) {
            if (e.target.nodeName.toUpperCase() !== "INPUT")
                $('#secretinput').focus();
        }
    });

    $(window).bind('beforepaste', function (e) {
        return false;
    });

    $(window).bind('paste', function (e) {
        var clipboardData = e.originalEvent.clipboardData;
        console.log(clipboardData);
        $('#textdest').html(clipboardData.getData('text/plain'));
    });
});

这篇关于Safari是否正确处理粘贴事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆