代码之家  ›  专栏  ›  技术社区  ›  Dan

客户端Javascript代码,用于从CKEditor中删除伪造的HTML

  •  3
  • Dan  · 技术社区  · 16 年前

    我相信这可能与 Need Pure/jQuery Javascript Solution For Cleaning Word HTML From Text Area

    是 使用编辑器;但是,在将数据发送到服务器之前(或在接收回数据之后),我希望去掉“垃圾”HTML标记和注释,例如粘贴最新(2007或更高版本)的Microsoft Office时出现的标记和注释。因为这里的服务器端是第三方应用程序,所以如果可以的话,我更愿意做这个客户端。是的,我知道这样做的安全风险;这只是为了清理常见用例中的数据。

    有没有什么常用的技术或现有的库(特别是jQuery友好的库)可以做到这一点?注意,我不想编码或剥离 全部的

    1 回复  |  直到 9 年前
        1
  •  3
  •   Amitay Dobo    16 年前

    当使用“从Word粘贴”对话框时,它似乎会自动运行,但也可以从代码中使用。 我不是CKEditor API方面的专家,因此可能有一种更有效或更正确的方法来实现这一点,但在当前版本(3.3.1)中似乎可以做到这一点:

    function cleanUp() {
    
        if (!CKEDITOR.cleanWord) {
            // since the filter is lazily loaded by the pastefromword plugin we need to add it ourselves. 
            // We use the same function as the callback for when the cleanup filter is loaded. Change the script path to the correct one
            CKEDITOR.scriptLoader.load("../plugins/pastefromword/filter/default.js", cleanUp, null, false, true );
            alert('loading script for the first usage');
        } else { // The cleanWord is available for use
    
            // change to the correct editor instance
            var editor = CKEDITOR.instances.editor1;
            // perform the clean up
            var cleanedUpData = CKEDITOR.cleanWord(editor .getData(),  editor );
    
            // do something with the clean up
            alert(cleanedUpData);
        }
    }
    
    cleanUp();
    

    http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html (搜索“pasteFromWord”选项)。

    如果您需要更高级的东西,但是需要服务器访问,我建议您关闭WordOff( http://wordoff.org/ ). 您可以围绕其服务构建一个代理和jsonp包装器,这样就可以从客户端使用它,而无需安装服务器。