Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
799 lines (725 loc) · 28 KB
/
Copy pathapp.js
File metadata and controls
799 lines (725 loc) · 28 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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
// Leitor do news_feed: lê as matérias do repositório privado pela API do
// GitHub e grava o que já foi lido em dados/lidos.json, para celular e
// desktop verem a mesma coisa.
const REPO = "marcelocompucorp/news_feed";
const API = `https://api.github.com/repos/${REPO}`;
const ARQ_LIDOS = "dados/lidos.json";
const $ = (seletor) => document.querySelector(seletor);
const estado = {
aba: "noticias",
materias: [], // dados/materias.json
lidos: new Set(), // guids lidos (local + remoto)
pendentes: { add: new Set(), remove: new Set() }, // ainda não gravados no GitHub
aberta: null, // matéria na tela de leitura
selecao: { tipo: "tudo" }, // ou { tipo: "pasta" | "feed", nome }
feeds: [], // feeds.txt: { url, nome, pasta, filtrar }
};
// ---------- armazenamento local ----------
function lerLocal(chave, padrao) {
try {
const valor = localStorage.getItem(chave);
return valor === null ? padrao : JSON.parse(valor);
} catch {
return padrao;
}
}
function gravarLocal(chave, valor) {
try {
localStorage.setItem(chave, JSON.stringify(valor));
} catch {
// modo privado ou armazenamento cheio: segue só em memória
}
}
function salvarLidosLocal() {
gravarLocal("leitor.lidos", [...estado.lidos]);
gravarLocal("leitor.pendentes", {
add: [...estado.pendentes.add],
remove: [...estado.pendentes.remove],
});
}
const token = () => lerLocal("leitor.token", "");
// ---------- API do GitHub ----------
class ErroToken extends Error {}
async function github(caminho, opcoes = {}) {
const resposta = await fetch(caminho.startsWith("http") ? caminho : `${API}${caminho}`, {
...opcoes,
cache: "no-store",
headers: {
Authorization: `Bearer ${token()}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
...(opcoes.headers || {}),
},
});
if (resposta.status === 401 || resposta.status === 403) {
throw new ErroToken(`GitHub respondeu ${resposta.status}`);
}
return resposta;
}
async function lerTexto(caminho) {
const resposta = await github(`/contents/${caminho}`, {
headers: { Accept: "application/vnd.github.raw+json" },
});
if (!resposta.ok) throw new Error(`Falha ao ler ${caminho}: ${resposta.status}`);
return resposta.text();
}
async function lerArquivo(caminho) {
const resposta = await github(`/contents/${caminho}`, {
headers: { Accept: "application/vnd.github.raw+json" },
});
if (resposta.status === 404) return null;
if (!resposta.ok) throw new Error(`Falha ao ler ${caminho}: ${resposta.status}`);
return resposta.json();
}
function paraBase64(texto) {
const bytes = new TextEncoder().encode(texto);
let binario = "";
for (const byte of bytes) binario += String.fromCharCode(byte);
return btoa(binario);
}
function deBase64(base64) {
const binario = atob(base64.replace(/\n/g, ""));
return new TextDecoder().decode(Uint8Array.from(binario, (c) => c.charCodeAt(0)));
}
// ---------- sincronização dos lidos ----------
let sincronizando = false;
let timerSync = null;
function agendarSync() {
clearTimeout(timerSync);
timerSync = setTimeout(sincronizarLidos, 2000);
}
async function sincronizarLidos() {
if (sincronizando || !token()) return;
sincronizando = true;
try {
for (let tentativa = 0; tentativa < 3; tentativa++) {
const resposta = await github(`/contents/${ARQ_LIDOS}`);
let remoto = [];
let sha;
if (resposta.ok) {
const arquivo = await resposta.json();
sha = arquivo.sha;
remoto = JSON.parse(deBase64(arquivo.content)).lidos || [];
} else if (resposta.status !== 404) {
throw new Error(`Falha ao ler os lidos: ${resposta.status}`);
}
const guids = estado.materias.map((m) => m.guid);
// cópia do que vai agora: o que for marcado durante a gravação fica para a próxima
const enviados = { add: new Set(estado.pendentes.add), remove: new Set(estado.pendentes.remove) };
const final = unirLidos(remoto, enviados, guids);
const semMudanca = enviados.add.size === 0 && enviados.remove.size === 0;
if (semMudanca) {
estado.lidos = new Set(unirLidos(final, estado.pendentes, guids));
salvarLidosLocal();
return;
}
const gravacao = await github(`/contents/${ARQ_LIDOS}`, {
method: "PUT",
body: JSON.stringify({
message: "Atualiza matérias lidas",
content: paraBase64(JSON.stringify({ lidos: final }, null, 1) + "\n"),
...(sha ? { sha } : {}),
}),
});
if (gravacao.ok) {
estado.pendentes = tirarEnviados(estado.pendentes, enviados);
estado.lidos = new Set(unirLidos(final, estado.pendentes, guids));
salvarLidosLocal();
return;
}
if (gravacao.status !== 409 && gravacao.status !== 422) {
throw new Error(`Falha ao gravar os lidos: ${gravacao.status}`);
}
// 409/422: alguém (o outro aparelho) gravou antes; relê e tenta de novo
}
} catch (erro) {
tratarErro(erro);
} finally {
sincronizando = false;
// marcou algo enquanto gravava: o agendarSync daquela hora foi ignorado
if (estado.pendentes.add.size || estado.pendentes.remove.size) agendarSync();
desenhar();
}
}
function marcarLida(guid, lida) {
if (lida) {
estado.lidos.add(guid);
estado.pendentes.add.add(guid);
estado.pendentes.remove.delete(guid);
} else {
estado.lidos.delete(guid);
estado.pendentes.remove.add(guid);
estado.pendentes.add.delete(guid);
}
salvarLidosLocal();
agendarSync();
}
// ---------- carregar matérias ----------
async function carregar() {
if (!token()) {
abrirConfig("Para começar, cole o seu token do GitHub.");
return;
}
try {
mostrarStatus("Carregando…");
const [materias, feeds] = await Promise.all([lerArquivo("dados/materias.json"), lerTexto("feeds.txt")]);
estado.materias = materias || [];
estado.feeds = lerFeeds(feeds);
gravarLocal("leitor.materias", estado.materias);
gravarLocal("leitor.feeds", estado.feeds);
esconderStatus();
desenhar();
await sincronizarLidos();
} catch (erro) {
tratarErro(erro);
}
}
// ---------- atualizar: dispara o filtro no GitHub Actions ----------
async function atualizar() {
const botao = $("#atualizar");
if (botao.classList.contains("girando") || !token()) return;
botao.classList.add("girando");
try {
const inicio = new Date(Date.now() - 5000).toISOString();
const disparo = await github("/actions/workflows/filtro.yml/dispatches", {
method: "POST",
body: JSON.stringify({ ref: "main" }),
});
if (!disparo.ok) throw new Error(`Não consegui iniciar o filtro: ${disparo.status}`);
mostrarStatus("Buscando matérias novas no Engadget…");
// Acompanha a execução por até 5 minutos.
for (let i = 0; i < 60; i++) {
await new Promise((r) => setTimeout(r, 5000));
const resposta = await github(
`/actions/workflows/filtro.yml/runs?event=workflow_dispatch&per_page=5&created=${encodeURIComponent(">=" + inicio)}`,
);
const execucao = (await resposta.json()).workflow_runs?.[0];
if (execucao?.status === "completed") {
if (execucao.conclusion !== "success") {
mostrarStatus("O filtro terminou com erro. Veja a aba Actions no GitHub.");
return;
}
break;
}
}
await carregar();
} catch (erro) {
tratarErro(erro);
} finally {
botao.classList.remove("girando");
}
}
// ---------- desenhar ----------
const pastaDe = (m) => m.pasta || "Feeds";
// Compara pelo guid: a lista é recarregada do GitHub (a cada 10 min e ao
// voltar para o app) e aí cada matéria vira um objeto novo (22/11/2026).
const eAberta = (m) => !!estado.aberta && m.guid === estado.aberta.guid;
function naSelecao(m) {
const { tipo, nome } = estado.selecao;
if (tipo === "pasta") return pastaDe(m) === nome;
if (tipo === "feed") return m.fonte === nome;
return true;
}
function visiveis() {
const busca = $("#busca").value.trim();
const selecionadas = estado.materias.filter((m) => naSelecao(m) && casaBusca(m, busca));
// buscando, aparecem também as já lidas
if (busca && estado.aba === "noticias") return selecionadas.filter((m) => m.manter);
if (estado.aba === "barradas") return selecionadas.filter((m) => !m.manter);
const aprovadas = selecionadas.filter((m) => m.manter);
return $("#so-nao-lidas").checked
? aprovadas.filter((m) => !estado.lidos.has(m.guid) || eAberta(m))
: aprovadas;
}
// Pastas e feeds: os do feeds.txt, mais os que só aparecem nas matérias.
function arvore() {
const pastas = new Map();
const incluir = (pasta, feed) => {
if (!pastas.has(pasta)) pastas.set(pasta, new Set());
if (feed) pastas.get(pasta).add(feed);
};
estado.feeds.forEach((f) => incluir(f.pasta, f.nome));
estado.materias.forEach((m) => incluir(pastaDe(m), m.fonte));
return pastas;
}
function desenharArvore() {
const naoLida = (m) => m.manter && !estado.lidos.has(m.guid);
const contar = (filtro) => estado.materias.filter((m) => naoLida(m) && filtro(m)).length;
const { tipo, nome } = estado.selecao;
const item = (classe, rotulo, numero, selecao) => {
const li = document.createElement("li");
li.className = classe;
const botao = document.createElement("button");
const texto = document.createElement("span");
texto.textContent = rotulo;
const qtd = document.createElement("span");
qtd.className = "numero";
qtd.textContent = numero || "";
botao.append(texto, qtd);
botao.classList.toggle("ativa", selecao.tipo === tipo && selecao.nome === nome);
botao.addEventListener("click", () => selecionar(selecao));
li.append(botao);
return li;
};
const lista = [item("pasta", "Tudo", contar(() => true), { tipo: "tudo" })];
for (const [pasta, feeds] of arvore()) {
lista.push(item("pasta", pasta, contar((m) => pastaDe(m) === pasta), { tipo: "pasta", nome: pasta }));
for (const feed of feeds) {
lista.push(item("feed", feed, contar((m) => m.fonte === feed), { tipo: "feed", nome: feed }));
}
}
$("#arvore").replaceChildren(...lista);
$("#selecao-titulo").textContent = tipo === "tudo" ? "Tudo" : nome;
}
function selecionar(selecao) {
estado.selecao = selecao;
gravarLocal("leitor.selecao", selecao);
fecharPastas();
sairDaLeitura();
desenhar();
$("#lista-painel").scrollTop = 0;
}
function abrirPastas() {
document.body.classList.add("pastas-abertas");
$("#fundo-pastas").hidden = false;
}
function fecharPastas() {
document.body.classList.remove("pastas-abertas");
$("#fundo-pastas").hidden = true;
}
function desenhar() {
const naoLidas = estado.materias.filter((m) => m.manter && !estado.lidos.has(m.guid)).length;
$("#contador").textContent = naoLidas ? `(${naoLidas})` : "";
document.title = naoLidas ? `(${naoLidas}) Leitor` : "Leitor";
$("#filtros").hidden = estado.aba === "barradas";
desenharArvore();
const lista = $("#lista");
lista.replaceChildren();
const itens = visiveis().sort((a, b) => new Date(b.data) - new Date(a.data));
let diaAnterior = null;
for (const materia of itens) {
const dia = rotuloDoDia(materia.data);
if (dia !== diaAnterior) {
const divisor = document.createElement("li");
divisor.className = "divisor";
divisor.textContent = dia;
lista.append(divisor);
diaAnterior = dia;
}
const li = document.createElement("li");
const lida = estado.aba === "noticias" && estado.lidos.has(materia.guid);
li.className = lida ? "lida" : "";
if (eAberta(materia)) li.classList.add("selecionada");
if (materia.imagem) {
const img = document.createElement("img");
img.className = "miniatura";
img.loading = "lazy";
img.referrerPolicy = "no-referrer";
img.alt = "";
img.src = materia.imagem;
li.append(img);
}
const info = document.createElement("div");
info.className = "info";
const titulo = document.createElement("div");
titulo.className = "titulo";
titulo.textContent = materia.titulo;
const meta = document.createElement("div");
meta.className = "meta";
meta.textContent = [tempoRelativo(materia.data), materia.fonte,
materia.comentarios ? `${materia.comentarios} comentário${materia.comentarios > 1 ? "s" : ""}` : ""]
.filter(Boolean).join(" · ");
info.append(titulo, meta);
if (estado.aba === "barradas") {
const motivo = document.createElement("div");
motivo.className = "motivo";
motivo.textContent = materia.motivo;
info.append(motivo);
}
li.append(info);
if (estado.aba === "noticias") {
const ponto = document.createElement("span");
ponto.className = "ponto";
li.append(ponto);
}
li.addEventListener("click", () => {
if (li.dataset.arrastou) return; // o toque terminou um arrasto, não abre
abrir(materia);
});
if (estado.aba === "noticias") permitirArrasto(li, materia);
lista.append(li);
}
const vazio = $("#vazio");
vazio.hidden = itens.length > 0;
vazio.textContent = $("#busca").value.trim()
? "Nenhuma matéria encontrada."
: estado.aba === "barradas"
? "Nenhuma matéria barrada."
: "Nada novo por aqui. Toque em ⟳ para buscar matérias novas.";
}
// Arrastar o item para a esquerda (no celular) alterna lida / não lida.
function permitirArrasto(li, materia) {
const acao = document.createElement("span");
acao.className = "acao";
li.prepend(acao);
let inicio = null, direcao = null, dx = 0;
const mover = (x) => {
li.style.setProperty("--dx", `${x}px`);
acao.classList.toggle("direita", x > 0);
acao.textContent = x > 0 ? "● Não lida" : "✓ Lida";
acao.style.width = `${Math.abs(x)}px`;
};
const soltar = () => {
li.classList.remove("arrastando");
if (direcao === "horizontal") {
li.dataset.arrastou = "1";
setTimeout(() => delete li.dataset.arrastou, 0); // depois do click que vem junto
const resultado = acaoDoArrasto(dx, li.offsetWidth);
if (resultado) {
const lida = resultado === "lida";
if (estado.lidos.has(materia.guid) !== lida) marcarLida(materia.guid, lida);
const some = lida && $("#so-nao-lidas").checked && !$("#busca").value.trim() && !eAberta(materia);
li.classList.add("concluindo");
mover(some ? -li.offsetWidth : 0);
setTimeout(desenhar, some ? 200 : 150);
} else {
mover(0);
}
}
inicio = null; direcao = null; dx = 0;
};
li.addEventListener("pointerdown", (e) => {
if (e.pointerType === "mouse") return; // no Mac continua o clique normal
inicio = { x: e.clientX, y: e.clientY, id: e.pointerId };
direcao = null; dx = 0;
});
li.addEventListener("pointermove", (e) => {
if (!inicio || e.pointerId !== inicio.id) return;
const ddx = e.clientX - inicio.x, ddy = e.clientY - inicio.y;
if (!direcao) {
direcao = direcaoDoArrasto(ddx, ddy);
if (direcao === "vertical") { inicio = null; return; }
if (direcao === "horizontal") { try { li.setPointerCapture(e.pointerId); } catch {} li.classList.add("arrastando"); }
}
if (direcao === "horizontal") { dx = ddx; mover(dx); }
});
li.addEventListener("pointerup", soltar);
li.addEventListener("pointercancel", () => { direcao = direcao === "horizontal" ? "cancelado" : direcao; mover(0); soltar(); });
}
async function abrir(materia, { empilhar = true } = {}) {
estado.aberta = materia;
document.body.classList.add("lendo");
$("#leitura").hidden = false;
$("#leitura").scrollTop = 0;
if (empilhar) history.pushState({ lendo: materia.guid }, "");
// Marca já no clique: se esperasse o texto carregar, quem passa para a
// próxima antes disso deixava esta como não lida (17/11/2026).
if (materia.manter && !estado.lidos.has(materia.guid)) marcarLida(materia.guid, true);
desenhar();
const tituloLink = $("#l-titulo-link");
tituloLink.textContent = materia.titulo;
if (linkSeguro(materia.link)) tituloLink.href = linkSeguro(materia.link);
else tituloLink.removeAttribute("href");
$("#l-meta").textContent = [tempoRelativo(materia.data), materia.fonte, ...(materia.categorias || [])]
.filter(Boolean).join(" · ");
const imagem = $("#l-imagem");
imagem.hidden = !materia.imagem;
if (materia.imagem) imagem.src = materia.imagem;
$("#l-motivo").textContent = materia.motivo ? `Filtro: ${materia.motivo}` : "";
$("#l-link").href = materia.link;
$("#l-nao-lida").hidden = !materia.manter;
$("#l-nao-lida-topo").hidden = !materia.manter;
const texto = $("#l-texto");
texto.replaceChildren(paragrafo(materia.resumo));
if (materia.manter && materia.tem_texto) {
texto.append(paragrafo("Carregando o texto completo…", "aviso"));
try {
const dados = await lerArquivo(`dados/texto/${materia.id}.json`);
if (!eAberta(materia)) return;
const blocos = blocosDoTexto(dados).filter((b) => !(b.tipo === "img" && b.src === materia.imagem));
texto.replaceChildren(...(blocos.length ? blocos.map(montarBloco) : [paragrafo(materia.resumo)]));
if (capaNoTexto(blocos)) $("#l-imagem").hidden = true;
} catch (erro) {
texto.replaceChildren(paragrafo(materia.resumo), paragrafo("Não consegui carregar o texto completo.", "aviso"));
tratarErro(erro);
}
} else if (materia.manter) {
texto.append(paragrafo("Texto completo indisponível; abra no site.", "aviso"));
}
prepararComentarios(materia);
}
// Monta um bloco do texto completo com elementos criados aqui e texto via
// textContent: nada que veio do site é interpretado como HTML.
function montarBloco(bloco) {
const elemento = (tag, filhos = [], texto) => {
const el = document.createElement(tag);
if (texto !== undefined) el.textContent = texto;
el.append(...filhos);
return el;
};
const partes = (lista = []) =>
lista.map((parte) => {
let el = document.createTextNode(parte.texto);
if (parte.italico) el = elemento("em", [el]);
if (parte.negrito) el = elemento("strong", [el]);
const href = parte.link && linkSeguro(parte.link);
if (href) {
const a = elemento("a", [el]);
a.href = href;
a.target = "_blank";
a.rel = "noopener noreferrer";
el = a;
}
return el;
});
switch (bloco.tipo) {
case "h":
return elemento("h2", [], bloco.texto);
case "lista":
return elemento("ul", (bloco.itens || []).map((item) => elemento("li", partes(item))));
case "citacao":
return elemento("blockquote", partes(bloco.partes));
case "tabela": {
const VISIVEIS = 10;
const linhas = (bloco.linhas || []).map((linha, i) => {
const tr = elemento("tr", linha.map((celula) => elemento("td", [], celula)));
if (i >= VISIVEIS) tr.hidden = true;
return tr;
});
const tabela = elemento("table", [
elemento("thead", [elemento("tr", (bloco.colunas || []).map((c) => elemento("th", [], c)))]),
elemento("tbody", linhas),
]);
const filhos = [bloco.titulo ? elemento("figcaption", [], bloco.titulo) : null, tabela].filter(Boolean);
if (linhas.length > VISIVEIS) {
const botao = elemento("button", [], `Ver todos os ${linhas.length} trechos`);
botao.addEventListener("click", () => {
linhas.forEach((tr) => (tr.hidden = false));
botao.remove();
});
filhos.push(botao);
}
return elemento("figure", filhos);
}
case "img": {
const src = linkSeguro(bloco.src);
if (!src) return document.createTextNode("");
const img = elemento("img");
img.src = src;
img.alt = bloco.legenda || "";
img.loading = "lazy";
img.referrerPolicy = "no-referrer";
const legenda = [bloco.legenda, bloco.credito && `Foto: ${bloco.credito}`].filter(Boolean).join(" · ");
return elemento("figure", legenda ? [img, elemento("figcaption", [], legenda)] : [img]);
}
default:
return elemento("p", partes(bloco.partes));
}
}
// Comentários: só são baixados quando você toca em "Ver comentários".
function prepararComentarios(materia) {
const secao = $("#l-comentarios");
const botao = $("#l-ver-comentarios");
const lista = $("#l-lista-comentarios");
lista.replaceChildren();
secao.hidden = !materia.comentarios;
botao.hidden = false;
botao.disabled = false;
botao.textContent = `Ver ${materia.comentarios} comentário${materia.comentarios > 1 ? "s" : ""}`;
botao.onclick = async () => {
botao.disabled = true;
botao.textContent = "Carregando comentários…";
try {
const dados = await lerArquivo(`dados/comentarios/${materia.id}.json`);
if (!eAberta(materia)) return;
lista.replaceChildren(...(dados?.comentarios || []).map(montarComentario));
botao.hidden = true;
} catch (erro) {
botao.disabled = false;
botao.textContent = "Não consegui carregar. Tentar de novo";
tratarErro(erro);
}
};
}
function montarComentario(comentario) {
const li = document.createElement("li");
li.style.setProperty("--nivel", Math.min(Math.max(comentario.nivel || 1, 1), 5) - 1);
const cabecalho = document.createElement("div");
cabecalho.className = "c-cabecalho";
const autor = document.createElement("b");
autor.textContent = comentario.autor;
const data = document.createElement("span");
data.textContent = dataDoComentario(comentario.data);
cabecalho.append(autor, data);
li.append(cabecalho, ...(comentario.paragrafos || []).map((p) => paragrafo(p)));
return li;
}
function paragrafo(conteudo, classe) {
const p = document.createElement("p");
p.textContent = conteudo; // texto puro: nada do site é interpretado como HTML
if (classe) p.className = classe;
return p;
}
// Trocar de feed, pasta ou aba volta para a lista. No celular a matéria aberta
// cobre a lista; sem isso a lista mudava por trás e nada aparecia.
function sairDaLeitura() {
if (!estado.aberta) return;
if (history.state?.lendo) history.back(); // o "popstate" chama fecharLeitura
else fecharLeitura();
}
function fecharLeitura() {
estado.aberta = null;
document.body.classList.remove("lendo");
$("#leitura").hidden = true;
desenhar();
}
// ---------- status, erros e configuração ----------
function mostrarStatus(texto) {
$("#status").textContent = texto;
$("#status").hidden = false;
}
function esconderStatus() {
$("#status").hidden = true;
}
function tratarErro(erro) {
console.error(erro);
if (erro instanceof ErroToken) {
abrirConfig("O GitHub recusou o token (inválido, expirado ou sem permissão). Cole um novo.");
} else {
mostrarStatus(navigator.onLine ? `Erro: ${erro.message}` : "Sem internet. Mostrando o que já estava carregado.");
}
}
function abrirConfig(mensagem = "") {
$("#token").value = token();
$("#config-msg").textContent = mensagem;
if (!$("#config").open) $("#config").showModal();
}
async function testarToken() {
const valor = $("#token").value.trim();
const msg = $("#config-msg");
msg.textContent = "Testando…";
gravarLocal("leitor.token", valor);
try {
const resposta = await github("");
if (!resposta.ok) throw new Error(`GitHub respondeu ${resposta.status}`);
msg.textContent = "Token funcionando.";
$("#config").close();
carregar();
} catch (erro) {
msg.textContent = `Não funcionou: ${erro.message}. Confira se o token tem acesso ao repositório news_feed.`;
}
}
// ---------- adicionar feed: acrescenta uma linha no feeds.txt ----------
function abrirNovoFeed() {
fecharPastas();
const pastas = [...arvore().keys()];
$("#nf-pastas").replaceChildren(...pastas.map((p) => Object.assign(document.createElement("option"), { value: p })));
$("#nf-pasta").value = estado.selecao.tipo === "pasta" ? estado.selecao.nome : "";
$("#nf-msg").textContent = "";
$("#novo-feed").showModal();
}
async function salvarNovoFeed() {
const url = $("#nf-url").value.trim();
const nome = $("#nf-nome").value.trim().replaceAll("|", "/");
const pasta = $("#nf-pasta").value.trim().replace(/[[\]]/g, "");
const msg = $("#nf-msg");
if (!/^https?:\/\/\S+$/.test(url) || !nome || !pasta) {
msg.textContent = "Preencha o endereço (começando com https://), o nome e a pasta.";
return;
}
if (estado.feeds.some((f) => f.url === url)) {
msg.textContent = "Esse feed já está na lista.";
return;
}
msg.textContent = "Salvando…";
try {
const linha = `${url} | ${nome}${$("#nf-filtrar").checked ? " | filtrar" : ""}`;
for (let tentativa = 0; tentativa < 3; tentativa++) {
const atual = await (await github("/contents/feeds.txt")).json();
const gravacao = await github("/contents/feeds.txt", {
method: "PUT",
body: JSON.stringify({
message: `Adiciona o feed ${nome}`,
content: paraBase64(incluirNoFeedsTxt(deBase64(atual.content), pasta, linha)),
sha: atual.sha,
}),
});
if (gravacao.ok) {
estado.feeds.push({ url, nome, pasta, filtrar: $("#nf-filtrar").checked });
$("#novo-feed").close();
$("#novo-feed-form").reset();
selecionar({ tipo: "feed", nome });
atualizar(); // busca as matérias do feed novo
return;
}
if (gravacao.status !== 409 && gravacao.status !== 422) throw new Error(`GitHub respondeu ${gravacao.status}`);
}
throw new Error("o arquivo mudou várias vezes seguidas");
} catch (erro) {
msg.textContent = `Não consegui salvar: ${erro.message}`;
if (erro instanceof ErroToken) tratarErro(erro);
}
}
// ---------- início ----------
function iniciar() {
estado.materias = lerLocal("leitor.materias", []);
estado.feeds = lerLocal("leitor.feeds", []);
estado.selecao = lerLocal("leitor.selecao", { tipo: "tudo" });
estado.lidos = new Set(lerLocal("leitor.lidos", []));
const pendentes = lerLocal("leitor.pendentes", { add: [], remove: [] });
estado.pendentes = { add: new Set(pendentes.add), remove: new Set(pendentes.remove) };
$("#so-nao-lidas").checked = lerLocal("leitor.soNaoLidas", true);
document.querySelectorAll(".aba").forEach((botao) =>
botao.addEventListener("click", () => {
estado.aba = botao.dataset.aba;
document.querySelectorAll(".aba").forEach((b) => b.classList.toggle("ativa", b === botao));
sairDaLeitura();
desenhar();
}),
);
$("#so-nao-lidas").addEventListener("change", (e) => {
gravarLocal("leitor.soNaoLidas", e.target.checked);
desenhar();
});
$("#marcar-todas").addEventListener("click", () => {
for (const m of estado.materias) if (m.manter && !estado.lidos.has(m.guid)) marcarLida(m.guid, true);
desenhar();
});
$("#busca").addEventListener("input", desenhar);
$("#busca").addEventListener("keydown", (e) => {
if (e.key === "Escape") { e.target.value = ""; desenhar(); }
});
$("#atualizar").addEventListener("click", atualizar);
$("#abrir-pastas").addEventListener("click", abrirPastas);
$("#fundo-pastas").addEventListener("click", fecharPastas);
$("#abrir-novo-feed").addEventListener("click", abrirNovoFeed);
$("#nf-salvar").addEventListener("click", salvarNovoFeed);
$("#abrir-config").addEventListener("click", () => abrirConfig());
$("#testar-token").addEventListener("click", testarToken);
$("#voltar").addEventListener("click", () => history.back());
for (const botao of [$("#l-nao-lida"), $("#l-nao-lida-topo")]) {
botao.addEventListener("click", () => {
if (estado.aberta) marcarLida(estado.aberta.guid, false);
desenhar();
});
}
window.addEventListener("popstate", fecharLeitura);
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") sincronizarLidos();
else carregar();
});
// O que foi lido no outro aparelho aparece aqui sem precisar recarregar:
// confere os lidos a cada minuto e a lista inteira a cada 10, só com o app
// na tela (são chamadas pequenas, bem abaixo do limite da API do GitHub).
window.addEventListener("focus", sincronizarLidos);
setInterval(() => {
if (document.visibilityState === "visible") sincronizarLidos();
}, 60 * 1000);
setInterval(() => {
if (document.visibilityState === "visible") carregar();
}, 10 * 60 * 1000);
desenhar();
carregar();
if ("serviceWorker" in navigator) navigator.serviceWorker.register("sw.js");
}
iniciar();