-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.js
More file actions
338 lines (296 loc) · 8.95 KB
/
inject.js
File metadata and controls
338 lines (296 loc) · 8.95 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
// Wrap entire script to allow early exit if context is invalidated
(function () {
'use strict';
// Early check: If extension context is invalidated, exit silently
try {
if (!chrome?.runtime?.id) return;
} catch {
// Extension context is invalidated, exit silently
return;
}
// Check if extension context is still valid
function isContextValid() {
try {
// Accessing chrome.runtime.id will throw if context is invalid
if (!chrome?.runtime?.id) return false;
return true;
} catch {
return false;
}
}
// Inject theme CSS into page
function injectTheme() {
if (!isContextValid()) return;
try {
chrome.storage.sync.get(['extensionEnabled', 'activeTheme', 'customOverrides'], (data) => {
try {
applyThemeData(data || {});
} catch (err) {
// Silently fail if context is invalid
}
});
} catch (err) {
// Context might be invalidated, skip silently
}
}
// Apply theme data to the page
function applyThemeData(data) {
const extensionEnabled = data.extensionEnabled !== false;
if (!extensionEnabled) {
const existingStyle = document.getElementById('xtheme-inject');
if (existingStyle) {
existingStyle.remove();
}
return;
}
const activeTheme = data.activeTheme || 'darkDefault';
const customOverrides = data.customOverrides || {};
const themes = getThemes();
const theme = themes[activeTheme] || themes.darkDefault;
const colors = { ...theme.colors, ...customOverrides };
let existingStyle = document.getElementById('xtheme-inject');
if (!existingStyle) {
existingStyle = document.createElement('style');
existingStyle.id = 'xtheme-inject';
existingStyle.setAttribute('data-source', 'xtheme-engine');
}
const css = buildThemeCSS(colors);
existingStyle.textContent = css;
if (!existingStyle.parentElement) {
if (document.head) {
document.head.appendChild(existingStyle);
} else {
document.addEventListener('readystatechange', () => {
if (document.head && !existingStyle.parentElement) {
document.head.appendChild(existingStyle);
}
});
}
}
// Style Post buttons after theme is applied
stylePostButtons();
}
// Watch for route changes (React navigation) - only observe when necessary
let mutationObserver = null;
let debounceTimer = null;
function setupMutationObserver() {
if (mutationObserver) return; // Already set up
mutationObserver = new MutationObserver(() => {
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
injectTheme();
stylePostButtons(); // Re-style Post buttons when DOM changes
}, 100);
});
mutationObserver.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
}
function cleanupMutationObserver() {
if (mutationObserver) {
mutationObserver.disconnect();
mutationObserver = null;
}
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = null;
}
}
// Get all theme definitions
function getThemes() {
return {
darkDefault: {
name: 'Dark Default',
colors: {
bodyBg: 'rgb(15, 23, 42)',
cardBg: 'rgb(30, 41, 59)',
overlayBg: 'rgb(18, 27, 35 / 65%)',
borderColor: 'rgb(51, 65, 85)',
textPrimary: 'rgb(241, 245, 249)',
accentBlue: 'rgb(59, 130, 246)'
}
},
midnightBlue: {
name: 'Midnight Blue',
colors: {
bodyBg: 'rgb(10, 20, 40)',
cardBg: 'rgb(20, 35, 50)',
overlayBg: 'rgb(10, 15, 25 / 70%)',
borderColor: 'rgb(40, 60, 100)',
textPrimary: 'rgb(200, 220, 255)',
accentBlue: 'rgb(100, 150, 255)'
}
},
deepPurple: {
name: 'Deep Purple',
colors: {
bodyBg: 'rgb(25, 15, 40)',
cardBg: 'rgb(40, 25, 60)',
overlayBg: 'rgb(20, 10, 35 / 70%)',
borderColor: 'rgb(60, 40, 100)',
textPrimary: 'rgb(230, 200, 255)',
accentBlue: 'rgb(180, 100, 255)'
}
},
oceanCyan: {
name: 'Ocean Cyan',
colors: {
bodyBg: 'rgb(8, 30, 45)',
cardBg: 'rgb(15, 50, 75)',
overlayBg: 'rgb(10, 35, 55 / 70%)',
borderColor: 'rgb(30, 80, 120)',
textPrimary: 'rgb(180, 240, 255)',
accentBlue: 'rgb(0, 200, 255)'
}
},
amberfireSoft: {
name: 'Amber Fire',
colors: {
bodyBg: 'rgb(45, 30, 15)',
cardBg: 'rgb(65, 45, 25)',
overlayBg: 'rgb(50, 35, 20 / 70%)',
borderColor: 'rgb(100, 75, 40)',
textPrimary: 'rgb(255, 235, 180)',
accentBlue: 'rgb(255, 165, 0)'
}
},
monochrome: {
name: 'Monochrome',
colors: {
bodyBg: 'rgb(20, 20, 20)',
cardBg: 'rgb(35, 35, 35)',
overlayBg: 'rgb(25, 25, 25 / 70%)',
borderColor: 'rgb(60, 60, 60)',
textPrimary: 'rgb(220, 220, 220)',
accentBlue: 'rgb(180, 180, 180)'
}
}
};
}
// Build CSS overrides from theme colors
function buildThemeCSS(colors) {
return `
/* XTheme Engine - Color Overrides */
/* Body background (inline style override) */
body {
background-color: ${colors.bodyBg} !important;
}
/* Card/section backgrounds - .r-kemksi maps to background-color rgb(20 35 50) */
.r-kemksi {
background-color: ${colors.cardBg} !important;
}
/* Overlay backgrounds (modals, tooltips) - .r-5zmot maps to rgb(18 27 35 / 65%) */
.r-5zmot {
background-color: ${colors.overlayBg} !important;
}
/* Border colors - .r-1kqtdi0 maps to border-color rgb(47, 51, 54) */
.r-1kqtdi0 {
border-color: ${colors.borderColor} !important;
}
/* Text primary - .css-1jxf684 maps to color text */
.css-1jxf684 {
color: ${colors.textPrimary} !important;
}
/* Accent icons - .r-yyyyoo maps to fill for SVG icons */
.r-yyyyoo {
fill: ${colors.accentBlue} !important;
}
/* Accent blue accent colors */
a {
color: ${colors.accentBlue} !important;
}
.r-kemksi {
border-radius: 16px;
}
/* Post button specific styles */
.xtheme-post-button {
color: ${colors.textPrimary} !important;
background-color: ${colors.accentBlue} !important;
}
.xtheme-post-button span,
.xtheme-post-button div {
color: ${colors.textPrimary} !important;
}
/* Reply button specific styles */
.xtheme-reply-button {
color: ${colors.textPrimary} !important;
background-color: ${colors.accentBlue} !important;
}
.xtheme-reply-button span,
.xtheme-reply-button div {
color: ${colors.textPrimary} !important;
}
/* Follow button specific styles */
.xtheme-follow-button {
color: ${colors.textPrimary} !important;
background-color: ${colors.accentBlue} !important;
}
.xtheme-follow-button span,
.xtheme-follow-button div {
color: ${colors.textPrimary} !important;
}
`;
}
// Style Post buttons specifically by finding them by text content or aria-label
function stylePostButtons() {
if (!isContextValid()) return;
// Find all buttons and button-like elements (including links)
const allButtons = document.querySelectorAll('button, div[role="button"], a[role="button"], a[role="link"], a[aria-label="Post"]');
allButtons.forEach(button => {
// Check if button contains the text "Post" (case-insensitive)
const buttonText = button.textContent.trim();
const ariaLabel = button.getAttribute('aria-label');
if (buttonText === 'Post' || buttonText === 'POST' || ariaLabel === 'Post') {
// Add our custom class for styling
button.classList.add('xtheme-post-button');
} else if (buttonText === 'Reply' || buttonText === 'REPLY' || ariaLabel === 'Reply') {
// Add our custom class for styling Reply buttons
button.classList.add('xtheme-reply-button');
} else if (buttonText === 'Follow' || buttonText === 'FOLLOW' || (ariaLabel && ariaLabel.startsWith('Follow '))) {
// Add our custom class for styling Follow buttons
button.classList.add('xtheme-follow-button');
}
});
}
// Start injection when DOM is ready or immediately
// Wrap everything in a try-catch to handle context invalidation gracefully
(function initXTheme() {
try {
// Early exit if chrome APIs are not available
if (!chrome?.runtime?.id) return;
// Set up listener ONCE at the top level (only if context is valid)
if (isContextValid()) {
try {
chrome.storage.onChanged.addListener((changes, areaName) => {
if (!isContextValid()) return;
try {
if ((areaName === 'sync' || areaName === 'local') &&
(changes.extensionEnabled || changes.activeTheme || changes.customOverrides)) {
injectTheme();
}
} catch (err) {
// Silently ignore errors (context invalidation, etc)
}
});
} catch (err) {
// Failed to set up listener - context may be invalid
}
}
// Initial injection
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
injectTheme();
setupMutationObserver();
});
} else {
injectTheme();
setupMutationObserver();
}
} catch (err) {
// Extension context invalidated or other error - fail silently
}
})();
})(); // End of main wrapper IIFE