-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
752 lines (666 loc) · 28.6 KB
/
Copy pathcontent.js
File metadata and controls
752 lines (666 loc) · 28.6 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
751
752
(() => {
// Define os elementos e atributos que normalmente representam blocos de Shorts no YouTube.
// Seletores focados apenas em elementos que explicitamente representam Shorts.
// Evitamos selecionar renderers genéricos (como ytd-rich-shelf-renderer) para não
// esconder prateleiras inteiras da página inicial que não são exclusivamente Shorts.
const seletoresParaEsconder = [
'a[href*="/shorts/"]',
'ytd-reel-shelf-renderer',
'ytd-reel-video-renderer',
'ytd-reel-item-renderer',
'[is-shorts]'
];
// Define o identificador do badge que será usado para mostrar o contador de dislikes.
const idDoBadgeDeDislikes = 'youtube-dislike-restorer-badge';
// Define a URL da API pública usada para recuperar a contagem de dislikes.
const urlDaApiDeDislikes = 'https://returnyoutubedislikeapi.com/votes';
// Configurações da extensão (lidas de chrome.storage.local quando disponível).
let youtunedSettings = {
hideShorts: true,
showDislikes: true,
blockedWords: []
};
let pendingDomWorkTimer = null;
let lastBlockedWordsSignature = '';
let thumbnailPendingNodes = new Set();
let thumbnailProcessingScheduled = false;
const DOM_WORK_DEBOUNCE_MS = 120;
const THUMBNAIL_PROCESS_DEBOUNCE_MS = 250;
let domWorkTimer = null;
let dislikeRetryTimer = null;
let thumbnailProcessTimer = null;
// Lê as configurações salvas — usa chrome.storage quando disponível, caso contrário localStorage.
function readSettings() {
return new Promise((resolve) => {
try {
if (typeof chrome !== 'undefined' && chrome.storage && chrome.storage.local) {
chrome.storage.local.get({ hideShorts: true, showDislikes: true, blockedWords: '' }, (res) => {
youtunedSettings.hideShorts = !!res.hideShorts;
youtunedSettings.showDislikes = !!res.showDislikes;
youtunedSettings.blockedWords = parseBlockedWords(res.blockedWords || '');
resolve(youtunedSettings);
});
return;
}
} catch (e) {}
// Fallback para localStorage (userscript / ambientes sem chrome.storage)
try {
const hs = localStorage.getItem('youtuned_hideShorts');
const sd = localStorage.getItem('youtuned_showDislikes');
const bw = localStorage.getItem('youtuned_blockedWords');
youtunedSettings.hideShorts = hs !== 'false';
youtunedSettings.showDislikes = sd !== 'false';
youtunedSettings.blockedWords = parseBlockedWords(bw || '');
} catch (e) {}
resolve(youtunedSettings);
});
}
// Aplica as configurações em tempo de execução: habilita/desabilita recursos.
function enableShorts() {
esconderShortsPorCSS();
esconderShortsNoDOM();
removerDescobertaAvancadaDeTemas();
limparItensVazios();
iniciarObservador();
agendarTrabalhoDOM();
}
function disableShorts() {
if (domWorkTimer) {
clearTimeout(domWorkTimer);
domWorkTimer = null;
}
// Remove a folha de estilo injetada
const estilo = document.getElementById('youtube-shorts-remover-style');
if (estilo && estilo.parentNode) estilo.parentNode.removeChild(estilo);
// Reexibe elementos que marcamos como ocultos manualmente
document.querySelectorAll('[data-shorts-hidden="true"]').forEach((el) => {
el.style.removeProperty('display');
el.removeAttribute('aria-hidden');
delete el.dataset.shortsHidden;
});
// Para o observador se estiver ativo
if (window.__youtubeShortsObserver) {
try { window.__youtubeShortsObserver.disconnect(); } catch {};
delete window.__youtubeShortsObserver;
}
}
function enableDislikes() {
restaurarDislikes();
iniciarProcessamentoDeThumbnails();
}
function disableDislikes() {
if (dislikeRetryTimer) {
clearTimeout(dislikeRetryTimer);
dislikeRetryTimer = null;
}
// Remove badge(s) das thumbnails
document.querySelectorAll('.youtuned-vote-badge').forEach((b) => b.remove());
// Remove badge único de vídeo
const badge = document.getElementById(idDoBadgeDeDislikes);
if (badge && badge.parentNode) badge.parentNode.removeChild(badge);
}
// Observa mudanças nas configurações (chrome.storage quando disponível, senão storage event local)
function listenSettingsChanges() {
try {
if (typeof chrome !== 'undefined' && chrome.storage && chrome.storage.onChanged) {
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== 'local') return;
if (changes.hideShorts) {
youtunedSettings.hideShorts = !!changes.hideShorts.newValue;
if (youtunedSettings.hideShorts) enableShorts(); else disableShorts();
}
if (changes.showDislikes) {
youtunedSettings.showDislikes = !!changes.showDislikes.newValue;
if (youtunedSettings.showDislikes) enableDislikes(); else disableDislikes();
}
if (changes.blockedWords) {
youtunedSettings.blockedWords = parseBlockedWords(changes.blockedWords.newValue || '');
aplicarFiltroDePalavras();
}
});
return;
}
} catch (e) {}
// Fallback para localStorage events (não funciona entre extensão e página, mas serve em alguns casos)
window.addEventListener('storage', (ev) => {
if (ev.key === 'youtuned_hideShorts') {
youtunedSettings.hideShorts = ev.newValue !== 'false';
if (youtunedSettings.hideShorts) enableShorts(); else disableShorts();
}
if (ev.key === 'youtuned_showDislikes') {
youtunedSettings.showDislikes = ev.newValue !== 'false';
if (youtunedSettings.showDislikes) enableDislikes(); else disableDislikes();
}
if (ev.key === 'youtuned_blockedWords') {
youtunedSettings.blockedWords = parseBlockedWords(ev.newValue || '');
aplicarFiltroDePalavras();
}
});
}
// Função responsável por esconder os elementos que representam Shorts no DOM atual.
function esconderShortsNoDOM() {
const elementosEncontrados = document.querySelectorAll(seletoresParaEsconder.join(','));
elementosEncontrados.forEach(aplicarRegraNoElemento);
}
// Função que aplica a regra de ocultação em um elemento específico e nos seus containers principais.
function aplicarRegraNoElemento(elemento) {
if (!elemento || elemento.dataset.shortsHidden === 'true') {
return;
}
const link = (elemento.getAttribute('href') || '').toLowerCase();
const texto = (elemento.textContent || '').toLowerCase();
const temShorts = link.includes('/shorts/') || texto.includes('shorts');
// Se não encontramos indícios óbvios de Shorts no link ou no texto, descartamos.
if (!temShorts && !elemento.matches(seletoresParaEsconder.join(','))) {
return;
}
// Escolhe um alvo seguro para esconder: preferimos um renderer de item/reel
// em vez de um shelf genérico. Isso evita remover toda a grade da home.
const alvo = elemento.closest('ytd-reel-item-renderer, ytd-reel-shelf-renderer, ytd-compact-video-renderer, ytd-video-renderer, ytd-grid-video-renderer, ytd-rich-item-renderer') || elemento;
// Se o alvo for uma prateleira genérica (pouco provável aqui), escondemos apenas o elemento encontrado.
const tag = (alvo && alvo.tagName || '').toLowerCase();
const esconderElemento = tag === 'ytd-reel-shelf-renderer' || tag.endsWith('reel-item-renderer') || alvo === elemento;
const nodeParaEsconder = esconderElemento ? elemento : alvo;
if (nodeParaEsconder) {
nodeParaEsconder.style.setProperty('display', 'none', 'important');
nodeParaEsconder.setAttribute('aria-hidden', 'true');
nodeParaEsconder.dataset.shortsHidden = 'true';
}
}
// Função que injeta uma regra CSS global para esconder Shorts mesmo antes do processamento do JavaScript.
function esconderShortsPorCSS() {
if (document.getElementById('youtube-shorts-remover-style')) {
return;
}
const estilo = document.createElement('style');
estilo.id = 'youtube-shorts-remover-style';
// Injeta apenas seletores explicitamente ligados a Shorts. Não remover prateleiras
// genéricas sem checagem evita que a home fique vazia.
estilo.textContent = `
ytd-reel-shelf-renderer,
ytd-reel-video-renderer,
ytd-reel-item-renderer,
a[href*="/shorts/"] {
display: none !important;
}
`;
document.head.appendChild(estilo);
}
// Função que limpa containers vazios após a ocultação dos blocos de Shorts.
function limparItensVazios() {
// Remove apenas containers que ficaram vazios (sem filhos visíveis) para
// não causar remoção de blocos inteiros que podem ter sido incorretamente marcados.
const containers = document.querySelectorAll('ytd-rich-item-renderer, ytd-rich-shelf-renderer, ytd-reel-shelf-renderer');
containers.forEach((container) => {
// Conta filhos visíveis
const filhosVisiveis = Array.from(container.children).some((c) => {
return c.offsetParent !== null && getComputedStyle(c).display !== 'none';
});
if (!filhosVisiveis) {
container.remove();
}
});
}
function removerDescobertaAvancadaDeTemas() {
document.querySelectorAll('ytd-chips-shelf-with-video-shelf-renderer').forEach((elemento) => {
const texto = (elemento.textContent || '').toLowerCase();
if (!texto.includes('descoberta avançada de novos temas')) {
return;
}
const alvo = elemento.closest('ytd-chips-shelf-with-video-shelf-renderer') || elemento;
alvo.style.setProperty('display', 'none', 'important');
alvo.dataset.youtunedHidden = 'true';
});
}
function scheduleDomWork() {
if (domWorkTimer) {
return;
}
domWorkTimer = window.setTimeout(() => {
domWorkTimer = null;
if (youtunedSettings.hideShorts) {
esconderShortsPorCSS();
esconderShortsNoDOM();
removerDescobertaAvancadaDeTemas();
limparItensVazios();
}
if (youtunedSettings.showDislikes) {
restaurarDislikes();
}
if (Array.isArray(youtunedSettings.blockedWords) && youtunedSettings.blockedWords.length > 0) {
aplicarFiltroDePalavras();
}
}, DOM_WORK_DEBOUNCE_MS);
}
// Função que cria um observador para acompanhar as mudanças dinâmicas da página do YouTube.
function iniciarObservador() {
if (window.__youtubeShortsObserver) {
return;
}
const observador = new MutationObserver(() => {
scheduleDomWork();
});
observador.observe(document.body || document.documentElement, {
childList: true,
subtree: true
});
window.__youtubeShortsObserver = observador;
}
// Função que verifica se a página atual é uma página de vídeo do YouTube.
function ePaginaDeVideo() {
const parametrosDaUrl = new URLSearchParams(window.location.search);
return window.location.pathname === '/watch' && Boolean(parametrosDaUrl.get('v'));
}
// Função que transforma os valores numéricos em texto com separadores legíveis.
function formatarNumero(valor) {
return new Intl.NumberFormat('pt-BR').format(Number(valor) || 0);
}
// Função que cria ou atualiza o badge visual que mostra o número de dislikes.
function mostrarBadgeDeDislikes(dislikes) {
if (!ePaginaDeVideo()) {
return;
}
const container = document.querySelector('#top-level-buttons-computed, ytd-menu-renderer, #actions');
if (!container) {
return;
}
let badge = document.getElementById(idDoBadgeDeDislikes);
if (!badge) {
badge = document.createElement('span');
badge.id = idDoBadgeDeDislikes;
badge.style.cssText = 'display:inline-flex; align-items:center; margin-left:8px; padding:6px 10px; border-radius:999px; background:var(--yt-spec-badge-chip-background); color:var(--yt-spec-text-primary); font-size:12px; font-weight:600;';
container.appendChild(badge);
}
badge.textContent = `↓ ${formatarNumero(dislikes)}`;
}
function parseBlockedWords(value) {
return value
.split(',')
.map((word) => word.trim().toLowerCase())
.filter((word) => word.length > 0);
}
function textoContemPalavraBloqueada(text, blockedWords) {
if (!text) return false;
const texto = text.toLowerCase();
return blockedWords.some((word) => texto.includes(word));
}
function restoreBlockedWordHiddenItems() {
document.querySelectorAll('[data-youtuned-blocked-word="true"]').forEach((el) => {
el.style.removeProperty('display');
el.removeAttribute('hidden');
el.removeAttribute('aria-hidden');
delete el.dataset.youtunedBlockedWord;
});
}
function findVideoCardForElement(element) {
return element.closest(
'ytd-rich-item-renderer, ytd-video-renderer, ytd-grid-video-renderer, ytd-compact-video-renderer, ytd-rich-grid-media, ytm-rich-item-renderer, ytm-grid-video-renderer, ytd-rich-grid-slim-media, ytd-rich-item-renderer'
);
}
function aplicarFiltroDePalavras() {
const signature = (Array.isArray(youtunedSettings.blockedWords) ? youtunedSettings.blockedWords : []).join('|');
if (signature !== lastBlockedWordsSignature) {
restoreBlockedWordHiddenItems();
lastBlockedWordsSignature = signature;
}
if (!Array.isArray(youtunedSettings.blockedWords) || youtunedSettings.blockedWords.length === 0) {
return;
}
const titles = Array.from(document.querySelectorAll('yt-formatted-string#video-title, h1.title, h1.title.style-scope.ytd-watch-metadata, ytd-rich-item-renderer h3, ytd-video-renderer h3, ytd-grid-video-renderer h3, ytd-compact-video-renderer h3'));
titles.forEach((title) => {
const text = title.textContent || title.innerText || '';
if (textoContemPalavraBloqueada(text, youtunedSettings.blockedWords)) {
const hideTarget = findVideoCardForElement(title) || title;
if (hideTarget && hideTarget.style.display !== 'none') {
hideTarget.style.setProperty('display', 'none', 'important');
hideTarget.hidden = true;
hideTarget.setAttribute('aria-hidden', 'true');
hideTarget.dataset.youtunedBlockedWord = 'true';
}
}
});
const comments = Array.from(document.querySelectorAll('#content-text, yt-formatted-string#comment-text, #main-text, #content-text ytd-expander'));
comments.forEach((comment) => {
const text = comment.textContent || comment.innerText || '';
if (textoContemPalavraBloqueada(text, youtunedSettings.blockedWords)) {
const commentItem = comment.closest('ytd-comment-thread-renderer, ytd-comment-renderer, ytd-item-section-renderer') || comment;
if (commentItem && commentItem.style.display !== 'none') {
commentItem.style.setProperty('display', 'none', 'important');
commentItem.hidden = true;
commentItem.setAttribute('aria-hidden', 'true');
commentItem.dataset.youtunedBlockedWord = 'true';
}
}
});
// Após ocultar itens por palavra bloqueada, limpa containers que ficaram vazios
limparItensVazios();
}
// Função que busca a contagem de dislikes diretamente em uma API pública.
async function buscarContagemDeDislikes() {
if (!ePaginaDeVideo()) {
return;
}
const existingBadge = document.getElementById(idDoBadgeDeDislikes);
if (existingBadge) {
existingBadge.remove();
}
const parametrosDaUrl = new URLSearchParams(window.location.search);
const videoId = parametrosDaUrl.get('v');
if (!videoId) {
return;
}
try {
const resposta = await fetch(`${urlDaApiDeDislikes}?videoId=${encodeURIComponent(videoId)}`, {
headers: {
Accept: 'application/json'
}
});
if (!resposta.ok) {
throw new Error('Falha ao buscar dislikes');
}
const dados = await resposta.json();
// Se receber dados, tentamos mostrar counts tanto no badge quanto nos botões do vídeo.
const likes = typeof dados.likes === 'number' ? dados.likes : null;
const dislikes = typeof dados.dislikes === 'number' ? dados.dislikes : null;
if (dislikes != null || likes != null) {
mostrarContadoresNosBotoes(likes, dislikes);
}
} catch (erro) {
console.warn('Não foi possível restaurar o contador de dislikes.', erro);
}
}
// Helpers inspirados na extensão de referência para localizar os botões e
// injetar os contadores de likes/dislikes no layout do vídeo.
function getButtons() {
const menuContainer = document.getElementById('menu-container');
if (menuContainer?.offsetParent === null) {
return (
document.querySelector('ytd-menu-renderer.ytd-watch-metadata > div') ||
document.querySelector('ytd-menu-renderer.ytd-video-primary-info-renderer > div')
);
}
return menuContainer?.querySelector('#top-level-buttons-computed');
}
function getDislikeButton() {
const buttons = getButtons();
if (!buttons) return null;
const firstChild = buttons.children[0];
if (firstChild?.tagName === 'YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER') {
return document.querySelector('#segmented-dislike-button') || firstChild.children[1];
}
if (buttons.querySelector('segmented-like-dislike-button-view-model')) {
return buttons.querySelector('dislike-button-view-model');
}
return buttons.children[1] || null;
}
function getLikeButton() {
const buttons = getButtons();
if (!buttons) return null;
const firstChild = buttons.children[0];
const tag = 'YTD-SEGMENTED-LIKE-DISLIKE-BUTTON-RENDERER';
if (firstChild?.tagName === tag) {
return document.querySelector('#segmented-like-button') || firstChild.children[0];
}
if (buttons.querySelector('segmented-like-dislike-button-view-model')) {
return buttons.querySelector('like-button-view-model');
}
return buttons.querySelector('like-button-view-model') || buttons.children[0];
}
function getTextElement(button) {
if (!button) return null;
return button.querySelector('#text, yt-formatted-string, .button-renderer-text, span[role="text"]');
}
function createTextElement(button) {
const textSpan = document.createElement('span');
// Reaproveita um elemento de texto existente, se houver, para evitar duplicação.
const existing = button.querySelector('#text, yt-formatted-string, .button-renderer-text, span[role="text"]');
if (existing) return existing;
textSpan.id = 'text';
textSpan.style.marginLeft = '6px';
const btn = button.querySelector('button');
if (btn) {
btn.appendChild(textSpan);
btn.style.width = 'auto';
}
return textSpan;
}
function formatNumber(count) {
return new Intl.NumberFormat('pt-BR', { notation: 'compact', compactDisplay: 'short' }).format(count);
}
function mostrarContadoresNosBotoes(likes, dislikes) {
// Apenas atualiza o botão de dislike — removemos qualquer escrita no botão de like.
const dislikeButton = getDislikeButton();
if (dislikeButton) {
let textEl = getTextElement(dislikeButton);
if (!textEl) textEl = createTextElement(dislikeButton);
if (textEl && dislikes != null) textEl.textContent = formatNumber(dislikes);
}
}
// Função que tenta restaurar o contador de dislikes quando o layout do vídeo estiver disponível.
function restaurarDislikes() {
if (!ePaginaDeVideo()) {
return;
}
const container = document.querySelector('#top-level-buttons-computed, ytd-menu-renderer, #actions');
if (!container) {
if (dislikeRetryTimer) {
clearTimeout(dislikeRetryTimer);
}
dislikeRetryTimer = window.setTimeout(() => {
dislikeRetryTimer = null;
restaurarDislikes();
}, 1000);
return;
}
if (dislikeRetryTimer) {
clearTimeout(dislikeRetryTimer);
dislikeRetryTimer = null;
}
buscarContagemDeDislikes();
}
// Função que inicializa a extensão e executa o processo de ocultação em cascata.
function iniciarExtensao() {
// Aplica recursos conforme as configurações lidas.
if (youtunedSettings.hideShorts) {
enableShorts();
}
if (youtunedSettings.showDislikes) {
enableDislikes();
}
aplicarFiltroDePalavras();
iniciarObservadorDeFiltro();
}
// Garante que a extensão rode quando a navegação do YouTube terminar de carregar a página.
document.addEventListener('yt-navigate-finish', iniciarExtensao, { once: false });
window.addEventListener('load', iniciarExtensao, { once: true });
// Lê configurações antes de iniciar e escuta mudanças.
readSettings().then(() => {
listenSettingsChanges();
iniciarExtensao();
});
})();
// =====================
// Thumbnails: likes/dislikes
// =====================
(function () {
// Cache local para votos recuperados de forma a reduzir requisições.
const CACHE_KEY = 'youtuned_votes_cache_v1';
const VOTES_API = 'https://returnyoutubedislikeapi.com/votes?videoId=';
let votesCache = null;
let inFlightThumbnailRequests = new Set();
function isWatchPage() {
const params = new URLSearchParams(window.location.search);
return window.location.pathname === '/watch' && Boolean(params.get('v'));
}
function loadVotesCache() {
if (votesCache !== null) {
return votesCache;
}
try {
votesCache = JSON.parse(localStorage.getItem(CACHE_KEY) || '{}');
} catch {
votesCache = {};
}
return votesCache;
}
function saveVotesCache(cache) {
try {
votesCache = cache;
localStorage.setItem(CACHE_KEY, JSON.stringify(cache));
} catch {}
}
// Extrai videoId de um href que pode ser /watch?v= ou /shorts/ID
function extrairVideoIdDeHref(href) {
try {
const url = new URL(href, location.origin);
if (url.pathname.startsWith('/shorts/')) {
return url.pathname.split('/shorts/')[1];
}
return url.searchParams.get('v');
} catch {
const m = href.match(/v=([a-zA-Z0-9_-]{6,})/);
if (m) return m[1];
const s = href.match(/\/shorts\/([a-zA-Z0-9_-]{6,})/);
return s ? s[1] : null;
}
}
// Faz fetch das contagens de likes/dislikes via API pública.
async function fetchVotes(videoId) {
if (!videoId) return null;
const cache = loadVotesCache();
if (cache[videoId]) return cache[videoId];
if (inFlightThumbnailRequests.has(videoId)) return null;
inFlightThumbnailRequests.add(videoId);
try {
const res = await fetch(VOTES_API + encodeURIComponent(videoId), { headers: { Accept: 'application/json' } });
if (!res.ok) return null;
const data = await res.json();
const votes = { likes: data.likes || null, dislikes: data.dislikes || null };
cache[videoId] = votes;
saveVotesCache(cache);
return votes;
} catch (e) {
return null;
} finally {
inFlightThumbnailRequests.delete(videoId);
}
}
// Determina se o tema do YouTube / container aparenta ser escuro.
function isDarkThemeForContainer(container) {
try {
const html = document.documentElement;
if (html.hasAttribute('dark')) return true;
if (html.classList && html.classList.contains('dark')) return true;
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) return true;
// Fallback: calcula luminância do background do container
const bg = getComputedStyle(container || document.body).backgroundColor || '';
const m = bg.match(/rgba?\((\d+), ?(\d+), ?(\d+)/);
if (m) {
const r = Number(m[1]), g = Number(m[2]), b = Number(m[3]);
const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return lum < 128;
}
} catch (e) {}
return false;
}
// Cria um badge pequeno e legível para a thumbnail com likes/dislikes.
// Usa SVGs com `currentColor` para permitir colorir os ícones conforme o tema.
function criarBadgeDeThumbnail(likes, dislikes, container) {
const dark = isDarkThemeForContainer(container);
const iconColor = dark ? '#fff' : '#000';
const bgColor = dark ? 'rgba(0,0,0,0.6)' : 'rgba(255,255,255,0.9)';
const badge = document.createElement('div');
badge.className = 'youtuned-vote-badge';
badge.style.cssText = `position:absolute; top:6px; right:6px; z-index:9999; background:${bgColor}; color:${iconColor}; padding:4px 8px; border-radius:12px; font-size:11px; display:inline-flex; gap:8px; align-items:center; font-weight:600;`;
const THUMBS_UP_SVG = `<svg viewBox="0 0 48 48" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><path d="M38,17H31l.4-3.3C32,8.8,31,4.9,27.8,4h-.3A2,2,0,0,0,26,5.2s-5.7,12-9,14.4V40h1.3a1.6,1.6,0,0,1,1.2.4c1.4,1,6.1,3.6,8.5,3.6h5c5.9,0,11-4,11.5-11.9h0l.5-8A6.7,6.7,0,0,0,38,17ZM3,22V38a2,2,0,0,0,2,2h8V20H5A2,2,0,0,0,3,22Z"/></svg>`;
const THUMBS_DOWN_SVG = `<svg viewBox="0 0 48 48" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><path d="M45,24l-.5-8h0C44,8,38.9,4,33,4H28c-2.4,0-7.1,2.6-8.5,3.6a1.6,1.6,0,0,1-1.2.4H17V28.4c3.3,2.4,9,14.4,9,14.4A2,2,0,0,0,27.5,44h.3c3.2-.9,4.2-4.8,3.6-9.7L31,31h7A6.7,6.7,0,0,0,45,24ZM5,28h8V8H5a2,2,0,0,0-2,2V26A2,2,0,0,0,5,28Z"/></svg>`;
function iconSVGFromString(svgString) {
return `<span style="display:inline-flex; width:12px; height:12px;" aria-hidden="true">${svgString}</span>`;
}
if (likes != null) {
const s = document.createElement('span');
s.style.display = 'inline-flex';
s.style.alignItems = 'center';
s.style.gap = '6px';
s.innerHTML = `${iconSVGFromString(THUMBS_UP_SVG)}<span>${Intl.NumberFormat('pt-BR').format(likes)}</span>`;
badge.appendChild(s);
}
if (dislikes != null) {
const s2 = document.createElement('span');
s2.style.display = 'inline-flex';
s2.style.alignItems = 'center';
s2.style.gap = '6px';
s2.innerHTML = `${iconSVGFromString(THUMBS_DOWN_SVG)}<span>${Intl.NumberFormat('pt-BR').format(dislikes)}</span>`;
badge.appendChild(s2);
}
return badge;
}
// Processa um elemento de thumbnail: extrai id, busca votes e injeta badge.
async function processThumbnail(anchor) {
if (!anchor || anchor.dataset.youtunedProcessed === 'true') return;
const href = anchor.getAttribute('href') || '';
const videoId = extrairVideoIdDeHref(href);
if (!videoId) return;
anchor.dataset.youtunedProcessed = 'true';
// Encontra o container visual da thumbnail para posicionar o badge.
const container = anchor.closest('ytd-rich-item-renderer, ytd-video-renderer, ytd-grid-video-renderer, ytd-compact-video-renderer, ytm-rich-item-renderer, ytm-grid-video-renderer') || anchor;
if (!container) return;
// Garante que o container permita posicionamento absoluto do badge.
if (getComputedStyle(container).position === 'static') {
container.style.position = 'relative';
}
const cache = loadVotesCache();
const votes = cache[videoId] || await fetchVotes(videoId);
if (!votes) return;
const badge = criarBadgeDeThumbnail(votes.likes, votes.dislikes, container);
// Remove badge antigo se existir
const existente = container.querySelector('.youtuned-vote-badge');
if (existente) existente.remove();
container.appendChild(badge);
}
// Varre todas thumbnails na página e processa as que encontrara.
function processAllThumbnails() {
if (isWatchPage()) {
return;
}
const anchors = Array.from(document.querySelectorAll('a[href*="/watch?v="], a[href*="/shorts/"]'));
anchors.forEach((a) => {
if (a.dataset.youtunedProcessed !== 'true') {
processThumbnail(a);
}
});
}
function scheduleThumbnailProcessing() {
if (thumbnailProcessTimer) {
return;
}
thumbnailProcessTimer = window.setTimeout(() => {
thumbnailProcessTimer = null;
processAllThumbnails();
}, THUMBNAIL_PROCESS_DEBOUNCE_MS);
}
let thumbnailObserver = null;
let wordFilterObserver = null;
// Inicia observador para detectar novas thumbnails dinamicamente.
function iniciarProcessamentoDeThumbnails() {
if (isWatchPage()) {
return;
}
scheduleThumbnailProcessing();
if (thumbnailObserver) return;
thumbnailObserver = new MutationObserver(() => {
scheduleThumbnailProcessing();
});
thumbnailObserver.observe(document.body || document.documentElement, { childList: true, subtree: true });
}
function iniciarObservadorDeFiltro() {
if (wordFilterObserver) return;
wordFilterObserver = new MutationObserver(() => {
aplicarFiltroDePalavras();
});
wordFilterObserver.observe(document.body || document.documentElement, { childList: true, subtree: true });
}
// Expor função global para ser chamada no flow principal.
window.iniciarProcessamentoDeThumbnails = iniciarProcessamentoDeThumbnails;
window.iniciarObservadorDeFiltro = iniciarObservadorDeFiltro;
})();