直接在原型添加方法:html
(function () { /* * 取消全部高亮 */ CKEDITOR.editor.prototype.CancleSensitiveWordsHighlight = function () { var regstrEpswh = '<span class="ep_ckeditor_sensitivewords" style="background-color:[^<>:]+[;]*">([^<>]+)<\\/span>'; var htmlEpswh = this.getData(); htmlEpswh = htmlEpswh.replace(eval("/" + regstrEpswh + "/ig"), "$1"); if (this.document != null) this.document.getBody().setHtml(htmlEpswh); return htmlEpswh; } /* * epswhlwords 敏感詞 * epswhligChar 敏感詞中忽略的特殊字符 * epswhlcolor 高亮底色 */ CKEDITOR.editor.prototype.SensitiveWordsHighlight = function (epswhlwords, epswhligChar, epswhlcolor) { //空的字符串 if (typeof epswhlwords == "string" && !epswhlwords) return; //空數組 if (typeof epswhlwords == "object" && epswhlwords[0] == undefined) return; var htmlEpswh = this.getData(); //高亮模板 var highLighCOde = '<span class="ep_ckeditor_sensitivewords" style="background-color:{$color};">{$word}</span>'; if (!epswhlcolor) epswhlcolor = "#ffff00"; highLighCOde = highLighCOde.replace("{$color}", epswhlcolor); //若是內容中有高亮內容先進行清理 if (htmlEpswh.indexOf('ep_ckeditor_sensitivewords') > -1) { htmlEpswh = this.CancleSensitiveWordsHighlight(); } //從新高亮 var epswhlkeyWords = []; if (typeof epswhlwords == "string") epswhlkeyWords = epswhlwords.split(','); else epswhlkeyWords = epswhlwords; //須要忽略的分隔符 if (epswhligChar && epswhligChar.length > 0) { epswhligChar = epswhligChar.replace(/[<>&"]/g, function (c) { return { '<': '<', '>': '>', '&': '&', '"': '"' }[c]; }); epswhligChar = "[" + epswhligChar + "]*"; } else { epswhligChar = ''; } for (var i = 0; i < epswhlkeyWords.length; i++) { var allkey = epswhlkeyWords[i].split(''); var regstr = allkey.join(epswhligChar); regstr = "(" + regstr + ")"; var reg = eval("/" + regstr + "/ig"); var hcode = highLighCOde.replace("{$word}", "$1"); htmlEpswh = htmlEpswh.replace(reg, hcode); } //document 對象在源碼模式無效,this.setData是從新加載,不是同步方法,不能使用 if (this.document!=null) this.document.getBody().setHtml(htmlEpswh); } })();
或者添加插件:數組
CKEDITOR.plugins.add('sensitivewordshighlight', { init: function (editor) { /* * 取消全部高亮 */ editor.CancleSensitiveWordsHighlight=function () { var regstrEpswh = '<span class="ep_ckeditor_sensitivewords" style="background-color:[^<>:]+[;]*">([^<>]+)<\\/span>'; var htmlEpswh = this.getData(); htmlEpswh = htmlEpswh.replace(eval("/" + regstrEpswh + "/ig"), "$1"); if (this.document != null) this.document.getBody().setHtml(htmlEpswh); return htmlEpswh; } /* * words 敏感詞 * igChar 敏感詞中忽略的特殊字符 * color 高亮底色 */ editor.SensitiveWordsHighlight = function (epswhlwords, epswhligChar, epswhlcolor) { //空的字符串 if (typeof epswhlwords == "string" && !epswhlwords) return; //空數組 if (typeof epswhlwords == "object" && epswhlwords[0] == undefined) return; var htmlEpswh = this.getData(); //高亮模板 var highLighCOde = '<span class="ep_ckeditor_sensitivewords" style="background-color:{$color};">{$word}</span>'; if (!epswhlcolor) epswhlcolor = "#ffff00"; highLighCOde = highLighCOde.replace("{$color}", epswhlcolor); //若是內容中有高亮內容先進行清理 if (htmlEpswh.indexOf('ep_ckeditor_sensitivewords') > -1) { htmlEpswh = this.CancleSensitiveWordsHighlight(); } //從新高亮 var epswhlkeyWords = []; if (typeof epswhlwords == "string") epswhlkeyWords = epswhlwords.split(','); else epswhlkeyWords = epswhlwords; //須要忽略的分隔符 if (epswhligChar && epswhligChar.length > 0) { epswhligChar = epswhligChar.replace(/[<>&"]/g, function (c) { return { '<': '<', '>': '>', '&': '&', '"': '"' }[c]; }); epswhligChar = "[" + epswhligChar + "]*"; } else { epswhligChar = ''; } for (var i = 0; i < epswhlkeyWords.length; i++) { var allkey = epswhlkeyWords[i].split(''); var regstr = allkey.join(epswhligChar); regstr = "(" + regstr + ")"; var reg = eval("/" + regstr + "/ig"); var hcode = highLighCOde.replace("{$word}", "$1"); htmlEpswh = htmlEpswh.replace(reg, hcode); } //document 對象在源碼模式無效,this.setData是從新加載,不是同步方法,不能使用 if (this.document != null) this.document.getBody().setHtml(htmlEpswh); } } });
啓用插件:this
config.extraPlugins = "sensitivewordshighlight";
調用:spa
//設置 CKEDITOR.instances.MYCKDEMO.SensitiveWordsHighlight(["一二","哈哈"], "`~!@#$^&*()=|{}':;',\\[\\]\\.<>/?~!@#¥……&*()—|{}【】‘;:」「'。,、? ","#FFFF00"); //取消 CKEDITOR.instances.MYCKDEMO.CancleSensitiveWordsHighlight();
注意:ckeditor 中setData()方法要刷新iframe非同步方法,同時使用屢次出現內容不按邏輯顯示~,~prototype