CKEditor - 阻止用户粘贴图像

发布于 2024-11-18 15:58:23 字数 152 浏览 3 评论 0原文

我想为我的用户提供一个有限的编辑器,使用出色的 CKEditor。

我试图阻止人们添加图像,因此我阻止了“源”视图并禁用了“粘贴”按钮(仅保留“粘贴为文本”按钮)。

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

谢谢。

I would like giving my users a limited editor, using the great CKEditor.

I was trying to prevent people from adding images, therefore I've blocked the "Source" view and disabled the "paste" button (leaving only the Paste as Text button).

However, it is still possible to paste images (copied from web page).
Is there a way to prevent that as well ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江心雾 2024-11-25 15:58:23

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

您应该使用如上所述的插件这里检查所有图像,如果用户尝试插入图像,则会警告他不允许使用“图像”。

请注意,该插件无法下载,因此我们可能必须创建自己的插件。它非常简单。我们只需将他的代码复制并粘贴到 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
} );

另一个选项解释如下 (我相信这只适用于粘贴事件,当拖动图像时不会执行任何操作!)

I know its been a while but if anyone else comes across this same problem.

You should use a plugin as described here to check for all images and if user tries to insert an image, then he is alerted that "images" are not allowed.

Please note that the plugin is not available to download so we might have to create our own plugin. Its pretty simple. We just have to copy and paste his code in plugin.js file.

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

Another option is explained here (which I believe only works on paste event doesn't do anything when image is dragged ! )

瞎闹 2024-11-25 15:58:23

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

You can use the 'paste' event, that way you can remove anything that you don't like. And of course, you should also verify the content at the server before saving.

凹づ凸ル 2024-11-25 15:58:23

这很有用,我使用了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
    });

})();

that was useful, I used the solution of Nis. but the problem is that if you drop a image, the paste event is lost. I made a change to prevent this situation.

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

})();
小忆控 2024-11-25 15:58:23

如果您也希望能够在 Source 视图中阻止这种情况,只需将此代码添加到您的插件中即可:

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

If you want to be able to prevent this in the Source view as well, just add this code to your plugin:

editor.on('key', function(e) {
    var html = CKEDITOR.currentInstance.getData();
    if (!html) {
        return;
    }
    CKEDITOR.currentInstance.setData(replaceImgText(html));
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文