Maskify

Ensitive Data Anonymizer

目前为 2024-09-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Maskify
  3. // @namespace shawnzhang31
  4. // @license MIT
  5. // @version 0.1
  6. // @description Ensitive Data Anonymizer
  7. // @author 守着瓜的猹
  8. // @match http://10.250.186.247:20201/project/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 正则表达式匹配IP地址
  16. const ipRegex = /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/g;
  17.  
  18. // 正则表达式匹配电话号码(简单示例,可以根据实际情况调整)
  19. //const phoneRegexCN = /^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}\b/g;
  20. //const phoneRegexCN = /(?:\+?0?86)?1[3-9]\d{9}\b/g; // 支持+86前缀
  21. const phoneRegexCN = /(\+?0?86)?(1[3-9]\d{1})\d{5}(\d{3})\b/g;
  22.  
  23.  
  24. // 遍历页面的所有文本节点,并替换内容
  25. function maskSensitiveData() {
  26. const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
  27. let node;
  28. while (node = walker.nextNode()) {
  29. let newText = node.nodeValue;
  30.  
  31. // 替换IP地址的中间部分为*
  32. newText = newText.replace(ipRegex, (match, p1, p2, p3, p4) => {
  33. return `${p1}.*.*.${p4}`;
  34. });
  35.  
  36.  
  37. // 替换电话号码中间部分为*
  38. newText = newText.replace(phoneRegexCN, (match, countryCode, firstPart, lastPart) => {
  39. return `${countryCode || ''}${firstPart}*****${lastPart}`;
  40. });
  41.  
  42.  
  43. // 更新文本节点
  44. if (newText !== node.nodeValue) {
  45. node.nodeValue = newText;
  46. }
  47. }
  48. }
  49.  
  50. // 执行替换
  51. // maskSensitiveData();
  52. const observer = new MutationObserver(maskSensitiveData);
  53. observer.observe(document.body, { childList: true, subtree: true });
  54. })();