我相信这可能与 Need Pure/jQuery Javascript Solution For Cleaning Word HTML From Text Area 有关
但就我而言,我正在使用 CKEditor;但是,在将数据发送到服务器之前(或在收到数据之后),我想去除“垃圾”HTML 标记和注释,例如从最新(2007 或更高版本)的 Microsoft Office 粘贴时出现的标记和注释。因为这里的服务器端是第三方应用程序,所以如果可以的话我更愿意做这个客户端。是的,我知道这样做的安全风险;这只是为了清理常见用例中的数据。
是否有任何通用技术或现有库(尤其是对 jQuery 友好的库)可以做到这一点?请注意,我不打算编码或剥离所有 HTML,只是与 Office 相关的垃圾。
最佳答案
您是否尝试过 CKEditor 内置的 Word 清理功能? 它似乎在使用“从 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 包装器,这样您就可以在不安装服务器的情况下从客户端使用它。
关于javascript - 用于从 CKEditor 中去除伪造 HTML 的客户端 Javascript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3391288/