-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
750 lines (641 loc) · 23.5 KB
/
content.js
File metadata and controls
750 lines (641 loc) · 23.5 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/**
* Element Modifier Pro - Content Script
* Modifica elementos HTML baseado em configurações salvas no chrome.storage.sync
* Suporte aprimorado para iframes, IDs duplicados e elementos dinâmicos
*/
(() => {
'use strict';
// ============================================
// ESTADO E CONFIGURAÇÕES
// ============================================
let CONFIG = {
rules: [],
globalEnabled: true,
debounceDelay: 50,
debug: false
};
// Usar Map para rastrear elementos processados por regra
const processedMap = new WeakMap();
let observer = null;
let isInitialized = false;
// Identificador único para este contexto (útil para debug em iframes)
const contextId = Math.random().toString(36).substring(2, 8);
const isIframe = window !== window.top;
// ============================================
// UTILITÁRIOS
// ============================================
function log(...args) {
if (CONFIG.debug) {
const prefix = isIframe ? `[EMP:${contextId}:iframe]` : `[EMP:${contextId}:main]`;
console.log(prefix, ...args);
}
}
function warn(...args) {
const prefix = isIframe ? `[EMP:${contextId}:iframe]` : `[EMP:${contextId}:main]`;
console.warn(prefix, ...args);
}
function getCurrentHost() {
return window.location.hostname;
}
function matchesUrlPattern(pattern, url) {
if (!pattern || pattern === '*' || pattern === '<all_urls>') {
return true;
}
try {
// Converte padrão simples para regex
const regexPattern = pattern
.replace(/[.+?^${}()|[\]\\]/g, '\\$&') // Escapa caracteres especiais
.replace(/\*/g, '.*'); // Converte * para .*
const regex = new RegExp(`^${regexPattern}$`, 'i');
return regex.test(url) || regex.test(getCurrentHost());
} catch (e) {
warn('Padrão de URL inválido:', pattern);
return false;
}
}
function getApplicableRules() {
const currentUrl = window.location.href;
const currentHost = getCurrentHost();
return CONFIG.rules.filter(rule => {
if (!rule.enabled) return false;
if (!rule.urlPattern) return true;
return matchesUrlPattern(rule.urlPattern, currentUrl) ||
matchesUrlPattern(rule.urlPattern, currentHost);
});
}
/**
* Verifica se um elemento já foi processado para uma regra específica
*/
function wasProcessed(element, ruleId) {
const processed = processedMap.get(element);
return processed && processed.has(ruleId);
}
/**
* Marca elemento como processado para uma regra
*/
function markProcessed(element, ruleId) {
let processed = processedMap.get(element);
if (!processed) {
processed = new Set();
processedMap.set(element, processed);
}
processed.add(ruleId);
}
/**
* Busca elementos de forma robusta, lidando com IDs duplicados
*/
function findElements(selector, root = document) {
const elements = [];
try {
// Tenta querySelectorAll normal primeiro
const found = root.querySelectorAll(selector);
found.forEach(el => elements.push(el));
// Se é um seletor de ID e encontrou apenas 1, verifica se há duplicados
if (selector.startsWith('#') && !selector.includes(' ') && !selector.includes('.')) {
const id = selector.substring(1);
// Busca por atributo para encontrar IDs duplicados
const byAttribute = root.querySelectorAll(`[id="${id}"]`);
byAttribute.forEach(el => {
if (!elements.includes(el)) {
elements.push(el);
}
});
}
} catch (e) {
warn(`Seletor inválido: "${selector}"`, e.message);
}
return elements;
}
// ============================================
// APLICAÇÃO DE MODIFICAÇÕES
// ============================================
function applyModifications(element, rule) {
// Verifica se já foi processado para esta regra
if (wasProcessed(element, rule.id)) {
return false;
}
try {
let modified = false;
const changes = []; // Para debug detalhado
// Modificar atributos
if (rule.attributes && typeof rule.attributes === 'object' && Object.keys(rule.attributes).length > 0) {
for (const [attr, value] of Object.entries(rule.attributes)) {
if (value === null || value === '') {
if (element.hasAttribute(attr)) {
element.removeAttribute(attr);
changes.push(`attr:${attr}=REMOVED`);
modified = true;
}
} else {
const currentValue = element.getAttribute(attr);
if (currentValue !== String(value)) {
element.setAttribute(attr, String(value));
changes.push(`attr:${attr}="${value}"`);
modified = true;
}
}
}
}
// Modificar estilos
if (rule.styles && typeof rule.styles === 'object' && Object.keys(rule.styles).length > 0) {
for (const [prop, value] of Object.entries(rule.styles)) {
if (!prop || prop.trim() === '') continue;
// Converte camelCase para kebab-case se necessário
let cssProp = prop.trim();
if (cssProp.match(/[A-Z]/)) {
cssProp = cssProp.replace(/([A-Z])/g, '-$1').toLowerCase();
}
if (value === null || value === '') {
element.style.removeProperty(cssProp);
changes.push(`style:${cssProp}=REMOVED`);
modified = true;
} else {
// Aplica com !important para garantir que sobrescreva CSS externo
const valueStr = String(value).trim();
const hasImportant = valueStr.toLowerCase().includes('!important');
const finalValue = hasImportant ? valueStr : valueStr;
const priority = hasImportant ? '' : 'important';
try {
element.style.setProperty(cssProp, finalValue.replace(/!important/gi, '').trim(), priority);
changes.push(`style:${cssProp}="${finalValue}" !important`);
modified = true;
// Verificação imediata
const appliedValue = element.style.getPropertyValue(cssProp);
if (!appliedValue) {
warn(`⚠️ Estilo não foi aplicado: ${cssProp}. Verificar se a propriedade é válida.`);
}
} catch (styleError) {
warn(`Erro ao aplicar estilo ${cssProp}:`, styleError.message);
}
}
}
}
// Modificar propriedades DOM
if (rule.properties && typeof rule.properties === 'object' && Object.keys(rule.properties).length > 0) {
for (const [prop, value] of Object.entries(rule.properties)) {
try {
if (element[prop] !== value) {
element[prop] = value;
changes.push(`prop:${prop}=${value}`);
modified = true;
}
} catch (e) {
warn(`Não foi possível definir propriedade "${prop}":`, e.message);
}
}
}
// Adicionar classes
if (rule.addClass && rule.addClass.length > 0) {
const classes = Array.isArray(rule.addClass) ? rule.addClass : [rule.addClass];
classes.forEach(cls => {
if (cls && typeof cls === 'string' && cls.trim() && !element.classList.contains(cls.trim())) {
element.classList.add(cls.trim());
changes.push(`class:+${cls.trim()}`);
modified = true;
}
});
}
// Remover classes
if (rule.removeClass && rule.removeClass.length > 0) {
const classes = Array.isArray(rule.removeClass) ? rule.removeClass : [rule.removeClass];
classes.forEach(cls => {
if (cls && typeof cls === 'string' && cls.trim() && element.classList.contains(cls.trim())) {
element.classList.remove(cls.trim());
changes.push(`class:-${cls.trim()}`);
modified = true;
}
});
}
// Modificar texto interno (cuidado: substitui todo o conteúdo)
if (rule.textContent !== undefined && rule.textContent !== null) {
if (element.textContent !== String(rule.textContent)) {
element.textContent = String(rule.textContent);
changes.push(`text:"${String(rule.textContent).substring(0, 20)}..."`);
modified = true;
}
}
// Marca como processado para esta regra
markProcessed(element, rule.id);
// Log detalhado
if (CONFIG.debug) {
const elementDesc = element.tagName +
(element.id ? '#' + element.id : '') +
(element.className && typeof element.className === 'string' ? '.' + element.className.split(' ')[0] : '');
if (changes.length > 0) {
log(`✓ MODIFICADO [${rule.name}]`, {
selector: rule.selector,
element: elementDesc,
changes: changes,
styleAttr: element.getAttribute('style') || '(vazio)'
});
} else {
log(`○ SEM MUDANÇAS [${rule.name}]`, {
selector: rule.selector,
element: elementDesc,
reason: 'Nenhum estilo/atributo configurado ou valores já aplicados',
ruleStyles: rule.styles,
ruleAttributes: rule.attributes
});
}
}
return modified;
} catch (error) {
warn('Erro ao modificar elemento:', error.message, element);
return false;
}
}
function processElements(root = document) {
if (!CONFIG.globalEnabled) return { total: 0, modified: 0 };
const rules = getApplicableRules();
let totalFound = 0;
let totalModified = 0;
for (const rule of rules) {
const elements = findElements(rule.selector, root);
totalFound += elements.length;
elements.forEach(el => {
if (applyModifications(el, rule)) {
totalModified++;
}
});
if (CONFIG.debug && elements.length > 0) {
log(`Regra "${rule.name}" (${rule.selector}): ${elements.length} elemento(s) encontrado(s)`);
}
}
return { total: totalFound, modified: totalModified };
}
function processNode(node) {
if (!CONFIG.globalEnabled) return;
if (node.nodeType !== Node.ELEMENT_NODE) return;
const rules = getApplicableRules();
for (const rule of rules) {
try {
// Verifica se o próprio nó corresponde ao seletor
if (node.matches && node.matches(rule.selector)) {
applyModifications(node, rule);
}
// Também verifica por ID se for um seletor de ID
if (rule.selector.startsWith('#') && !rule.selector.includes(' ')) {
const id = rule.selector.substring(1);
if (node.id === id) {
applyModifications(node, rule);
}
}
} catch (e) {
// matches() pode falhar com seletores inválidos
}
}
// Processa filhos do nó
processElements(node);
}
// ============================================
// MUTATION OBSERVER
// ============================================
function setupMutationObserver() {
if (observer) {
observer.disconnect();
}
let timeoutId = null;
let pendingNodes = [];
observer = new MutationObserver((mutations) => {
if (!CONFIG.globalEnabled) return;
// Coleta nós adicionados
for (const mutation of mutations) {
if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
pendingNodes.push(node);
}
}
}
}
// Debounce: processa todos de uma vez
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
const nodesToProcess = [...pendingNodes];
pendingNodes = [];
timeoutId = null;
nodesToProcess.forEach(processNode);
}, CONFIG.debounceDelay);
});
if (document.body) {
observer.observe(document.body, {
childList: true,
subtree: true
});
log('MutationObserver ativado');
}
return observer;
}
// ============================================
// STORAGE E SINCRONIZAÇÃO
// ============================================
async function loadConfig() {
return new Promise((resolve) => {
chrome.storage.sync.get(['rules', 'globalEnabled', 'debug'], (data) => {
if (chrome.runtime.lastError) {
warn('Erro ao carregar configurações:', chrome.runtime.lastError);
resolve(getDefaultConfig());
return;
}
CONFIG.rules = data.rules || [];
CONFIG.globalEnabled = data.globalEnabled !== false;
CONFIG.debug = data.debug || false;
// Log detalhado das regras carregadas
if (CONFIG.debug) {
log('=== CONFIGURAÇÕES CARREGADAS ===');
log('Global Enabled:', CONFIG.globalEnabled);
log('Total de regras:', CONFIG.rules.length);
CONFIG.rules.forEach((rule, i) => {
log(`Regra ${i + 1}: "${rule.name}"`, {
id: rule.id,
selector: rule.selector,
urlPattern: rule.urlPattern,
enabled: rule.enabled,
styles: rule.styles || '(nenhum)',
attributes: rule.attributes || '(nenhum)',
addClass: rule.addClass || '(nenhum)',
removeClass: rule.removeClass || '(nenhum)'
});
});
log('================================');
}
resolve(CONFIG);
});
});
}
function getDefaultConfig() {
return {
rules: [],
globalEnabled: true,
debug: false
};
}
function setupStorageListener() {
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== 'sync') return;
log('Configurações alteradas:', changes);
if (changes.rules) {
const oldRules = changes.rules.oldValue || [];
const newRules = changes.rules.newValue || [];
// Detecta regras que foram modificadas (mesmo ID, mas conteúdo diferente)
const modifiedRuleIds = newRules
.filter(newRule => {
const oldRule = oldRules.find(r => r.id === newRule.id);
if (!oldRule) return false; // É nova, não modificada
// Compara se houve mudança
return JSON.stringify(oldRule) !== JSON.stringify(newRule);
})
.map(r => r.id);
// Remove estilos das regras modificadas antes de reaplicar
if (modifiedRuleIds.length > 0) {
log('Regras modificadas detectadas:', modifiedRuleIds);
removeStylesFromRules(oldRules.filter(r => modifiedRuleIds.includes(r.id)));
}
CONFIG.rules = newRules;
reprocessAllElements();
}
if (changes.globalEnabled !== undefined) {
CONFIG.globalEnabled = changes.globalEnabled.newValue !== false;
if (CONFIG.globalEnabled) {
reprocessAllElements();
}
}
if (changes.debug !== undefined) {
CONFIG.debug = changes.debug.newValue || false;
}
});
}
/**
* Remove estilos aplicados por regras específicas
*/
function removeStylesFromRules(rules) {
for (const rule of rules) {
if (!rule || !rule.selector) continue;
try {
const elements = findElements(rule.selector);
elements.forEach(element => {
// Remove estilos que foram aplicados por esta regra
if (rule.styles && typeof rule.styles === 'object') {
for (const prop of Object.keys(rule.styles)) {
let cssProp = prop.trim();
if (cssProp.match(/[A-Z]/)) {
cssProp = cssProp.replace(/([A-Z])/g, '-$1').toLowerCase();
}
element.style.removeProperty(cssProp);
log(`Removido estilo: ${cssProp} de`, element);
}
}
// Remove classes adicionadas
if (rule.addClass) {
const classes = Array.isArray(rule.addClass) ? rule.addClass : [rule.addClass];
classes.forEach(cls => {
if (cls && element.classList.contains(cls.trim())) {
element.classList.remove(cls.trim());
}
});
}
// Restaura classes removidas (adiciona de volta)
if (rule.removeClass) {
const classes = Array.isArray(rule.removeClass) ? rule.removeClass : [rule.removeClass];
classes.forEach(cls => {
if (cls && !element.classList.contains(cls.trim())) {
element.classList.add(cls.trim());
}
});
}
// Limpa a marcação de processamento para esta regra
const processed = processedMap.get(element);
if (processed) {
processed.delete(rule.id);
}
});
} catch (e) {
warn(`Erro ao remover estilos da regra "${rule.name}":`, e.message);
}
}
}
function reprocessAllElements() {
log('Reprocessando todos os elementos...');
// IMPORTANTE: Limpa todas as marcações de processamento
// Isso permite que os elementos sejam processados novamente
// O WeakMap será limpo implicitamente, mas precisamos garantir
// que os elementos sejam remarcados
// Busca todos os elementos que podem ter sido modificados e limpa suas marcações
const rules = getApplicableRules();
for (const rule of rules) {
try {
const elements = findElements(rule.selector);
elements.forEach(el => {
const processed = processedMap.get(el);
if (processed) {
processed.delete(rule.id);
}
});
} catch (e) {
// Ignora erros de seletores inválidos
}
}
const result = processElements();
log(`Reprocessamento concluído: ${result.total} encontrados, ${result.modified} modificados`);
return result;
}
// ============================================
// COMUNICAÇÃO COM POPUP/BACKGROUND
// ============================================
function setupMessageListener() {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
log('Mensagem recebida:', message.action);
switch (message.action) {
case 'ping':
sendResponse({
status: 'ok',
url: window.location.href,
isIframe: isIframe,
contextId: contextId
});
break;
case 'reprocess':
const result = reprocessAllElements();
sendResponse({
status: 'ok',
...result
});
break;
case 'getStatus':
const rules = getApplicableRules();
sendResponse({
status: 'ok',
enabled: CONFIG.globalEnabled,
rulesCount: CONFIG.rules.length,
applicableRules: rules.length,
url: window.location.href,
isIframe: isIframe
});
break;
case 'testSelector':
try {
// Busca elementos usando nossa função robusta
const elements = findElements(message.selector);
// Também verifica iframes se estiver no documento principal
let iframeCount = 0;
if (!isIframe) {
try {
const iframes = document.querySelectorAll('iframe');
iframes.forEach(iframe => {
try {
if (iframe.contentDocument) {
const iframeElements = findElements(message.selector, iframe.contentDocument);
iframeCount += iframeElements.length;
}
} catch (e) {
// Cross-origin iframe, não podemos acessar
}
});
} catch (e) {
// Ignorar erros de iframe
}
}
const totalCount = elements.length + iframeCount;
sendResponse({
status: 'ok',
count: totalCount,
mainDocument: elements.length,
iframes: iframeCount,
preview: elements.slice(0, 5).map(el => ({
tag: el.tagName.toLowerCase(),
id: el.id || null,
classes: Array.from(el.classList).slice(0, 3).join(' ') || null,
text: (el.textContent || '').substring(0, 30).trim() || null
}))
});
} catch (e) {
sendResponse({ status: 'error', message: e.message });
}
break;
case 'getDebugInfo':
const applicableRules = getApplicableRules();
const debugInfo = {
status: 'ok',
contextId: contextId,
isIframe: isIframe,
url: window.location.href,
hostname: window.location.hostname,
rulesTotal: CONFIG.rules.length,
rulesApplicable: applicableRules.length,
rules: applicableRules.map(r => ({
name: r.name,
selector: r.selector,
urlPattern: r.urlPattern,
enabled: r.enabled,
elementsFound: findElements(r.selector).length
}))
};
sendResponse(debugInfo);
break;
default:
sendResponse({ status: 'unknown_action' });
}
return true; // Mantém o canal aberto para resposta assíncrona
});
}
// ============================================
// INICIALIZAÇÃO
// ============================================
async function init() {
if (isInitialized) return;
isInitialized = true;
log('Inicializando...', {
url: window.location.href,
isIframe
});
// Carrega configurações do storage
await loadConfig();
// Configura listeners
setupStorageListener();
setupMessageListener();
// Função para processar elementos
const doProcess = () => {
const result = processElements();
log(`Processamento inicial: ${result.total} encontrados, ${result.modified} modificados`);
return result;
};
// Aguarda o body existir
if (!document.body) {
document.addEventListener('DOMContentLoaded', () => {
doProcess();
setupMutationObserver();
// Retry após 500ms para elementos carregados tardiamente
setTimeout(() => {
const retryResult = processElements();
if (retryResult.modified > 0) {
log(`Retry: ${retryResult.modified} elemento(s) modificado(s)`);
}
}, 500);
});
} else {
doProcess();
setupMutationObserver();
// Retry após 500ms para elementos carregados tardiamente
setTimeout(() => {
const retryResult = processElements();
if (retryResult.modified > 0) {
log(`Retry: ${retryResult.modified} elemento(s) modificado(s)`);
}
}, 500);
// Segundo retry após 2s para páginas muito lentas
setTimeout(() => {
const retryResult = processElements();
if (retryResult.modified > 0) {
log(`Retry 2: ${retryResult.modified} elemento(s) modificado(s)`);
}
}, 2000);
}
log('Inicialização concluída');
}
// Inicia quando o documento estiver pronto
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();