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

我相信这可能与Need Pure / jQuery Javascript解决方案有关,用于从文本区域清除Word HTML

但就我而言,我正在使用CKEditor; 但是,在将数据发送到服务器之前(或在收到数据之后),我想删除“垃圾”HTML标记和评论,例如从最近(2007年或更晚)版本的Microsoft Office粘贴时出现的那些。 因为这里的服务器端是第三方应用程序,如果可以,我更愿意做这个客户端。 是的,我知道这样做的安全风险; 这只是为了清理常见用例中的数据。

有没有可以做到这一点的常用技术或现有的库(尤其是jQuery友好的)? 注意,我不是要编码或删除所有 HTML,只是与Office相关的crud。

您是否尝试过CKEditor内置的Word清理function? 它似乎在使用“从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(); 

如果您对此清理不满意,可以修改default.js以满足清理需求。 有一些可用于清理的配置选项,请查看http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html (搜索“pasteFromWord”选项)。

如果您需要更高级的东西,但这需要服务器访问,我建议您检查WordOff( http://wordoff.org/ )。 您可能能够围绕其服务构建代理和jsonp包装器,以便您可以在不安装服务器的情况下从客户端使用它。