-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
158 lines (133 loc) · 3.24 KB
/
content.js
File metadata and controls
158 lines (133 loc) · 3.24 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// content.js
// Blurs only real interactive fields (no static text elements)
(() => {
if (window.__hideExtensionLoaded) return;
window.__hideExtensionLoaded = true;
const BLUR_CLASS = "__hide_blurred";
const STYLE_ID = "__hide_blur_style";
let observer = null;
let active = false;
function addStyles() {
if (document.getElementById(STYLE_ID)) return;
const style = document.createElement("style");
style.id = STYLE_ID;
style.textContent = `
.${BLUR_CLASS} {
filter: blur(8px) !important;
transition: filter 0.2s ease !important;
}
`;
document.head.appendChild(style);
}
function removeStyles() {
document.getElementById(STYLE_ID)?.remove();
}
function blurFields(root = document) {
const elements = root.querySelectorAll(`
input,
textarea,
select,
[contenteditable="true"],
[role="textbox"]
`);
elements.forEach(el => {
el.classList.add(BLUR_CLASS);
});
}
function unblurFields() {
document
.querySelectorAll(`.${BLUR_CLASS}`)
.forEach(el => el.classList.remove(BLUR_CLASS));
}
function blurIframes() {
document.querySelectorAll("iframe").forEach(frame => {
frame.classList.add(BLUR_CLASS);
});
}
function unblurIframes() {
document.querySelectorAll("iframe").forEach(frame => {
frame.classList.remove(BLUR_CLASS);
});
}
function startWatching() {
observer = new MutationObserver(mutations => {
for (const mutation of mutations) {
mutation.addedNodes.forEach(node => {
if (node.nodeType !== 1) return;
// If the node itself is interactive
if (
node.matches?.(`
input,
textarea,
select,
[contenteditable="true"],
[role="textbox"]
`)
) {
node.classList.add(BLUR_CLASS);
}
// Also scan inside it
blurFields(node);
});
}
blurIframes();
});
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
}
function stopWatching() {
if (observer) {
observer.disconnect();
observer = null;
}
}
function enable() {
addStyles();
blurFields();
blurIframes();
startWatching();
active = true;
}
function disable() {
stopWatching();
unblurFields();
unblurIframes();
removeStyles();
active = false;
}
function getCount() {
return document.querySelectorAll(`
input,
textarea,
select,
iframe,
[contenteditable="true"],
[role="textbox"]
`).length;
}
chrome.runtime.onMessage.addListener((msg, _, sendResponse) => {
if (msg.type === "PING") {
sendResponse({ pong: true });
return true;
}
if (msg.type === "TOGGLE") {
active ? disable() : enable();
sendResponse({ active, fieldCount: getCount() });
return true;
}
if (msg.type === "GET_COUNT") {
sendResponse({ fieldCount: getCount() });
return true;
}
});
window.addEventListener(
"pagehide",
() => {
if (active) disable();
window.__hideExtensionLoaded = false;
},
{ once: true }
);
})();