-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblur.js
More file actions
89 lines (75 loc) · 2.42 KB
/
Copy pathblur.js
File metadata and controls
89 lines (75 loc) · 2.42 KB
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function applyBlur(blurPx, detectors) {
const MARKER = "data-blur-applied";
const BLUR_STYLE = `blur(${blurPx}px)`;
function mark(el) {
if (el.getAttribute(MARKER)) return;
el.setAttribute(MARKER, "1");
el.dataset.originalFilter = el.style.filter || "";
el.style.filter = BLUR_STYLE;
}
document.querySelectorAll(".sensitive").forEach(mark);
const patterns = {
dollars: /\$[\d,]+\.\d{2}/,
emails: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/,
phones: /(?:\+?\d{1,3}[-.\s]?)?\(?\d{2,4}\)?[-.\s]?\d{3,4}[-.\s]?\d{3,4}/,
tfn: /\b\d{3}\s?\d{3}\s?\d{3}\b/,
ird: /\b\d{2,3}[-\s]?\d{3}[-\s]?\d{3}\b/,
};
const activePatterns = detectors
.filter((d) => patterns[d])
.map((d) => patterns[d]);
if (activePatterns.length > 0) {
const combined = new RegExp(
activePatterns.map((p) => `(${p.source})`).join("|")
);
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
null
);
const matched = new Set();
while (walker.nextNode()) {
const text = walker.currentNode.textContent;
if (combined.test(text)) {
const el = walker.currentNode.parentElement;
if (el && !el.closest("script, style, noscript")) {
matched.add(el);
}
}
}
matched.forEach((el) => {
const cell = el.closest("td, th, li, dd");
mark(cell || el);
});
}
if (detectors.includes("tableNames")) {
const nameKeywords =
/^(name|full\s?name|employee|first\s?name|last\s?name|staff|worker|member)$/i;
document.querySelectorAll("table").forEach((table) => {
const headers = table.querySelectorAll(
"thead th, thead td, tr:first-child th"
);
const nameColumns = [];
headers.forEach((th, index) => {
const text = th.textContent.trim();
if (nameKeywords.test(text)) {
nameColumns.push(index);
}
});
if (nameColumns.length === 0) return;
table.querySelectorAll("tbody tr").forEach((row) => {
const cells = row.querySelectorAll("td");
nameColumns.forEach((colIdx) => {
if (cells[colIdx]) mark(cells[colIdx]);
});
});
});
}
}
function removeBlur() {
document.querySelectorAll("[data-blur-applied]").forEach((el) => {
el.style.filter = el.dataset.originalFilter || "";
delete el.dataset.originalFilter;
el.removeAttribute("data-blur-applied");
});
}