-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent.js
More file actions
433 lines (375 loc) · 14.7 KB
/
content.js
File metadata and controls
433 lines (375 loc) · 14.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
(function () {
// Prevent multiple runs on the same page
if (window.hypeLessLiActivated) {
return;
}
window.hypeLessLiActivated = true;
let isEnabled = true;
let isSidebarVisible = false; // Start with sidebar hidden
let elements = {
sidebar: null,
floatBtn: null,
tooltip: null,
highlights: []
};
// Initialize with extension state
initializeExtensionState();
function initializeExtensionState() {
// Check storage for current state
if (chrome.storage) {
chrome.storage.local.get(['hypeLessEnabled']).then(result => {
isEnabled = result.hypeLessEnabled ?? true;
if (isEnabled) {
initializeExtension();
}
}).catch(() => {
// Fallback if storage fails
isEnabled = true;
initializeExtension();
});
} else {
// Fallback if chrome.storage not available
isEnabled = true;
initializeExtension();
}
}
function initializeExtension() {
const terms = window.hypeLessTerms;
if (!terms || terms.length === 0) return;
const termMap = new Map(terms.map(t => [t.term.toLowerCase(), t.explanation]));
const textNodes = getTextNodes(document.body);
const regex = buildCombinedRegex(terms);
const { matchesByTerm, termCounts } = highlightMatches(textNodes, regex, termMap);
elements.sidebar = buildSidebar(termCounts, matchesByTerm);
elements.floatBtn = buildFloatButton();
elements.tooltip = buildTooltip();
setupInteractions(elements.sidebar, elements.floatBtn, matchesByTerm, elements.tooltip);
// Start with sidebar hidden
elements.sidebar.classList.add("collapsed");
elements.floatBtn.style.display = "block";
}
function escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\\\$&");
}
function getTextNodes(root) {
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
const nodes = [];
while (walker.nextNode()) {
const parentEl = walker.currentNode.parentNode;
if (!parentEl) continue;
const tag = parentEl.tagName.toLowerCase();
if (["script", "style", "code", "pre", "noscript", "meta", "title"].includes(tag)) continue;
const style = window.getComputedStyle(parentEl);
const ariaHidden = parentEl.getAttribute("aria-hidden") === "true";
if (style.display === "none" || style.visibility === "hidden" || ariaHidden) continue;
nodes.push(walker.currentNode);
}
return nodes;
}
function buildCombinedRegex(terms) {
const patterns = terms.map(t => escapeRegex(t.term.trim()));
return new RegExp(`(^|\\W)(${patterns.join("|")})(?=\\W|$)`, "gi");
}
function getContrastingHighlightColor(element) {
// Pick the effective text color where the highlight sits (fall back to parent colors).
let target = element || document.body;
let textColor = window.getComputedStyle(target).color;
if (!textColor || textColor === 'transparent' || textColor === 'rgba(0, 0, 0, 0)') {
let parent = target.parentElement;
while (parent && parent !== document.body) {
const pColor = window.getComputedStyle(parent).color;
if (pColor && pColor !== 'transparent' && pColor !== 'rgba(0, 0, 0, 0)') {
textColor = pColor;
break;
}
parent = parent.parentElement;
}
if (!textColor) textColor = 'rgb(0,0,0)'; // default to black
}
// Parse "rgb(...)" or hex "#..." into [r,g,b]
const parseRGB = (color) => {
if (!color) return [0,0,0];
const rgbMatch = color.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/i);
if (rgbMatch) return [parseInt(rgbMatch[1],10), parseInt(rgbMatch[2],10), parseInt(rgbMatch[3],10)];
const hexMatch = color.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i);
if (hexMatch) {
let hex = hexMatch[1];
if (hex.length === 3) hex = hex.split('').map(c=>c+c).join('');
return [parseInt(hex.substr(0,2),16), parseInt(hex.substr(2,2),16), parseInt(hex.substr(4,2),16)];
}
return [0,0,0];
};
const [r, g, b] = parseRGB(textColor);
const getLuminance = (r, g, b) => {
const [rs, gs, bs] = [r, g, b].map(c => {
c = c / 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
});
return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;
};
const textLuminance = getLuminance(r, g, b);
// YOUR REQUIREMENT:
// - If the *text* is dark (low luminance) => use yellow highlights.
// - If the *text* is light (high luminance, e.g. white text on dark bg) => use purple highlights.
if (textLuminance < 0.5) {
// dark text -> lighter warm highlight
return {
normal: '#FFDD33', // lighter gold/yellow
preview: '#FFAA33', // lighter orange, pops
focus: '#FF5533' // lighter red, strong focus
};
} else {
// light text -> darker warm highlight
return {
normal: '#CC9900', // dark gold
preview: '#FF6600', // rich orange, pops
focus: '#CC3300' // brick red, strong focus
};
}
}
function highlightMatches(nodes, regex, termMap) {
const matchesByTerm = new Map();
const termCounts = new Map();
let matchId = 0;
const exceptions = (window.hypeLessExceptions || []).map(e => e.toLowerCase());
nodes.forEach(node => {
let replaced = false;
const newHTML = node.textContent.replace(regex, (match, prefix, word, offset, fullText) => {
const term = word.toLowerCase();
const contextWindow = fullText
.slice(Math.max(0, offset - 30), offset + word.length + 30)
.toLowerCase();
if (exceptions.some(exc => contextWindow.includes(exc))) return match;
const explanation = termMap.get(term) || "";
const id = "hypeLessMatch" + matchId++;
if (!matchesByTerm.has(term)) matchesByTerm.set(term, []);
matchesByTerm.get(term).push({ id, explanation });
termCounts.set(term, (termCounts.get(term) || 0) + 1);
replaced = true;
return `${prefix}<mark id="${id}" class="hypeless-highlight" data-expl="${explanation}" data-term="${term}">${word}</mark>`;
});
if (replaced && node.parentNode) {
const wrapper = document.createElement("span");
wrapper.innerHTML = newHTML;
node.parentNode.replaceChild(wrapper, node);
// Apply dynamic highlighting colors
const highlights = wrapper.querySelectorAll('.hypeless-highlight');
highlights.forEach(highlight => {
elements.highlights.push(highlight);
const colors = getContrastingHighlightColor(highlight);
highlight.style.setProperty('--highlight-normal', colors.normal);
highlight.style.setProperty('--highlight-preview', colors.preview);
highlight.style.setProperty('--highlight-focus', colors.focus);
});
}
});
return { matchesByTerm, termCounts };
}
function buildSidebar(termCounts, matchesByTerm) {
const sidebar = document.createElement("div");
sidebar.id = "hypeless-sidebar";
const itemsHTML = [...termCounts.entries()]
.filter(([_, count]) => count > 0)
.map(([term, count]) => {
const explanation = matchesByTerm.get(term)?.[0]?.explanation || "";
return `
<div class="hypeless-item" data-term="${term}">
<b>${term}</b> (${count})<br>
<small>${explanation}</small>
</div>
`;
})
.join("");
const totalCount = [...termCounts.values()].reduce((a, b) => a + b, 0);
sidebar.innerHTML = `
<div id="hypeless-header">
<span>HypeLessLi v3.1 (${totalCount} found)</span>
<div>
<button id="hypeless-help">Info</button>
<button id="hypeless-toggle">Hide</button>
</div>
</div>
<div id="hypeless-content">${itemsHTML}</div>
<div id="hypeless-help-popup">
<b>HypeLessLi Help</b><br><br>
- Highlights hype/buzz words in text.<br>
- Hover a highlighted word to see explanation.<br>
- Click a term in sidebar to jump to it.<br>
- Resize sidebar by dragging its edge.<br>
- Toggle with extension popup or floating button.<br>
</div>
`;
document.body.appendChild(sidebar);
// Add custom visible resizer handle
const resizer = document.createElement("div");
resizer.id = "hypeless-resizer";
sidebar.appendChild(resizer);
return sidebar;
}
function buildFloatButton() {
const btn = document.createElement("button");
btn.id = "hypeless-float-btn";
btn.innerHTML = "📝";
btn.title = "Show HypeLessLi sidebar";
document.body.appendChild(btn);
return btn;
}
function buildTooltip() {
const tooltip = document.createElement("div");
tooltip.id = "hypeless-tooltip";
document.body.appendChild(tooltip);
return tooltip;
}
function setupInteractions(sidebar, floatBtn, matchesByTerm, tooltip) {
const termPositions = new Map();
for (const term of matchesByTerm.keys()) termPositions.set(term, 0);
sidebar.addEventListener("click", e => {
const item = e.target.closest(".hypeless-item");
if (!item) return;
const term = item.dataset.term;
const termMatches = matchesByTerm.get(term) || [];
if (termMatches.length === 0) return;
let index = termPositions.get(term) % termMatches.length;
const { id } = termMatches[index];
const el = document.getElementById(id);
if (el) {
el.scrollIntoView({ behavior: "smooth", block: "center" });
el.classList.add("hypeless-focus");
setTimeout(() => el.classList.remove("hypeless-focus"), 2000);
}
termPositions.set(term, (index + 1) % termMatches.length);
});
sidebar.addEventListener("mouseover", e => {
const item = e.target.closest(".hypeless-item");
if (!item) return;
const term = item.dataset.term;
(matchesByTerm.get(term) || []).forEach(m => {
document.getElementById(m.id)?.classList.add("hypeless-preview");
});
});
sidebar.addEventListener("mouseout", e => {
const item = e.target.closest(".hypeless-item");
if (!item) return;
const term = item.dataset.term;
(matchesByTerm.get(term) || []).forEach(m => {
document.getElementById(m.id)?.classList.remove("hypeless-preview");
});
});
const toggleSidebar = () => {
sidebar.classList.toggle("collapsed");
isSidebarVisible = !sidebar.classList.contains("collapsed");
floatBtn.style.display = isSidebarVisible ? "none" : "block";
floatBtn.title = isSidebarVisible ? "Hide HypeLessLi sidebar" : "Show HypeLessLi sidebar";
};
document.getElementById("hypeless-toggle").addEventListener("click", toggleSidebar);
floatBtn.addEventListener("click", toggleSidebar);
document.getElementById("hypeless-help").addEventListener("click", () => {
const popup = document.getElementById("hypeless-help-popup");
popup.classList.toggle("visible");
});
document.body.addEventListener("mouseover", e => {
if (e.target.classList.contains("hypeless-highlight")) {
const expl = e.target.getAttribute("data-expl");
if (expl) {
tooltip.innerText = expl;
tooltip.style.display = "block";
}
}
});
document.body.addEventListener("mousemove", e => {
if (tooltip.style.display === "block") {
tooltip.style.left = e.pageX + 12 + "px";
tooltip.style.top = e.pageY + 12 + "px";
}
});
document.body.addEventListener("mouseout", e => {
if (e.target.classList.contains("hypeless-highlight")) {
tooltip.style.display = "none";
}
});
// Custom sidebar resizing logic
const resizerEl = document.getElementById("hypeless-resizer");
if (resizerEl) {
const css = getComputedStyle(sidebar);
const minW = parseInt(css.minWidth, 10) || 180;
const maxW = parseInt(css.maxWidth, 10) || 500;
let startX = 0;
let startW = 0;
const onPointerMove = (e) => {
const dx = startX - e.clientX;
let newW = Math.min(maxW, Math.max(minW, startW + dx));
sidebar.style.width = newW + "px";
};
const onPointerUp = () => {
sidebar.classList.remove("resizing");
document.removeEventListener("pointermove", onPointerMove);
document.removeEventListener("pointerup", onPointerUp);
};
const onPointerDown = (e) => {
if (sidebar.classList.contains("collapsed")) return;
startX = e.clientX;
startW = sidebar.offsetWidth;
sidebar.classList.add("resizing");
document.addEventListener("pointermove", onPointerMove);
document.addEventListener("pointerup", onPointerUp);
};
resizerEl.addEventListener("pointerdown", onPointerDown);
}
}
function toggleExtension(enabled) {
isEnabled = enabled;
if (enabled) {
// Show all highlights
elements.highlights.forEach(highlight => {
highlight.style.display = '';
});
// Show sidebar and button if they exist
if (elements.sidebar) {
elements.sidebar.style.display = 'flex';
}
if (elements.floatBtn) {
elements.floatBtn.style.display = isSidebarVisible ? 'none' : 'block';
}
} else {
// Hide all highlights
elements.highlights.forEach(highlight => {
highlight.style.display = 'none';
});
// Hide sidebar and button
if (elements.sidebar) {
elements.sidebar.style.display = 'none';
}
if (elements.floatBtn) {
elements.floatBtn.style.display = 'none';
}
}
if (elements.tooltip) {
elements.tooltip.style.display = 'none';
}
}
// Listen for messages from background script
if (chrome.runtime && chrome.runtime.onMessage) {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'stateChanged') {
// No response needed
toggleExtension(message.enabled);
} else if (message.type === 'toggleSidebar') {
// Only act if UI exists; otherwise ignore silently
if (elements.sidebar && elements.floatBtn) {
const isCollapsed = elements.sidebar.classList.contains('collapsed');
if (isCollapsed) {
elements.sidebar.classList.remove('collapsed');
elements.floatBtn.style.display = 'none';
isSidebarVisible = true;
} else {
elements.sidebar.classList.add('collapsed');
elements.floatBtn.style.display = 'block';
isSidebarVisible = false;
}
}
}
// IMPORTANT: Do NOT return true unless you will call sendResponse later.
// We are not sending any response from the content script, so we simply return undefined.
});
}
})();