-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-script.js
More file actions
222 lines (199 loc) ยท 5.52 KB
/
Copy pathcontent-script.js
File metadata and controls
222 lines (199 loc) ยท 5.52 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
/****************************************************
* content-script.js
****************************************************/
let CAT1 = []; // ๐
let CAT2 = []; // ๐ฒ๐ฝ
let CAT3 = []; // โ
let CAT4 = []; // โ
let CAT5 = []; // โ
let CAT6 = []; // ๐
let CAT7 = []; // MAGA Donor (๐คก), new
/**
* superNormalize: uppercase, remove parentheses, trademark symbols,
* curly quotes, punctuation, etc.
*/
function superNormalize(str) {
let out = str.toUpperCase();
out = out.replace(/\(.*?\)/g, "");
out = out.replace(/[ยฎโขยฉ]/g, "");
out = out.replace(/[โโ]/g, "'").replace(/[โโ]/g, '"');
out = out.replace(/[^A-Z0-9]/g, "");
return out;
}
/**
* foundMagaDonor checks if text includes any brand from cat7 (โฅ4 chars).
*/
function foundMagaDonor(text) {
const normText = superNormalize(text);
for (const brand of CAT7) {
const nb = superNormalize(brand);
if (nb.length < 4) continue;
if (nb && normText.includes(nb)) {
return true;
}
}
return false;
}
/**
* findBestBrandAndEmoji
* Return { brand, emoji } from the best match in cat1..cat6, ignoring
* brand length <4. Ties break by smaller "priority".
*/
function findBestBrandAndEmoji(originalText) {
const normalizedText = superNormalize(originalText);
let best = { brand: "", length: 0, priority: Infinity, emoji: "" };
const categories = [
{ arr: CAT1, emoji: "๐", priority: 1 },
{ arr: CAT2, emoji: "๐ฒ๐ฝ", priority: 2 },
{ arr: CAT3, emoji: "โ", priority: 3 },
{ arr: CAT4, emoji: "โ", priority: 4 },
{ arr: CAT5, emoji: "โ", priority: 5 },
{ arr: CAT6, emoji: "๐", priority: 6 }
];
for (const cat of categories) {
for (const brand of cat.arr) {
const nb = superNormalize(brand);
if (nb.length < 4) continue;
if (nb && normalizedText.includes(nb)) {
// If brand is longer or tie but lower priority => store
if (
nb.length > best.length ||
(nb.length === best.length && cat.priority < best.priority)
) {
best = {
brand,
length: nb.length,
priority: cat.priority,
emoji: cat.emoji
};
}
}
}
}
if (best.emoji) {
return { brand: best.brand, emoji: best.emoji };
} else {
return null;
}
}
/**
* gatherAllText from an element
*/
function gatherAllText(elem) {
if (!elem) return "";
let txt = "";
elem.childNodes.forEach(child => {
if (child.nodeType === Node.TEXT_NODE) {
txt += child.nodeValue;
} else if (child.nodeType === Node.ELEMENT_NODE) {
txt += gatherAllText(child);
}
});
return txt;
}
/**
* tagElement logic:
* - skip if text already includes any known indicator
* - if MAGA brand => prepend "๐คก MAGA Donor ๐คฌ"
* - then find best brand => append that emoji
*/
function tagElement(el) {
if (!el) return;
const originalText = gatherAllText(el);
if (!originalText) return;
// If already has an indicator, skip
if (/(โ|๐|๐ฒ๐ฝ|โ|โ|๐|MAGA Donor)/.test(originalText)) {
return;
}
let newText = originalText;
// A) if in MAGA cat7 => prepend
if (foundMagaDonor(originalText)) {
newText = "๐คก MAGA Donor ๐คฌ" + newText;
}
// B) see if cat1..cat6 matched => append
const catResult = findBestBrandAndEmoji(originalText);
if (catResult) {
newText += " " + catResult.emoji;
}
el.innerText = newText;
}
/**
* scanBySelectors: attempt known product-title selectors
*/
function scanBySelectors() {
const selectors = [
'[data-test="item-name"]',
'[data-testid="item-detail-name"]',
'[data-testid="item-name"]',
'.item-title',
'.product-title',
'.product-name',
'.item-name',
'.sku-title',
'.css-1nhiovu',
'.css-1kiw93k',
'.product-header',
'.s-title'
];
const combined = selectors.join(",");
const elems = document.querySelectorAll(combined);
elems.forEach(tagElement);
}
/**
* walkTextNodes: do the same logic for raw text nodes
*/
function walkTextNodes() {
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
while (true) {
const node = walker.nextNode();
if (!node) break;
let txt = node.nodeValue;
if (!txt || !txt.trim()) continue;
// skip if it has an indicator
if (/(โ|๐|๐ฒ๐ฝ|โ|โ|๐|MAGA Donor)/.test(txt)) continue;
if (foundMagaDonor(txt)) {
txt = "๐คก MAGA Donor ๐คฌ" + txt;
}
const catResult = findBestBrandAndEmoji(txt);
if (catResult) {
txt += " " + catResult.emoji;
}
node.nodeValue = txt;
}
}
/**
* observeMutations
*/
function observeMutations() {
const obs = new MutationObserver(() => {
scanBySelectors();
walkTextNodes();
});
obs.observe(document.body, { childList: true, subtree: true });
}
/**
* main
*/
function main() {
chrome.storage.sync.get([
"cat1Canadian",
"cat2Mexican",
"cat3US",
"cat4PartialUSCA",
"cat5PartialUSMX",
"cat6Outside",
"cat7MagaDonor"
], (data) => {
CAT1 = data.cat1Canadian || [];
CAT2 = data.cat2Mexican || [];
CAT3 = data.cat3US || [];
CAT4 = data.cat4PartialUSCA || [];
CAT5 = data.cat5PartialUSMX || [];
CAT6 = data.cat6Outside || [];
CAT7 = data.cat7MagaDonor || [];
scanBySelectors();
walkTextNodes();
observeMutations();
});
}
main();