CKEditor - 阻止用户粘贴图像

时间:2011-07-05 12:21:58

标签: ckeditor

我想使用优秀的CKEditor为我的用户提供一个有限的编辑器。

我试图阻止人们添加图片,因此我屏蔽了“来源”视图并停用了“粘贴”按钮(只留下了粘贴为文字按钮)。

但是,仍然可以粘贴图像(从网页复制)。 有没有办法防止这种情况发生?

感谢。

4 个答案:

答案 0 :(得分:5)

我知道它已经有一段时间但是如果有其他人遇到同样的问题。

您应该使用a plugin as described here来检查所有图像,如果用户尝试插入图像,则会提示他"图像"是不允许的。

请注意,该插件无法下载,因此我们可能需要创建自己的插件。它非常简单。我们只需将其代码复制并粘贴到plugin.js文件中。

CKEDITOR.plugins.add( 'blockimagepaste',
{
    init : function( editor )
    {

    function replaceImgText(html) {
            var ret = html.replace( /<img[^>]*src="data:image\/(bmp|dds|gif|jpg|jpeg|png|psd|pspimage|tga|thm|tif|tiff|yuv|ai|eps|ps|svg);base64,.*?"[^>]*>/gi, function( img ){
                        alert("Direct image paste is not allowed.");
                        return '';

                     });
            return ret;
        }

        function chkImg() {
            // don't execute code if the editor is readOnly
            if (editor.readOnly)
                return;

            setTimeout( function() {
                editor.document.$.body.innerHTML = replaceImgText(editor.document.$.body.innerHTML);
            },100);
        }

        editor.on( 'contentDom', function() {
            // For Firefox
            editor.document.on('drop', chkImg);
            // For IE
            editor.document.getBody().on('drop', chkImg);
        });

        editor.on( 'paste', function(e) {

        var html = e.data.dataValue;
            if (!html)
                return;

        e.data.dataValue = replaceImgText(html);
        });

    } //Init
} );

另一个选项is explained here(我认为只适用于粘贴事件,在拖动图片时不会执行任何操作!)

答案 1 :(得分:3)

您可以使用'paste' event,这样您就可以删除任何您不喜欢的内容。当然,您还应该在保存之前验证服务器上的内容。

答案 2 :(得分:1)

这很有用,我使用了Nis的解决方案。但问题是,如果删除图像,粘贴事件将丢失。我做了一些改变以防止这种情况。

(function(){
    var pluginName = 'blockimagepaste';
    function replaceImgText(html) {
        var ret = html.replace( /<img[^>]*src="data:image\/(bmp|dds|gif|jpg|jpeg|png|psd|pspimage|tga|thm|tif|tiff|yuv|ai|eps|ps|svg);base64,.*?"[^>]*>/gi, function( img ){
            alert("Direct image paste is not allowed.");
            return '';
        });
        return ret;
    };

    function chkImg(editor) {
        // don't execute code if the editor is readOnly
        if (editor.readOnly)
            return;

        setTimeout( function() {
            editor.document.$.body.innerHTML = replaceImgText(editor.document.$.body.innerHTML);
        },100);
    };

    CKEDITOR.plugins.add( pluginName, {
        icons: pluginName,
        init : function( editor ){

            editor.on( 'contentDom', function() {
                // For Firefox
                editor.document.on('drop', function(e) {chkImg(editor);});
                // For IE
                editor.document.getBody().on('drop', function(e) {chkImg(editor);});

                editor.document.on( 'paste', function(e) {chkImg(editor);});

                // For IE
                editor.document.getBody().on('paste', function(e) {chkImg(editor);});
            });

        } //Init
    });

})();

答案 3 :(得分:0)

如果您希望能够在Source视图中阻止此操作,只需将此代码添加到您的插件中:

editor.on('key', function(e) {
    var html = CKEDITOR.currentInstance.getData();
    if (!html) {
        return;
    }
    CKEDITOR.currentInstance.setData(replaceImgText(html));
});
相关问题