ckeditor 敏感詞標記顯示處理方法

來源:互聯網
上載者:User

標籤:undefined   frame   eth   高亮   set   val   ack   標記   object   

直接在原型添加方法:

(function () {    /*    *      取消所有高亮    *      noset =true 返回清理後的內容不重新整理編輯器    *      noset =false 或 不傳返回清理後的內容 重新整理編輯器    */    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 { ‘<‘: ‘&lt;‘, ‘>‘: ‘&gt;‘, ‘&‘: ‘&amp;‘, ‘"‘: ‘&quot;‘ }[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 { ‘<‘: ‘&lt;‘, ‘>‘: ‘&gt;‘, ‘&‘: ‘&amp;‘, ‘"‘: ‘&quot;‘ }[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);        }    }});

啟用外掛程式:

config.extraPlugins = "sensitivewordshighlight";

調用:

//設定CKEDITOR.instances.MYCKDEMO.SensitiveWordsHighlight(["一二","哈哈"], "`[email protected]#$^&*()=|{}‘:;‘,\\[\\]\\.<>/?~!@#¥……&*()—|{}【】‘;:”“‘。,、?  ","#FFFF00");//取消CKEDITOR.instances.MYCKDEMO.CancleSensitiveWordsHighlight();

 注意:ckeditor 中setData()方法要重新整理iframe非同步方法,同時使用多次出現內容不按邏輯顯示~,~

ckeditor 敏感詞標記顯示處理方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.