-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
187 lines (161 loc) · 6.43 KB
/
content.js
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
let usernameColors = new Map();
let usernameBatch = new Map();
let isEnabled = localStorage.getItem('highlighterState') === 'on'; // Default state based on saved state
// Listen for toggle action from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "toggle") {
isEnabled = !isEnabled;
// Save the new state to localStorage
localStorage.setItem('highlighterState', isEnabled ? 'on' : 'off');
// Update the button's state in the UI
updateButtonState();
if (isEnabled) {
highlightUsernames();
} else {
clearHighlights();
}
sendResponse({ enabled: isEnabled });
}
});
// Update button UI to reflect the highlighter state
function updateButtonState() {
const button = document.getElementById('highlighter-toggle'); // Assuming you have a button with this ID
if (!button) return;
if (isEnabled) {
// Example: change button text or add a class to indicate it's "on"
button.innerText = 'Disable Highlighter'; // Text change
button.classList.add('enabled'); // Add "enabled" class for styles
button.classList.remove('disabled'); // Remove "disabled" class if exists
} else {
button.innerText = 'Enable Highlighter'; // Text change
button.classList.add('disabled'); // Add "disabled" class for styles
button.classList.remove('enabled'); // Remove "enabled" class if exists
}
}
// Load usernames and GIFs from external JSON file
fetch(chrome.runtime.getURL("usernames.json"))
.then(response => response.json())
.then(data => {
for (const [batchName, batchData] of Object.entries(data)) {
batchData.usernames.forEach(username => {
usernameColors.set("@" + username, {
color: batchData.color,
icon: batchData.gif ? chrome.runtime.getURL(batchData.gif) : null // Convert local path
});
usernameBatch.set("@" + username, batchName);
});
}
if (isEnabled) highlightUsernames();
updateButtonState(); // Make sure the button reflects the correct state on page load
})
.catch(error => console.error("Failed to load usernames.json:", error));
// Function to highlight usernames and add GIFs
function highlightUsernames() {
if (!isEnabled) return;
document.querySelectorAll('div[dir="ltr"] span, a[dir="ltr"]').forEach(el => {
const username = el.innerText.trim();
if (username.startsWith("@") && !el.dataset.highlighted) {
if (usernameColors.has(username)) {
const { color, icon } = usernameColors.get(username);
// Apply color and bold style
el.style.color = color;
el.style.fontWeight = "bold";
el.dataset.highlighted = "true";
// Ensure we only insert the GIF for profile usernames and post maker usernames
const isProfileUsername = el.closest('div[data-testid="UserName"]');
const isPostMakerUsername = el.closest('article')?.querySelector('div[dir="ltr"] span') === el;
if (icon && (isProfileUsername || isPostMakerUsername) && !el.nextSibling?.classList?.contains("username-gif")) {
const gifImage = document.createElement("img");
gifImage.src = icon;
gifImage.alt = "GIF";
gifImage.classList.add("username-gif");
gifImage.style.width = "40px";
gifImage.style.height = "30px";
gifImage.style.marginLeft = "5px";
gifImage.style.verticalAlign = "middle";
// Insert the GIF immediately after the username
el.parentNode.insertBefore(gifImage, el.nextSibling);
}
// Tooltip event handlers
const mouseOverHandler = () => showBatchName(el, username);
const mouseOutHandler = hideBatchName;
el._mouseOverHandler = mouseOverHandler;
el._mouseOutHandler = mouseOutHandler;
el.addEventListener("mouseover", mouseOverHandler);
el.addEventListener("mouseout", mouseOutHandler);
} else {
// Clear styles if username is not listed
el.style.color = "";
el.style.fontWeight = "";
// Remove associated GIF if present
const nextSibling = el.nextSibling;
if (nextSibling && nextSibling.classList.contains("username-gif")) {
nextSibling.remove();
}
}
}
});
}
// Function to remove highlights and GIFs
function clearHighlights() {
document.querySelectorAll('[data-highlighted="true"]').forEach(el => {
el.style.color = "";
el.style.fontWeight = "";
el.removeAttribute("data-highlighted");
// Remove associated GIF
const nextSibling = el.nextSibling;
if (nextSibling && nextSibling.classList.contains("username-gif")) {
nextSibling.remove();
}
// Remove event listeners for tooltips
if (el._mouseOverHandler) {
el.removeEventListener("mouseover", el._mouseOverHandler);
delete el._mouseOverHandler;
}
if (el._mouseOutHandler) {
el.removeEventListener("mouseout", el._mouseOutHandler);
delete el._mouseOutHandler;
}
});
}
// Function to show tooltip with batch name
function showBatchName(element, username) {
const batchName = usernameBatch.get(username);
if (!batchName) return;
let tooltip = document.getElementById("batch-tooltip");
if (!tooltip) {
tooltip = document.createElement("div");
tooltip.id = "batch-tooltip";
tooltip.style.position = "absolute";
tooltip.style.backgroundColor = "#0c0200";
tooltip.style.border = "1px solid #ccc";
tooltip.style.padding = "5px";
tooltip.style.borderRadius = "5px";
tooltip.style.boxShadow = "0 2px 10px rgba(248, 162, 162, 0.1)";
tooltip.style.fontSize = "12px";
tooltip.style.display = "none";
tooltip.style.color = "white";
document.body.appendChild(tooltip);
}
tooltip.innerHTML = `<strong>${batchName}</strong>`;
const rect = element.getBoundingClientRect();
tooltip.style.top = `${rect.top + window.scrollY + rect.height + 5}px`;
tooltip.style.left = `${rect.left + window.scrollX}px`;
tooltip.style.display = "block";
}
// Function to hide tooltip
function hideBatchName() {
const tooltip = document.getElementById("batch-tooltip");
if (tooltip) {
tooltip.style.display = "none";
}
}
// Mutation observer for dynamic content
const observer = new MutationObserver(() => {
clearTimeout(window.highlightTimeout);
window.highlightTimeout = setTimeout(() => {
clearHighlights();
highlightUsernames();
}, 300);
});
observer.observe(document.body, { childList: true, subtree: true });