-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
368 lines (319 loc) · 10.7 KB
/
Copy pathcontent.js
File metadata and controls
368 lines (319 loc) · 10.7 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
const CURRENCY_PATTERN =
/(?:[$€£¥₹₩₽₺₫₪₱฿₴₦₲₵₡₭₮₨₸₼₥₧₯₠₢₣₤]|USD|EUR|GBP|JPY|CAD|AUD|NZD|CHF|CNY|RMB|HKD|SGD|SEK|NOK|DKK)\s?\d[\d,.\s]*/gi;
const TRAILING_CURRENCY_PATTERN =
/\d[\d,.\s]*\s?(?:[$€£¥₹₩₽₺₫₪₱฿₴₦₲₵₡₭₮₨₸₼₥₧₯₠₢₣₤]|USD|EUR|GBP|JPY|CAD|AUD|NZD|CHF|CNY|RMB|HKD|SGD|SEK|NOK|DKK)/gi;
const COMBINED_PATTERN = new RegExp(
`(${CURRENCY_PATTERN.source})|(${TRAILING_CURRENCY_PATTERN.source})`,
"gi"
);
const SKIP_TAGS = new Set([
"SCRIPT",
"STYLE",
"NOSCRIPT",
"TEXTAREA",
"INPUT",
"SELECT",
"OPTION"
]);
// currency symbols for heuristic detection
const CURRENCY_SYMBOLS = /[$€£¥₹₩₽₺₫₪₱฿₴₦₲₵₡₭₮₨₸₼₥₧₯₠₢₣₤]/;
const CURRENCY_CODES = /\b(USD|EUR|GBP|JPY|CAD|AUD|NZD|CHF|CNY|RMB|HKD|SGD|SEK|NOK|DKK)\b/i;
// elements that are too broad to be price containers
const SKIP_CONTAINER_TAGS = new Set([
"HTML", "BODY", "MAIN", "ARTICLE", "SECTION", "DIV", "HEADER", "FOOTER",
"NAV", "ASIDE", "TABLE", "TBODY", "THEAD", "TR", "UL", "OL", "DL",
"FORM", "FIELDSET"
]);
// inject CSS to handle the visual masking (non-destructive)
// when extension is disabled, CSS is not injected, so:
// - original text becomes visible (font-size returns to normal)
// - ::after pseudo-element doesn't render (no CSS to define it)
function injectStyles() {
if (document.getElementById("price-hider-styles")) {
return;
}
const style = document.createElement("style");
style.id = "price-hider-styles";
style.textContent = `
/* Text-node based price hiding */
[data-price-hider] {
font-size: 0 !important;
letter-spacing: -9999px !important;
}
[data-price-hider]::after {
content: "•••";
font-size: 1rem !important;
letter-spacing: normal !important;
}
/* element-level price hiding (for split-element prices) */
[data-price-hider-element] {
font-size: 0 !important;
letter-spacing: -9999px !important;
visibility: visible !important;
}
[data-price-hider-element]::after {
content: "•••";
font-size: 1rem !important;
letter-spacing: normal !important;
visibility: visible !important;
}
/* Hide all nested elements inside price containers */
[data-price-hider-element] * {
visibility: hidden !important;
}
`;
(document.head || document.documentElement).appendChild(style);
}
// check if text content looks like a price (has currency + digits)
function looksLikePrice(text) {
if (!text) return false;
const hasCurrency = CURRENCY_SYMBOLS.test(text) || CURRENCY_CODES.test(text);
const hasDigits = /\d/.test(text);
return hasCurrency && hasDigits;
}
// check if an element is a good candidate for a price container
// (small, contains price-like content split across children)
function isPriceContainer(el) {
// skip if already processed or ignored
if (el.hasAttribute("data-price-hider-element") ||
el.hasAttribute("data-price-hider") ||
el.closest("[data-price-hider-ignore]")) {
return false;
}
// skip broad container tags
if (SKIP_CONTAINER_TAGS.has(el.tagName)) {
return false;
}
const text = el.textContent || "";
// must look like a price
if (!looksLikePrice(text)) {
return false;
}
// text should be relatively short (prices are compact)
// this avoids matching large containers that happen to contain prices
const trimmedText = text.replace(/\s+/g, "");
if (trimmedText.length > 50) {
return false;
}
// check if the price is split across multiple child elements
// (e.g., currency symbol in one element, digits in another)
const childElements = el.querySelectorAll("*");
if (childElements.length === 0) {
// no children - this might be a simple text element
// Let the text-node masking handle it
return false;
}
// check if currency and digits are in different text nodes/elements
let hasCurrencyChild = false;
let hasDigitChild = false;
for (const child of childElements) {
// only check direct text content of this element (not nested)
const directText = Array.from(child.childNodes)
.filter(n => n.nodeType === Node.TEXT_NODE)
.map(n => n.textContent)
.join("");
if (CURRENCY_SYMBOLS.test(directText) || CURRENCY_CODES.test(directText)) {
hasCurrencyChild = true;
}
if (/\d/.test(directText)) {
hasDigitChild = true;
}
}
// it's a split-element price if currency and digits are in different children
return hasCurrencyChild || hasDigitChild;
}
// find and mark price container elements using heuristics
function maskPriceElements(root) {
// strategy 1: Schema.org and accessibility attributes (most stable)
const stableSelectors = [
"[itemprop='price']:not([data-price-hider-element])",
"[data-price]:not([data-price-hider-element])",
];
stableSelectors.forEach((selector) => {
try {
root.querySelectorAll(selector).forEach((el) => {
if (!el.closest("[data-price-hider-ignore]") && looksLikePrice(el.textContent)) {
el.setAttribute("data-price-hider-element", "");
}
});
} catch (e) { /* invalid selector */ }
});
// strategy 2: elements with aria-label containing prices
try {
root.querySelectorAll("[aria-label]:not([data-price-hider-element])").forEach((el) => {
const label = el.getAttribute("aria-label") || "";
if (looksLikePrice(label) && !el.closest("[data-price-hider-ignore]")) {
el.setAttribute("data-price-hider-element", "");
}
});
} catch (e) { /* skip */ }
// strategy 3: heuristic detection for split-element prices
// Look for small elements containing currency + digits across children
try {
// target common inline/small container elements
const candidates = root.querySelectorAll(
"span:not([data-price-hider-element]), " +
"div:not([data-price-hider-element]), " +
"p:not([data-price-hider-element]), " +
"td:not([data-price-hider-element]), " +
"li:not([data-price-hider-element]), " +
"a:not([data-price-hider-element]), " +
"strong:not([data-price-hider-element]), " +
"b:not([data-price-hider-element]), " +
"em:not([data-price-hider-element])"
);
candidates.forEach((el) => {
if (isPriceContainer(el)) {
// make sure we're not inside an already-marked container
if (!el.closest("[data-price-hider-element]")) {
el.setAttribute("data-price-hider-element", "");
}
}
});
} catch (e) { /* skip */ }
}
function shouldSkipTextNode(textNode) {
const parent = textNode.parentElement;
if (!parent) {
return true;
}
if (SKIP_TAGS.has(parent.tagName)) {
return true;
}
// skip if already processed by text-node masking (non-destructive approach)
if (parent.hasAttribute("data-price-hider") || parent.closest("[data-price-hider]")) {
return true;
}
// skip if inside an element-level price container (split-element prices)
if (parent.closest("[data-price-hider-element]")) {
return true;
}
return Boolean(parent.closest("[data-price-hider-ignore]"));
}
function hasPriceMatch(text) {
COMBINED_PATTERN.lastIndex = 0;
return COMBINED_PATTERN.test(text);
}
function maskTextNode(textNode) {
if (!textNode || textNode.nodeType !== Node.TEXT_NODE) {
return;
}
if (shouldSkipTextNode(textNode)) {
return;
}
const original = textNode.textContent;
if (!original || original.trim().length === 0) {
return;
}
if (!hasPriceMatch(original)) {
return;
}
// non-destructive approach: wrap price in a span
// CSS makes the text invisible and shows "•••" via ::after pseudo-element
// when extension is disabled, CSS is gone and original text is visible
const fragment = document.createDocumentFragment();
let lastIndex = 0;
COMBINED_PATTERN.lastIndex = 0;
let match;
while ((match = COMBINED_PATTERN.exec(original)) !== null) {
if (match.index > lastIndex) {
fragment.appendChild(
document.createTextNode(original.slice(lastIndex, match.index))
);
}
// wrapper span - CSS hides the text content and shows "•••" via ::after
// when extension is disabled, CSS is gone so original text shows
const wrapper = document.createElement("span");
wrapper.setAttribute("data-price-hider", "");
wrapper.textContent = match[0]; // original preserved in DOM
fragment.appendChild(wrapper);
lastIndex = COMBINED_PATTERN.lastIndex;
}
if (lastIndex < original.length) {
fragment.appendChild(document.createTextNode(original.slice(lastIndex)));
}
textNode.parentNode.replaceChild(fragment, textNode);
}
function walkAndMask(root) {
// first, detect and mark split-element price containers via heuristics
if (root.nodeType === Node.ELEMENT_NODE) {
maskPriceElements(root);
}
// collect all text nodes first to avoid TreeWalker issues
// when we modify the DOM during iteration
const textNodes = [];
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null);
let current = walker.nextNode();
while (current) {
textNodes.push(current);
current = walker.nextNode();
}
// now process them (safe to modify DOM)
textNodes.forEach((node) => maskTextNode(node));
}
function maskExistingPage() {
walkAndMask(document.body);
}
function observeMutations() {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === "characterData") {
maskTextNode(mutation.target);
}
if (mutation.type === "childList") {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.TEXT_NODE) {
maskTextNode(node);
return;
}
if (node.nodeType === Node.ELEMENT_NODE) {
walkAndMask(node);
}
});
}
}
});
observer.observe(document.documentElement, {
subtree: true,
childList: true,
characterData: true
});
}
function init() {
injectStyles();
if (document.body) {
maskExistingPage();
}
observeMutations();
}
// ensure DOM is ready
if (typeof document !== 'undefined') {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
}
// export functions for testing (Node.js environment)
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
CURRENCY_PATTERN,
TRAILING_CURRENCY_PATTERN,
COMBINED_PATTERN,
SKIP_TAGS,
CURRENCY_SYMBOLS,
CURRENCY_CODES,
SKIP_CONTAINER_TAGS,
injectStyles,
looksLikePrice,
isPriceContainer,
shouldSkipTextNode,
hasPriceMatch,
maskTextNode,
maskPriceElements,
walkAndMask,
maskExistingPage,
observeMutations,
init
};
}