在面向客户开发时,特别是根据客户输入的内容进行入库试,无法预计会输入什么,所以需要对客户输入的内容进行过滤,以免引起不必要的bug甚至数据库崩掉
参考网站
安装插件
引入插件
| 1
 | import { getDefaultWhiteList, FilterXSS } from 'xss';
 | 
封装函数
插件的github文档中给出了很多api,根据自己的需求进行封装即可,如下所示:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | export const filterXSS = (() => {const whiteList: any = getDefaultWhiteList();
 
 for (const i of Object.keys(whiteList)) {
 whiteList[i].push('style', 'class');
 if (i === 'table' && whiteList[i]) {
 whiteList[i].push('cellpadding', 'cellspacing', 'bordercolor');
 }
 }
 whiteList.strike = ['style', 'class'];
 const options = {
 whiteList,
 css: false,
 stripIgnoreTag: true,
 stripIgnoreTagBody: ['script', 'style'],
 onTagAttr(tag: any, name: any, value: any, isWhiteAttr: any) {
 
 if (tag === 'img' && name === 'src') {
 return `${name}="data:image/ico;base64,aWNv" data-${name}=${value}`;
 }
 
 if (tag === 'a') {
 return `${name}=${value} style="pointer-events: none;"`;
 }
 
 },
 };
 const myxss = new FilterXSS(options);
 return myxss.process.bind(myxss);
 })();
 
 | 
使用方法
| 12
 3
 4
 
 | import { filterXSS } from '@/utils';
 var filterData = filterXSS(res.data);
 console.log(filterData);
 
 | 
祝君无Bug~