-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidepanel.js
More file actions
1400 lines (1240 loc) · 49.2 KB
/
sidepanel.js
File metadata and controls
1400 lines (1240 loc) · 49.2 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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
getSettings, saveSettings, DEFAULT_SETTINGS,
getPinnedMap, addPin, removePin,
getFromStorage, setToStorage,
getCachedComments, setCachedComments,
setPinnedTags, pushRecentTag, getRecentTags, normalizeTag, tagHue,
getCollapsedCards, setCardCollapsed,
getRadarStore, updateRadarSnapshots, computeVelocityDetail,
getMutedDomains, setMutedDomains,
getCachedOPActive, setCachedOPActive,
// Phase 3a — Watchlist
getWatchRules, addWatchRule, updateWatchRule, deleteWatchRule,
getWatchUnread, clearWatchUnread, removeWatchUnread,
WATCH_RULES_MAX
} from "./storage.js";
import { startWatcher, stopWatcher, onUnreadChange, triggerTick } from "./watcher.js";
import { summarizeRule } from "./match-engine.js";
import { fetchTopComments, relativeTime, commentToPlainText, translateText, isTranslatorAvailable } from "./hn-api.js";
import { fetchListIds, fetchItemsBatch, extractDomain, detectItemType, checkOPActive } from "./hn-data.js";
import { LANGUAGES, RTL_LANGS, detectLanguage, setLanguage, t, tr, applyI18n } from "./i18n.js";
const $ = (id) => document.getElementById(id);
let settings = { ...DEFAULT_SETTINGS };
document.addEventListener("DOMContentLoaded", async () => {
settings = await getSettings();
// ── i18n init ────
const lang = settings.uiLanguage || detectLanguage();
setLanguage(lang);
applyI18n();
document.documentElement.dir = RTL_LANGS.has(lang) ? "rtl" : "ltr";
// Populate UI language picker
const langSel = $("uiLanguage");
if (langSel) {
LANGUAGES.forEach(({ code, label }) => {
const opt = document.createElement("option");
opt.value = code;
opt.textContent = label;
langSel.appendChild(opt);
});
langSel.value = lang;
langSel.addEventListener("change", async () => {
const newLang = langSel.value;
settings.uiLanguage = newLang;
setLanguage(newLang);
document.documentElement.dir = RTL_LANGS.has(newLang) ? "rtl" : "ltr";
applyI18n();
applyCtaPitch();
await saveSettings(settings);
updateTransHint();
renderPinned();
});
}
// ── Master toggle ────
$("enabled").checked = settings.enabled;
$("enabled").addEventListener("change", () => {
settings.enabled = $("enabled").checked;
persistAndBroadcast();
updateSettingsState();
});
// ── Segmented controls ────
document.querySelectorAll(".seg").forEach((seg) => {
const key = seg.dataset.key;
seg.querySelectorAll("button").forEach((btn) => {
const value = btn.dataset.value;
if (settings[key] === value) btn.classList.add("active");
btn.addEventListener("click", () => {
settings[key] = value;
seg.querySelectorAll("button").forEach((b) => b.classList.remove("active"));
btn.classList.add("active");
persistAndBroadcast();
});
});
});
// ── Checkboxes ────
const checkIds = ["compact", "markVisited", "highlightShow", "highlightAsk", "highlightLaunch", "autoTranslate", "autoTranslatePinned", "zebraEnabled"];
checkIds.forEach((id) => {
$(id).checked = !!settings[id];
$(id).addEventListener("change", () => {
settings[id] = $(id).checked;
persistAndBroadcast();
if (id === "autoTranslatePinned") renderPinned();
});
});
// ── Translation language picker ────
$("translateTo").value = settings.translateTo || "";
$("translateTo").addEventListener("change", () => {
settings.translateTo = $("translateTo").value;
persistAndBroadcast();
if (settings.autoTranslatePinned) renderPinned();
});
updateTransHint();
// ── Zebra color picker + intensity ────
const zebraColorEl = $("zebraColor");
if (zebraColorEl) {
zebraColorEl.value = settings.zebraColor || "#ff8800";
zebraColorEl.addEventListener("input", () => {
settings.zebraColor = zebraColorEl.value;
persistAndBroadcast();
});
}
const zebraIntEl = $("zebraIntensity");
const zebraIntValEl = $("zebraIntensityValue");
if (zebraIntEl) {
const initial = settings.zebraIntensity || 12;
zebraIntEl.value = initial;
if (zebraIntValEl) zebraIntValEl.textContent = `${initial}%`;
zebraIntEl.addEventListener("input", () => {
const v = Number(zebraIntEl.value) || 12;
settings.zebraIntensity = v;
if (zebraIntValEl) zebraIntValEl.textContent = `${v}%`;
persistAndBroadcast();
});
}
updateSettingsState();
// ── Muted domains textarea ────
const mutedEl = $("mutedDomains");
if (mutedEl) {
const initial = await getMutedDomains();
mutedEl.value = initial.join("\n");
let mutedTimer = null;
mutedEl.addEventListener("input", () => {
clearTimeout(mutedTimer);
mutedTimer = setTimeout(async () => {
const list = mutedEl.value.split(/\r?\n/);
await setMutedDomains(list);
loadRadar(activeRadarCat); // re-render radar with new filter
}, 500);
});
}
// ── Pinned section ────
renderPinned();
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === "local" && changes.pinned) renderPinned();
});
// ── Collapsible panels ────
initCollapsiblePanels();
// ── Radar ────
initRadar();
// ── Watch (Phase 3a) ────
initWatch();
// ── NODUS CTA strip — daily-rotating pitch + ticker animation ────
applyCtaPitch();
startCtaCycle();
// Stop polling when the panel is hidden — Chrome side panels don't fire
// beforeunload reliably, so we use visibilitychange as a defensive stop.
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") {
stopWatcher();
stopCtaCycle();
} else {
startWatcher();
startCtaCycle();
}
});
});
async function initCollapsiblePanels() {
// Defaults: pinned + radar expanded, settings collapsed (matches HTML)
const stored = (await getFromStorage("panelStates")) || { pinned: false, radar: false, settings: true };
document.querySelectorAll(".panel[data-panel-key]").forEach((panel) => {
const key = panel.dataset.panelKey;
const collapsed = stored[key];
if (typeof collapsed === "boolean") {
panel.classList.toggle("collapsed", collapsed);
}
const head = panel.querySelector(".panel-head[data-collapsible]");
if (!head) return;
head.addEventListener("click", async () => {
const nowCollapsed = panel.classList.toggle("collapsed");
const states = (await getFromStorage("panelStates")) || {};
states[key] = nowCollapsed;
await setToStorage("panelStates", states);
});
});
}
function updateTransHint() {
const hintEl = $("translate-hint");
if (!hintEl) return;
if (isTranslatorAvailable()) {
hintEl.textContent = tr("settings.transReady");
hintEl.classList.remove("warning");
} else {
hintEl.textContent = tr("settings.transUnavailable");
hintEl.classList.add("warning");
}
}
function updateSettingsState() {
const panel = $("settings-panel");
if (!panel) return;
if (settings.enabled) panel.classList.remove("disabled");
else panel.classList.add("disabled");
}
async function persistAndBroadcast() {
await saveSettings(settings);
chrome.runtime.sendMessage({ type: "BROADCAST_SETTINGS", settings }, () => {
void chrome.runtime.lastError;
});
}
// ── Pinned posts rendering ──────────────────────────────
async function renderPinned() {
const map = await getPinnedMap();
const collapsedMap = await getCollapsedCards();
const entries = Object.values(map).sort((a, b) => (b.pinnedAt || 0) - (a.pinnedAt || 0));
entries.forEach((p) => { p._collapsed = !!collapsedMap[p.id]; });
const listEl = $("pinned-list");
const emptyEl = $("pinned-empty");
const countEl = $("pinned-count");
countEl.textContent = entries.length === 0 ? "0" : String(entries.length);
if (entries.length === 0) {
emptyEl.hidden = false;
listEl.hidden = true;
listEl.innerHTML = "";
return;
}
emptyEl.hidden = true;
listEl.hidden = false;
listEl.innerHTML = "";
// Optionally translate titles before rendering
const target = resolveTargetLang();
const doTranslate = settings.autoTranslatePinned && target && isTranslatorAvailable();
if (doTranslate) {
await Promise.all(entries.map(async (p) => {
const res = await translateText(p.title || "", target);
if (res && res.translated) {
p._translatedTitle = res.translated;
p._sourceLang = res.sourceLang;
p._targetLang = target;
}
}));
}
entries.forEach((p) => listEl.appendChild(buildPinnedCard(p)));
}
function buildPinnedCard(post) {
const card = document.createElement("div");
card.className = "pinned-card";
card.dataset.id = post.id;
if (post._collapsed) card.classList.add("card-collapsed");
const typeBadge = post.type
? `<span class="pinned-card-type ${escapeAttr(post.type)}">${escapeText(post.type)}</span>`
: "";
const hasTranslation = !!post._translatedTitle;
const displayTitle = hasTranslation ? post._translatedTitle : (post.title || "(no title)");
const transBadge = hasTranslation
? `<span class="pinned-card-trans" title="Click to see original" data-original="${escapeAttr(post.title || "")}" data-translated="${escapeAttr(post._translatedTitle)}">🌐 ${(post._sourceLang || "?").toUpperCase()}→${(post._targetLang || "?").toUpperCase()}</span>`
: "";
card.innerHTML = `
<div class="pinned-card-actions">
<button class="card-chevron" type="button" title="${escapeAttr(post._collapsed ? tr("pinned.expand") : tr("pinned.collapse"))}">▾</button>
<button class="pinned-card-unpin" title="${escapeAttr(tr("pinned.unpin"))}">×</button>
</div>
<div class="pinned-card-title">${typeBadge}<span class="pinned-card-title-text">${escapeText(displayTitle)}</span>${transBadge}</div>
<div class="pinned-card-collapsible">
${post.domain ? `<div class="pinned-card-domain">${escapeText(post.domain)}</div>` : ""}
<div class="pinned-card-meta">
<span class="pin-score">${post.score || 0} ${escapeText(tr("pinned.pointsUnit"))}</span>
<span class="sep">·</span>
<span class="pinned-card-comments" data-id="${escapeAttr(post.id)}" title="${escapeAttr(tr("pinned.hoverHint"))}">
${post.comments || 0} ${escapeText(tr("pinned.commentsUnit"))}
</span>
${post.ageText ? `<span class="sep">·</span><span>${escapeText(post.ageText)}</span>` : ""}
</div>
<div class="pinned-card-tags" data-post-id="${escapeAttr(post.id)}"></div>
<div class="pinned-card-preview" hidden></div>
</div>
`;
renderTagsInCard(card, post);
// Open post on card click — but ignore clicks on inner controls
card.addEventListener("click", (e) => {
if (e.target.closest(".pinned-card-unpin")) return;
if (e.target.closest(".pinned-card-comments")) return;
if (e.target.closest(".pinned-card-preview")) return;
if (e.target.closest(".pinned-card-trans")) return;
chrome.tabs.create({ url: post.itemUrl || `https://news.ycombinator.com/item?id=${post.id}` });
});
// Toggle translated/original title on badge click
const transEl = card.querySelector(".pinned-card-trans");
const titleTextEl = card.querySelector(".pinned-card-title-text");
if (transEl && titleTextEl) {
let showingTranslated = true;
transEl.addEventListener("click", (e) => {
e.stopPropagation();
showingTranslated = !showingTranslated;
titleTextEl.textContent = showingTranslated
? transEl.dataset.translated
: transEl.dataset.original;
transEl.style.opacity = showingTranslated ? "" : "0.55";
transEl.title = showingTranslated ? "Click to see original" : "Click to see translation";
});
}
// Unpin handler
card.querySelector(".pinned-card-unpin").addEventListener("click", async (e) => {
e.stopPropagation();
await removePin(post.id);
chrome.tabs.query({ url: "https://news.ycombinator.com/*" }, (tabs) => {
tabs.forEach((tab) => {
if (tab.id != null) {
chrome.tabs.sendMessage(tab.id, { type: "LENS_PIN_CHANGED", postId: post.id, pinned: false }).catch(() => {});
}
});
});
});
// Card-level collapse chevron
card.querySelector(".card-chevron").addEventListener("click", async (e) => {
e.stopPropagation();
const nowCollapsed = card.classList.toggle("card-collapsed");
await setCardCollapsed(post.id, nowCollapsed);
const ch = card.querySelector(".card-chevron");
if (ch) ch.title = nowCollapsed ? tr("pinned.expand") : tr("pinned.collapse");
});
// ── Comment preview on hover ────
const commentsEl = card.querySelector(".pinned-card-comments");
const previewEl = card.querySelector(".pinned-card-preview");
if (commentsEl && previewEl) {
let openTimer = null;
let closeTimer = null;
const openPreview = async () => {
clearTimeout(closeTimer);
if (!previewEl.hidden) return;
previewEl.hidden = false;
if (!previewEl.dataset.loaded) {
previewEl.innerHTML = `<div class="preview-loading">${escapeText(tr("comments.loading"))}</div>`;
try {
const comments = await loadComments(post.id);
renderCommentsPreview(previewEl, comments, post);
previewEl.dataset.loaded = "1";
} catch (err) {
previewEl.innerHTML = `<div class="preview-error">${escapeText(tr("comments.error"))}</div>`;
}
}
};
const closePreview = () => {
clearTimeout(openTimer);
previewEl.hidden = true;
};
commentsEl.addEventListener("mouseenter", () => {
clearTimeout(closeTimer);
openTimer = setTimeout(openPreview, 220);
});
// Once open, keep open while mouse is over the preview itself
previewEl.addEventListener("mouseenter", () => clearTimeout(closeTimer));
card.addEventListener("mouseleave", () => {
clearTimeout(openTimer);
closeTimer = setTimeout(closePreview, 280);
});
// Tap on touch: toggle
commentsEl.addEventListener("click", (e) => {
e.stopPropagation();
if (previewEl.hidden) openPreview();
else closePreview();
});
}
return card;
}
async function loadComments(postId) {
// Try cache first
const cached = await getCachedComments(postId);
if (cached) return cached;
const fresh = await fetchTopComments(postId, 5);
await setCachedComments(postId, fresh);
return fresh;
}
async function renderCommentsPreview(container, comments, post) {
if (!comments || comments.length === 0) {
container.innerHTML = `<div class="preview-empty">${escapeText(tr("comments.empty"))}</div>`;
return;
}
// Build snippets in plain text once
const items = comments.map((c) => ({
raw: c,
original: commentToPlainText(c.text, 220),
translated: null,
sourceLang: null
}));
// Optional translation pass
const targetLang = resolveTargetLang();
const doTranslate = settings.autoTranslate && targetLang && isTranslatorAvailable();
if (doTranslate) {
container.innerHTML = `<div class="preview-loading">${escapeText(tr("page.translating"))}</div>`;
await Promise.all(items.map(async (it) => {
const result = await translateText(it.original, targetLang);
if (result && result.translated) {
it.translated = result.translated;
it.sourceLang = result.sourceLang;
}
}));
}
// Build DOM
const wrap = document.createElement("div");
wrap.className = "cm-list";
items.forEach((it) => {
const c = it.raw;
const div = document.createElement("div");
div.className = "cm-item";
const showTranslated = !!it.translated;
const initialText = showTranslated ? it.translated : it.original;
div.innerHTML = `
<div class="cm-head">
<span class="cm-author">${escapeText(c.by)}</span>
<span class="cm-sep">·</span>
<span class="cm-time">${escapeText(relativeTime(c.time))}</span>
</div>
<div class="cm-text"></div>
${c.replies > 0 ? `<span class="cm-replies">↳ ${c.replies} ${escapeText(c.replies === 1 ? tr("comments.reply") : tr("comments.replies"))}</span>` : ""}
`;
div.querySelector(".cm-text").textContent = initialText;
// Add translate indicator if we have a translation
if (it.translated) {
const head = div.querySelector(".cm-head");
const badge = document.createElement("span");
badge.className = "cm-translated";
badge.textContent = `🌐 ${(it.sourceLang || "?").toUpperCase()} → ${targetLang.toUpperCase()}`;
let showingTranslated = true;
badge.title = "Click to see original";
badge.addEventListener("click", (e) => {
e.stopPropagation();
showingTranslated = !showingTranslated;
div.querySelector(".cm-text").textContent = showingTranslated ? it.translated : it.original;
badge.title = showingTranslated ? "Click to see original" : "Click to see translation";
badge.style.opacity = showingTranslated ? "" : "0.55";
});
head.appendChild(badge);
}
wrap.appendChild(div);
});
// "View all" link
const moreA = document.createElement("a");
moreA.className = "cm-all";
moreA.href = post.itemUrl || `https://news.ycombinator.com/item?id=${post.id}`;
moreA.target = "_blank";
moreA.rel = "noopener noreferrer";
moreA.textContent = tr("comments.viewAll", { count: post.comments || 0 });
container.innerHTML = "";
container.appendChild(wrap);
container.appendChild(moreA);
}
function resolveTargetLang() {
if (settings.translateTo) return settings.translateTo;
const nav = (navigator.language || "en").split("-")[0].toLowerCase();
return nav;
}
function escapeText(s) {
return String(s).replace(/[&<>"]/g, (c) => ({"&":"&","<":"<",">":">",'"':"""})[c]);
}
// ── Tag rendering / editing on pinned cards ─────────────
function renderTagsInCard(card, post) {
const wrap = card.querySelector(".pinned-card-tags");
if (!wrap) return;
wrap.innerHTML = "";
const tags = Array.isArray(post.tags) ? post.tags : [];
tags.forEach((tag) => wrap.appendChild(buildTagChip(post, tag)));
// "+ tag" button
const addBtn = document.createElement("button");
addBtn.type = "button";
addBtn.className = "tag-add-btn";
addBtn.textContent = tr("pinned.addTag");
addBtn.addEventListener("click", (e) => {
e.stopPropagation();
openTagInput(card, post);
});
wrap.appendChild(addBtn);
}
function buildTagChip(post, tag) {
const chip = document.createElement("span");
chip.className = "tag-chip";
chip.title = tr("pinned.removeTag");
chip.style.setProperty("--tag-hue", tagHue(tag));
chip.innerHTML = `<span class="tag-text">${escapeText(tag)}</span><span class="tag-x">×</span>`;
chip.addEventListener("click", async (e) => {
e.stopPropagation();
const next = (post.tags || []).filter((t) => t !== tag);
await setPinnedTags(post.id, next);
post.tags = next;
chip.remove();
broadcastTagsChanged(post.id, next);
});
return chip;
}
async function openTagInput(card, post) {
const wrap = card.querySelector(".pinned-card-tags");
if (!wrap) return;
if (wrap.querySelector(".tag-input-row")) return; // already open
const row = document.createElement("span");
row.className = "tag-input-row";
const input = document.createElement("input");
input.type = "text";
input.className = "tag-input";
input.placeholder = tr("pinned.tagPlaceholder");
input.maxLength = 30;
const suggBox = document.createElement("div");
suggBox.className = "tag-suggestions";
suggBox.hidden = true;
row.appendChild(input);
row.appendChild(suggBox);
// Remove the "+ tag" button while editing
const addBtn = wrap.querySelector(".tag-add-btn");
if (addBtn) addBtn.remove();
wrap.appendChild(row);
input.focus();
const recent = await getRecentTags();
const refreshSuggestions = () => {
const q = normalizeTag(input.value);
const existing = new Set((post.tags || []).map(normalizeTag));
const candidates = recent.filter((t) => !existing.has(t) && (q ? t.includes(q) : true)).slice(0, 6);
if (candidates.length === 0) {
suggBox.hidden = true;
suggBox.innerHTML = "";
return;
}
suggBox.hidden = false;
suggBox.innerHTML = candidates.map((t) =>
`<button type="button" class="tag-sugg" data-tag="${escapeAttr(t)}" style="--tag-hue:${tagHue(t)}">${escapeText(t)}</button>`
).join("");
suggBox.querySelectorAll(".tag-sugg").forEach((b) => {
b.addEventListener("click", (e) => {
e.stopPropagation();
commitTag(b.dataset.tag);
});
});
};
const commitTag = async (raw) => {
const t = normalizeTag(raw);
if (!t) { closeInput(); return; }
const next = Array.from(new Set([...(post.tags || []), t]));
await setPinnedTags(post.id, next);
await pushRecentTag(t);
post.tags = next;
closeInput();
renderTagsInCard(card, post);
broadcastTagsChanged(post.id, next);
};
const closeInput = () => {
row.remove();
// Re-add the "+ tag" button
const newAdd = document.createElement("button");
newAdd.type = "button";
newAdd.className = "tag-add-btn";
newAdd.textContent = tr("pinned.addTag");
newAdd.addEventListener("click", (e) => { e.stopPropagation(); openTagInput(card, post); });
wrap.appendChild(newAdd);
};
input.addEventListener("input", refreshSuggestions);
input.addEventListener("keydown", (e) => {
if (e.key === "Enter") { e.preventDefault(); commitTag(input.value); }
else if (e.key === "Escape") { e.preventDefault(); closeInput(); }
});
input.addEventListener("blur", () => {
setTimeout(() => {
if (document.activeElement && document.activeElement.closest(".tag-suggestions")) return;
closeInput();
}, 150);
});
refreshSuggestions();
}
function broadcastTagsChanged(postId, tags) {
chrome.tabs.query({ url: "https://news.ycombinator.com/*" }, (tabs) => {
tabs.forEach((tab) => {
if (tab.id != null) {
chrome.tabs.sendMessage(tab.id, { type: "LENS_TAGS_CHANGED", postId, tags }).catch(() => {});
}
});
});
}
// ── Radar ─────────────────────────────────────────────────
let activeRadarCat = "top";
let radarLoading = false;
function initRadar() {
const tabs = document.querySelectorAll(".radar-tab");
tabs.forEach((btn) => {
btn.addEventListener("click", async () => {
if (radarLoading) return;
activeRadarCat = btn.dataset.cat;
tabs.forEach((b) => b.classList.toggle("active", b === btn));
await loadRadar(activeRadarCat);
});
});
const refreshBtn = $("radar-refresh");
if (refreshBtn) {
refreshBtn.hidden = false;
refreshBtn.addEventListener("click", () => loadRadar(activeRadarCat, true));
}
// Initial load
loadRadar(activeRadarCat);
}
async function loadRadar(category, force = false) {
const container = $("radar-content");
if (!container || radarLoading) return;
radarLoading = true;
const statusEl = $("radar-status");
if (statusEl) statusEl.textContent = tr("page.translating") /* reuse "loading-like" */ || "";
// Show shimmer skeleton on first load or forced refresh
const store = await getRadarStore();
const cached = (store.lists[category] || []).map((id) => store.byPost[id]).filter(Boolean);
if (force || cached.length === 0) {
container.innerHTML = renderSkeleton();
} else {
// Render cached immediately, refresh in background
renderRadarCards(container, await sortAndFilter(cached));
}
try {
const ids = await fetchListIds(category);
const items = await fetchItemsBatch(ids, 30);
// Annotate type before storing
items.forEach((it) => {
it.type = detectItemType(it.title);
});
const updatedStore = await updateRadarSnapshots(category, items);
const list = (updatedStore.lists[category] || [])
.map((id) => updatedStore.byPost[id])
.filter(Boolean);
renderRadarCards(container, await sortAndFilter(list));
if (statusEl) {
const now = new Date();
statusEl.textContent = `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}`;
}
} catch (e) {
console.warn("[HN Radar] load failed:", e);
if (cached.length === 0) {
container.innerHTML = `<div class="radar-error">${escapeText(tr("comments.error") || "Failed to load")}</div>`;
}
} finally {
radarLoading = false;
}
}
async function sortAndFilter(posts) {
const muted = (await getMutedDomains()).map((d) => d.toLowerCase());
const filtered = posts.filter((p) => {
if (!p.url) return true; // Ask HN / Show HN without URL never filtered
const dom = extractDomain(p.url).toLowerCase();
return !muted.some((m) => dom === m || dom.endsWith("." + m));
});
return filtered
.map((p) => {
const { current, delta } = computeVelocityDetail(p);
return { ...p, _velocity: current, _velocityDelta: delta };
})
.sort((a, b) => b._velocity - a._velocity)
.slice(0, 15);
}
function renderSkeleton() {
return `
<div class="radar-list">
<div class="radar-skeleton"></div>
<div class="radar-skeleton"></div>
<div class="radar-skeleton"></div>
<div class="radar-skeleton"></div>
</div>
`;
}
function renderRadarCards(container, items) {
container.innerHTML = "";
if (!items || items.length === 0) {
container.innerHTML = `<div class="radar-empty">${escapeText(tr("radar.empty"))}</div>`;
return;
}
const list = document.createElement("div");
list.className = "radar-list";
items.forEach((post) => list.appendChild(buildRadarCard(post)));
container.appendChild(list);
// Async enrichment: OP active badge (top 10 only to keep API calls reasonable)
enrichOPActive(items.slice(0, 10));
}
async function enrichOPActive(items) {
for (const post of items) {
if (!post.by || !Array.isArray(post.kids) || post.kids.length === 0) continue;
let active = await getCachedOPActive(post.id);
if (active === undefined) {
try {
active = await checkOPActive(post, 8);
await setCachedOPActive(post.id, active);
} catch {
continue;
}
}
if (active) {
const card = document.querySelector(`.radar-card[data-id="${post.id}"]`);
if (!card) continue;
const flagsEl = card.querySelector(".radar-card-flags");
if (flagsEl && !flagsEl.querySelector(".op-active")) {
const flag = document.createElement("span");
flag.className = "op-active";
flag.textContent = tr("radar.opActive");
flag.title = tr("radar.opActiveTip");
flagsEl.appendChild(flag);
}
}
}
}
function buildRadarCard(post) {
const card = document.createElement("div");
card.className = "radar-card";
card.dataset.id = post.id;
card.dataset.author = post.by || "";
const typeBadge = post.type
? `<span class="radar-card-type ${escapeAttr(post.type)}">${escapeText(post.type)}</span>`
: "";
const domain = extractDomain(post.url) || "news.ycombinator.com";
const ageSeconds = Math.max(1, Math.floor(Date.now() / 1000 - (post.time || 0)));
const ageText = formatRelativeShort(ageSeconds);
const velocity = Math.round(post._velocity || 0);
const velTier = velocity >= 60 ? "hot" : velocity >= 20 ? "warm" : "cool";
const delta = post._velocityDelta;
// Velocity delta indicator
let deltaHtml = "";
if (delta != null && Math.abs(delta) >= 2) {
const cls = delta > 0 ? "delta-up" : "delta-down";
const sym = delta > 0 ? "▲" : "▼";
deltaHtml = `<span class="vel-delta ${cls}">${sym} ${Math.abs(delta)}</span>`;
}
// Comment war indicator
const comments = post.descendants || 0;
let commentTag = "";
if (comments >= 400) {
commentTag = `<span class="cm-war heated">🔥 ${escapeText(tr("radar.heated"))}</span>`;
} else if (comments >= 150) {
commentTag = `<span class="cm-war active">💬 ${escapeText(tr("radar.active"))}</span>`;
}
card.innerHTML = `
<div class="radar-card-row">
<div class="radar-card-main">
<div class="radar-card-title">${typeBadge}${escapeText(post.title || "")}</div>
<div class="radar-card-meta">
<span class="radar-domain">${escapeText(domain)}</span>
<span class="sep">·</span>
<span>${post.score || 0} ${escapeText(tr("pinned.pointsUnit"))}</span>
<span class="sep">·</span>
<span>${comments} ${escapeText(tr("pinned.commentsUnit"))}</span>
<span class="sep">·</span>
<span>${escapeText(ageText)}</span>
${commentTag}
</div>
<div class="radar-card-flags"></div>
</div>
<div class="radar-card-side">
<div class="radar-velocity tier-${velTier}" title="${escapeAttr(tr("radar.velocityTip"))}">
<span class="vel-arrow">↑</span>
<span class="vel-num">${velocity}</span>
${deltaHtml}
</div>
<button class="radar-card-pin" type="button" title="Pin">📌</button>
</div>
</div>
`;
// Open post on card click
card.addEventListener("click", (e) => {
if (e.target.closest(".radar-card-pin")) return;
chrome.tabs.create({ url: `https://news.ycombinator.com/item?id=${post.id}` });
});
// Pin handler
card.querySelector(".radar-card-pin").addEventListener("click", async (e) => {
e.stopPropagation();
const map = await getPinnedMap();
const id = String(post.id);
if (map[id]) return; // already pinned
map[id] = {
id, title: post.title || "", url: post.url || `https://news.ycombinator.com/item?id=${post.id}`,
domain: extractDomain(post.url) || "news.ycombinator.com",
score: post.score || 0, comments: post.descendants || 0, author: post.by || "",
ageText, itemUrl: `https://news.ycombinator.com/item?id=${post.id}`,
type: post.type || "", pinnedAt: Date.now(), tags: []
};
await setToStorage("pinned", map);
e.currentTarget.classList.add("pinned-flash");
e.currentTarget.textContent = "✓";
setTimeout(() => {
e.currentTarget.classList.remove("pinned-flash");
e.currentTarget.textContent = "📌";
}, 1200);
// Refresh pinned section + notify HN tab
renderPinned();
chrome.tabs.query({ url: "https://news.ycombinator.com/*" }, (tabs) => {
tabs.forEach((tab) => {
if (tab.id != null) {
chrome.tabs.sendMessage(tab.id, { type: "LENS_PIN_CHANGED", postId: post.id, pinned: true }).catch(() => {});
}
});
});
});
return card;
}
function formatRelativeShort(seconds) {
if (seconds < 60) return `${seconds}s`;
if (seconds < 3600) return `${Math.floor(seconds / 60)}m`;
if (seconds < 86400) return `${Math.floor(seconds / 3600)}h`;
return `${Math.floor(seconds / 86400)}d`;
}
function escapeAttr(s) {
return String(s).replace(/[^a-z0-9_-]/gi, "");
}
// ═══════════════════════════════════════════════════════════
// Phase 3a — Watchlist (rules + bell + dropdown + form)
// ═══════════════════════════════════════════════════════════
let _editingRuleId = null;
async function initWatch() {
await renderWatchRules();
await refreshBellBadge();
wireBell();
wireWatchForm();
// Watcher lifecycle: start now, watch storage for changes from other contexts
startWatcher();
onUnreadChange(() => refreshBellBadge(true));
chrome.storage.onChanged.addListener((changes, area) => {
if (area !== "local") return;
if (changes.watchRules) renderWatchRules();
if (changes.watchUnread) refreshBellBadge();
});
}
// ── Bell badge + dropdown ──
async function refreshBellBadge(animate = false) {
const unread = await getWatchUnread();
const btn = $("bell-btn");
const badge = $("bell-badge");
if (!btn || !badge) return;
const n = unread.length;
if (n > 0) {
badge.hidden = false;
badge.textContent = n > 99 ? "99+" : String(n);
btn.classList.add("has-unread");
if (animate) {
btn.classList.remove("pulse");
void btn.offsetWidth; // force reflow to restart animation
btn.classList.add("pulse");
}
} else {
badge.hidden = true;
btn.classList.remove("has-unread");
}
// If dropdown is open, also re-render its contents
if ($("bell-dropdown")?.classList.contains("open")) {
renderBellDropdown(unread);
}
}
function wireBell() {
const btn = $("bell-btn");
const dropdown = $("bell-dropdown");
const markAll = $("bell-mark-all");
const manage = $("bell-manage");
if (!btn || !dropdown) return;
btn.addEventListener("click", async (e) => {
e.stopPropagation();
const isOpen = dropdown.classList.contains("open");
if (isOpen) {
dropdown.classList.remove("open");
dropdown.setAttribute("aria-hidden", "true");
return;
}
const unread = await getWatchUnread();
renderBellDropdown(unread);
dropdown.classList.add("open");
dropdown.setAttribute("aria-hidden", "false");
});
markAll?.addEventListener("click", async () => {
await clearWatchUnread();
await refreshBellBadge();
renderBellDropdown([]);
});
manage?.addEventListener("click", () => {
dropdown.classList.remove("open");
dropdown.setAttribute("aria-hidden", "true");
const panel = $("watch-panel");
panel?.classList.remove("collapsed");
panel?.scrollIntoView({ behavior: "smooth", block: "start" });
});
}
function renderBellDropdown(unread) {
const body = $("bell-dropdown-body");
if (!body) return;
// CTA inline at the very top of the expanded bell — brand-anchor only.
// The rotating pitches happen on the sticky footer; showing them here
// too would be redundant (same message on screen twice). The bell stays
// as a calm, static NODUS-AI identity that's seen every time the user
// checks notifications.
const ctaHtml = `