ckeditor 敏感词标记显示处理方法
直接在原型添加方法:
(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);
}
}
});
启用插件:
config.extraPlugins = "sensitivewordshighlight";
调用:
//设置
CKEDITOR.instances.MYCKDEMO.SensitiveWordsHighlight(["一二","哈哈"], "`~!@#$^&*()=|{}':;',\\[\\]\\.<>/?~!@#¥……&*()—|{}【】‘;:”“'。,、? ","#FFFF00");
//取消
CKEDITOR.instances.MYCKDEMO.CancleSensitiveWordsHighlight();
注意:ckeditor 中setData()方法要刷新iframe非同步方法,同时使用多次出现内容不按逻辑显示~,~
ckeditor 敏感词标记显示处理方法的更多相关文章
- PHP实现的敏感词过滤方法
PHP实现的敏感词过滤方法,以下是一份过滤敏感词的编码.有需要可以参考参考. /** * @todo 敏感词过滤,返回结果 * @param array $list 定义敏感词一维数组 * @para ...
- 基于DFA敏感词查询的算法简析
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中需要对敏感词做一个过滤,首先有几个方案可以选择: a.直 ...
- js检测文章敏感词
在一些博客或者论坛中,文章中的敏感词需要显示出来和高亮显示起到提示用户的作用.这个功能实现的方法有很多,下面是js的实现方式. //将文章中匹配到的敏感词罗列出来 <span style=&qu ...
- JS采用ActiveXObject实现用户在提交表单时屏蔽敏感词的功能
本例中敏感词ciku.txt放在C盘根目录下,采用的ActiveXObject插件获取本地文件内容.使用此插件不需网上下插件,直接用如下js代码即可. 浏览器需修改interner安全选项的级别,启用 ...
- 敏感词过滤和XML的创建
今天我慢下来啦,因为这三天没有新的课程学习内容,自己仅仅看啦一些,这让我停下来栖息片刻:说说现在的生活,简单的进行着,每天要奔波着去上课,然后回来,每天都在想怎样学习这个小知识点,大脑也在想怎样解决程 ...
- [原创] Trie树 php 实现敏感词过滤
目录 背景 简介 存储结构 PHP 其他语言 字符串分割 示例代码 php 优化 缓存字典树 常驻服务 参考文章 背景 项目中需要过滤用户发送的聊天文本, 由于敏感词有将近2W条, 如果用 str_r ...
- 基于DFA算法、RegExp对象和vee-validate实现前端敏感词过滤
面临敏感词过滤的问题,最简单的方案就是对要检测的文本,遍历所有敏感词,逐个检测输入的文本是否包含指定的敏感词. 很明显上面这种实现方法的检测时间会随着敏感词库数量的增加而线性增加.系统会因此面临性能和 ...
- Java基础:String类详解,案例用户登录实现,案例手机号截取实现,案例敏感词替换实现;StringBuilder类详解,StringBuilder和String相互转换,附练习案例.
1.API 1.1 API概述-帮助文档的使用 什么是API API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK ...
- .Net 中两分钟集成敏感词组件
现如今大部分服务都会有用户输入,为了服务的正常运行,很多时候不得不针对输入进行敏感词的检测.替换.如果人工做这样的工作,不仅效率低,成本也高.所以,先让代码去处理输入,成为了经济方便的途径.水弟在这里 ...
随机推荐
- 编程框架—Autofac
Autofac是一款轻量级的IOC框架,性能高. Autofac基本使用步骤: 1.创建容器建造者(Builder): 2.对Builder注册类型. 3.Buildder创建容器(Container ...
- 十六进制字符串转化为byte数组
工作上有这样的需求之前找了好多都不行,好不容易有个可以的赶紧留下来. 原址:http://blog.163.com/roadwalker@126/blog/static/113561841201013 ...
- E: Write error - write (28 No space left on device)
1:在终端中运行cd命令,提示: e: Write error - write (28 No space left on device) E: Cant mmap an empty file 2:使用 ...
- 横向技术分析C#、C++和Java优劣
转自横向技术分析C#.C++和Java优劣 C#诞生之日起,关于C#与Java之间的论战便此起彼伏,至今不辍.抛却Microsoft与Sun之间的恩怨与口角,客观地从技术上讲,C#与Java都是对传统 ...
- C++中结构体与类的区别(结构不能被继承,默认是public,在堆栈中创建,是值类型,而类是引用类型)good
结构是一种用关键字struct声明的自定义数据类型.与类相似,也可以包含构造函数,常数,字段,方法,属性,索引器,运算符和嵌套类型等,不过,结构是值类型. 1.结构的构造函数和类的构造函数不同. a. ...
- Android ActivityManager.killBackgroundProcesses方法去结束
android2.2以后,如果服务在ondestroy里加上了start自己,用kill backgroudprocess通常无法结束自己.有一种最新发现的方法,利用反射调用forceStopPack ...
- 别再说“我已经努力了”,你的“努力”一文不值!
有次,让一个研究生男收集一份资料,快下班了问结果,竟然毛也没有.见我要怒,他慷慨激昂地说:"我已经很努力找了,但真的查不到." 作为主管,"我已经努力"这话我不 ...
- 用指针将字符串a的内容复制到字符串b
#include <stdio.h> #include <stdlib.h> /**int main() { char a[]="i love you very ma ...
- UVA 11796 Dog Distance(向量)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31962 [代码] #include<cstdio> # ...
- vijosP1067Warcraft III 守望者的烦恼
vijosP1067Warcraft III 守望者的烦恼 链接:https://vijos.org/p/1067 [思路] 矩阵乘法. 可以得出递推式: f[i]=sum{ f[n-1], ...