-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
694 lines (632 loc) · 27.7 KB
/
Copy pathscript.js
File metadata and controls
694 lines (632 loc) · 27.7 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
const navToggle = document.querySelector("[data-menu-toggle]");
const navLinks = document.querySelector("[data-nav-links]");
if (navToggle && navLinks) {
navToggle.addEventListener("click", () => {
const isOpen = navLinks.classList.toggle("is-open");
navToggle.setAttribute("aria-expanded", String(isOpen));
});
}
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("is-visible");
observer.unobserve(entry.target);
}
});
}, { threshold: 0.12 });
document.querySelectorAll(".reveal").forEach((element) => observer.observe(element));
const publicationGrid = document.querySelector("[data-publication-grid]");
const mediaGrid = document.querySelector("[data-media-grid]");
const ALLOWED_PUBLICATION_URLS = new Set([
"https://thenitishkr.substack.com/p/we-record-everything-we-remember",
"https://thenitishkr.substack.com/p/india-arrested-the-operators-but",
"https://thenitishkr.substack.com/p/india-followed-the-money-i-followed",
"https://thenitishkr.medium.com/india-followed-the-money-he-followed-the-data-106c0fa40190",
"https://medium.com/@thenitishkr/india-followed-the-money-he-followed-the-data-106c0fa40190"
]);
const TRUSTED_PUBLICATIONS = [
{
source: "Substack",
title: "We Record Everything. We Remember Almost Nothing.",
link: "https://thenitishkr.substack.com/p/we-record-everything-we-remember",
date: new Date("2026-06-03T06:09:01+05:30"),
summary: "A cybersecurity researcher took the question to the Supreme Court. On 19 May 2026, the Court handed it to the government. The question is bigger than either of them.",
image: "https://substackcdn.com/image/fetch/$s_!1nrT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F15cbdc89-e59d-420b-9738-0248865e73b7_1536x1024.png"
},
{
source: "Substack",
title: "India Arrested the Operators. But Did It Miss the Architecture?",
link: "https://thenitishkr.substack.com/p/india-arrested-the-operators-but",
date: new Date("2026-05-24T05:21:28+05:30"),
summary: "Supreme Court asked MeitY to examine a PIL raising questions about Chinese-linked cyber networks, data extraction, and India's unfinished cyber investigation.",
image: "https://substackcdn.com/image/fetch/$s_!TAZa!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7349c51f-1210-405a-9be7-7c5e8e586296_1536x1024.png"
},
{
source: "Substack",
title: "India Followed the Money. I Followed the Data.",
link: "https://thenitishkr.substack.com/p/india-followed-the-money-i-followed",
date: new Date("2026-05-23T05:29:41+05:30"),
summary: "Supreme Court sent the cyber-security petition to MeitY. The next submission raises a bigger question about India's data-layer national security risk.",
image: "https://substackcdn.com/image/fetch/$s_!mWru!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbe7e4f54-8ce8-46f2-affd-45410e5b3cd2_1536x1024.png"
},
{
source: "Medium",
title: "India Followed the Money. He Followed the Data.",
link: "https://thenitishkr.medium.com/india-followed-the-money-he-followed-the-data-106c0fa40190",
date: new Date("2026-06-03T00:00:00+05:30"),
summary: "A public-interest record on money trails, data trails, and the evidence architecture behind constitutional accountability.",
image: "https://cdn-images-1.medium.com/max/1024/1*IIE11lc9f6FumGfDKroiIQ.png"
}
];
function escapeHtml(value) {
return String(value || "").replace(/[&<>"']/g, (char) => ({
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
})[char]);
}
function textFromHtml(value) {
const template = document.createElement("template");
template.innerHTML = value || "";
return (template.content.textContent || "").replace(/\s+/g, " ").trim();
}
function firstImageFromHtml(value) {
const match = String(value || "").match(/<img[^>]+src=["']([^"']+)["']/i);
return match ? match[1] : "";
}
function metaImageFromHtml(value, baseUrl) {
const html = String(value || "");
const patterns = [
/<meta[^>]+property=["']og:image["'][^>]+content=["']([^"']+)["'][^>]*>/i,
/<meta[^>]+name=["']twitter:image["'][^>]+content=["']([^"']+)["'][^>]*>/i,
/<meta[^>]+content=["']([^"']+)["'][^>]+property=["']og:image["'][^>]*>/i,
/<meta[^>]+content=["']([^"']+)["'][^>]+name=["']twitter:image["'][^>]*>/i
];
for (const pattern of patterns) {
const match = html.match(pattern);
if (match && match[1]) {
try { return new URL(match[1], baseUrl).href; } catch (_) { return match[1]; }
}
}
return "";
}
function optimizeImageUrl(url) {
const value = String(url || "");
if (!value) return value;
return value
.replace(/width-1200,height-630/g, "width-640,height-360")
.replace(/fit-in\/1200x675/g, "fit-in/640x360")
.replace(/\/jpg\/large\/high/g, "/jpg/medium/high")
.replace(/w=1200/g, "w=640");
}
function normalizeItem(item, source) {
const content = item.content || item.description || "";
const remoteImage = item.thumbnail || item.enclosure?.link || firstImageFromHtml(content);
return {
source,
title: textFromHtml(item.title || "Untitled"),
link: item.link || item.guid || "#",
date: item.pubDate ? new Date(item.pubDate) : null,
summary: textFromHtml(content).slice(0, 180),
image: remoteImage ? optimizeImageUrl(remoteImage) : "/assets/record.webp"
};
}
function canonicalPublicationUrl(url) {
try {
const parsed = new URL(url);
parsed.search = "";
parsed.hash = "";
return parsed.href.replace(/\/$/, "");
} catch (_) {
return String(url || "").replace(/[?#].*$/, "").replace(/\/$/, "");
}
}
function updateItemListSchema(id, name, items, mapper) {
const element = document.getElementById(id);
if (!element) return;
element.textContent = JSON.stringify({
"@context": "https://schema.org",
"@type": "ItemList",
name,
itemListElement: items.map((item, index) => ({
"@type": "ListItem",
position: index + 1,
item: mapper(item)
}))
});
}
function readXmlText(node, selector) {
return node.querySelector(selector)?.textContent?.trim() || "";
}
function parseRss(xmlText, source) {
const xml = new DOMParser().parseFromString(xmlText, "application/xml");
return [...xml.querySelectorAll("item")].map((item) => {
const content = readXmlText(item, "encoded") || readXmlText(item, "description");
const enclosure = item.querySelector("enclosure[url]")?.getAttribute("url") || "";
const media = item.querySelector("thumbnail, content[url]")?.getAttribute("url") || "";
return normalizeItem({
title: readXmlText(item, "title"),
link: readXmlText(item, "link"),
guid: readXmlText(item, "guid"),
pubDate: readXmlText(item, "pubDate") || readXmlText(item, "date"),
description: content,
thumbnail: media || enclosure || firstImageFromHtml(content)
}, source);
});
}
async function fetchWithTimeout(url, timeout = 9000) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeout);
try {
return await fetch(url, { signal: controller.signal });
} finally {
clearTimeout(timer);
}
}
async function loadFeed(feed) {
const freshFeedUrl = feed.url + (feed.url.includes("?") ? "&" : "?") + "t=" + Date.now();
const jsonResponse = await fetchWithTimeout("https://api.rss2json.com/v1/api.json?rss_url=" + encodeURIComponent(freshFeedUrl), 12000);
if (jsonResponse.ok) {
const data = await jsonResponse.json();
const items = (data.items || []).map((item) => normalizeItem(item, feed.source));
if (items.length) return items;
}
const rawUrls = [
"https://api.allorigins.win/raw?url=" + encodeURIComponent(freshFeedUrl),
"https://api.codetabs.com/v1/proxy?quest=" + encodeURIComponent(freshFeedUrl)
];
for (const url of rawUrls) {
const response = await fetchWithTimeout(url, 9000);
if (!response.ok) continue;
const items = parseRss(await response.text(), feed.source);
if (items.length) return items;
}
return [];
}
function renderPagedArchive(container, items, renderCard, options = {}) {
const featured = items.slice(0, 4);
if (container.dataset.archiveMode === "compact") {
container.innerHTML = `<div class="archive-featured">${featured.map((item, index) => renderCard(item, index)).join("")}</div>`;
if (options.onPageRender) options.onPageRender(featured, 0);
return;
}
const remaining = items.slice(4);
const perPage = options.perPage || 4;
const pageCount = Math.ceil(remaining.length / perPage);
container.innerHTML = `
<div class="archive-featured">${featured.map((item, index) => renderCard(item, index)).join("")}</div>
${remaining.length ? '<div class="archive-sequence" data-archive-sequence></div>' : ""}
${pageCount > 1 ? `<div class="archive-pagination" data-archive-pagination aria-label="${escapeHtml(options.label || "Archive pages")}"></div>` : ""}
`;
const sequence = container.querySelector("[data-archive-sequence]");
const pagination = container.querySelector("[data-archive-pagination]");
const renderPage = (page) => {
if (!sequence) return;
const start = (page - 1) * perPage;
const pageItems = remaining.slice(start, start + perPage);
sequence.innerHTML = pageItems.map((item, index) => renderCard(item, start + index + 4)).join("");
if (pagination) {
pagination.querySelectorAll("button").forEach((button) => {
button.classList.toggle("is-active", Number(button.dataset.page) === page);
});
}
if (options.onPageRender) options.onPageRender(pageItems, start + 4);
};
if (pagination) {
pagination.innerHTML = Array.from({ length: pageCount }, (_, index) => {
const page = index + 1;
return `<button type="button" data-page="${page}" aria-label="Show page ${page}">${page}</button>`;
}).join("");
pagination.addEventListener("click", (event) => {
const button = event.target.closest("button[data-page]");
if (!button) return;
renderPage(Number(button.dataset.page));
});
}
renderPage(1);
}
function renderPublications(items) {
if (!publicationGrid) return;
if (!items.length) {
publicationGrid.innerHTML = '<div class="publication-status">Unable to load live publications right now. Substack and Medium feeds remain linked from the footer.</div>';
publicationGrid.classList.add("is-loaded");
return;
}
const renderCard = (item, index) => {
const date = item.date && !Number.isNaN(item.date.valueOf())
? item.date.toLocaleDateString("en-IN", { day: "numeric", month: "short", year: "numeric" })
: "Latest";
return `
<article class="publication-card reveal is-visible">
<a href="${escapeHtml(item.link)}" target="_blank" rel="noopener">
<img src="${escapeHtml(item.image)}" alt="${escapeHtml(item.title)}" loading="lazy" decoding="async" width="640" height="360" sizes="(max-width: 760px) 92vw, (max-width: 1100px) 45vw, 320px" fetchpriority="low">
</a>
<div class="publication-body">
<span class="publication-meta">${escapeHtml(item.source)} | ${escapeHtml(date)}</span>
<h3><a href="${escapeHtml(item.link)}" target="_blank" rel="noopener">${escapeHtml(item.title)}</a></h3>
<p>${escapeHtml(item.summary)}</p>
</div>
</article>
`;
};
renderPagedArchive(publicationGrid, items, renderCard, {
label: "Substack and Medium archive pages",
perPage: 4
});
publicationGrid.classList.add("is-loaded");
updateItemListSchema("publication-schema", "thenitishkr Substack and Medium publications", items, (item) => ({
"@type": "Article",
headline: item.title,
url: item.link,
image: item.image,
datePublished: item.date && !Number.isNaN(item.date.valueOf()) ? item.date.toISOString() : undefined,
author: { "@type": "Person", name: "Nitish Kumar", alternateName: "thenitishkr" },
publisher: { "@type": "Organization", name: item.source }
}));
}
function renderMediaCoverage(items) {
if (!mediaGrid) return;
if (!items.length) {
mediaGrid.innerHTML = '<div class="publication-status">No media coverage items found.</div>';
mediaGrid.classList.add("is-loaded");
return;
}
const renderCard = (item, index) => {
const image = optimizeImageUrl(item.remote_image || item.thumbnail || "/assets/court.webp");
return `
<article class="media-card reveal is-visible">
<a href="${escapeHtml(item.url)}" target="_blank" rel="noopener">
<img data-media-image="${index}" src="${escapeHtml(image)}" alt="${escapeHtml(item.outlet + ': ' + item.headline)}" loading="lazy" decoding="async" width="640" height="360" sizes="(max-width: 760px) 92vw, (max-width: 1100px) 45vw, 320px" fetchpriority="low">
</a>
<div class="media-body">
<span class="media-outlet">${escapeHtml(item.outlet)} | ${escapeHtml(item.source_type || 'Coverage')}</span>
<h3><a href="${escapeHtml(item.url)}" target="_blank" rel="noopener">${escapeHtml(item.headline)}</a></h3>
<p>${escapeHtml(item.summary || '')}</p>
<div class="media-actions">
<a href="${escapeHtml(item.url)}" target="_blank" rel="noopener">Source</a>
<a href="${escapeHtml(item.archive_url || item.url)}" target="_blank" rel="noopener">Archive</a>
</div>
</div>
</article>
`;
};
const resolveVisibleImages = (visibleItems, startIndex) => {
visibleItems.forEach((item, index) => {
if (item.remote_image) return;
const imageIndex = startIndex + index;
resolveMediaImage(item.url).then((image) => {
if (!image) return;
const element = mediaGrid.querySelector(`[data-media-image="${imageIndex}"]`);
if (element) element.src = optimizeImageUrl(image);
});
});
};
renderPagedArchive(mediaGrid, items, renderCard, {
label: "Media coverage archive pages",
perPage: 4,
onPageRender: resolveVisibleImages
});
mediaGrid.classList.add("is-loaded");
resolveVisibleImages(items.slice(0, 4), 0);
updateItemListSchema("media-schema", "thenitishkr media coverage archive", items, (item) => ({
"@type": "NewsArticle",
headline: item.headline,
url: item.url,
image: optimizeImageUrl(item.remote_image || item.thumbnail || undefined),
datePublished: item.published_date,
description: item.summary,
publisher: { "@type": "Organization", name: item.outlet },
author: { "@type": "Organization", name: item.outlet }
}));
}
async function resolveMediaImage(url) {
const proxies = [
"https://api.allorigins.win/raw?url=" + encodeURIComponent(url),
"https://api.codetabs.com/v1/proxy?quest=" + encodeURIComponent(url)
];
for (const proxyUrl of proxies) {
try {
const response = await fetchWithTimeout(proxyUrl, 7000);
if (!response.ok) continue;
const image = metaImageFromHtml(await response.text(), url);
if (image) return image;
} catch (_) {}
}
return "";
}
async function loadMediaCoverage() {
if (!mediaGrid) return;
try {
const response = await fetch("/media-coverage.json?v=20260609media", { cache: "no-store" });
if (!response.ok) throw new Error("media");
renderMediaCoverage(await response.json());
} catch (_) {
mediaGrid.innerHTML = '<div class="publication-status">Unable to load media coverage.</div>';
mediaGrid.classList.add("is-loaded");
}
}
async function loadPublications() {
if (!publicationGrid) return;
if (publicationGrid.dataset.staticPublications === "true") return;
const feeds = [
{ source: "Substack", url: "https://thenitishkr.substack.com/feed" },
{ source: "Medium", url: "https://medium.com/feed/@thenitishkr" }
];
try {
const results = await Promise.allSettled(feeds.map(async (feed) => {
return loadFeed(feed);
}));
const items = results
.filter((result) => result.status === "fulfilled")
.flatMap((result) => result.value)
.filter((item) => ALLOWED_PUBLICATION_URLS.has(canonicalPublicationUrl(item.link)))
.sort((a, b) => (b.date || 0) - (a.date || 0));
const approved = new Map();
[...items, ...TRUSTED_PUBLICATIONS].forEach((item) => {
const key = canonicalPublicationUrl(item.link);
if (!ALLOWED_PUBLICATION_URLS.has(key) || approved.has(key)) return;
approved.set(key, { ...item, image: optimizeImageUrl(item.image) });
});
renderPublications([...approved.values()].sort((a, b) => (b.date || 0) - (a.date || 0)));
} catch (_) {
renderPublications(TRUSTED_PUBLICATIONS.map((item) => ({ ...item, image: optimizeImageUrl(item.image) })));
}
}
function loadWhenNearViewport(element, callback) {
if (!element) return;
let started = false;
const start = () => {
if (started) return;
started = true;
callback();
};
if (!("IntersectionObserver" in window)) {
start();
return;
}
const loader = new IntersectionObserver((entries) => {
if (!entries.some((entry) => entry.isIntersecting)) return;
loader.disconnect();
start();
}, { rootMargin: "600px 0px" });
loader.observe(element);
}
loadPublications();
loadWhenNearViewport(mediaGrid, loadMediaCoverage);
const RELATED_RECORDS = {
"intelligence-ndma-disaster-governance": [
["Case 03", "NITI Aayog / Certification & Funds", "intelligence-niti-aayog-certification-funds"],
["Case 04", "ODF / False Justification", "intelligence-odf-false-justification"],
["Framework", "Article 12 accountability", "article-12"]
],
"intelligence-meity-digital-governance": [
["Framework", "Digital Constitutional Personhood", "digital-constitutional-personhood"],
["Case 12", "SIR / Constitutional Scam Allegation", "intelligence-sir-constitutional-scam"],
["Archive", "DISHA Intelligence Archive", "intelligence"]
],
"intelligence-niti-aayog-certification-funds": [
["Case 04", "ODF / False Justification", "intelligence-odf-false-justification"],
["Case 01", "NDMA / Disaster Governance", "intelligence-ndma-disaster-governance"],
["Framework", "Article 12 accountability", "article-12"]
],
"intelligence-odf-false-justification": [
["Case 03", "NITI Aayog / Certification & Funds", "intelligence-niti-aayog-certification-funds"],
["Case 01", "NDMA / Disaster Governance", "intelligence-ndma-disaster-governance"],
["Archive", "DISHA Intelligence Archive", "intelligence"]
],
"intelligence-sir-constitutional-scam": [
["Case 02", "MeitY / Digital Governance", "intelligence-meity-digital-governance"],
["Framework", "Digital Constitutional Personhood", "digital-constitutional-personhood"],
["Framework", "Article 12 accountability", "article-12"]
]
};
const DEFAULT_RELATED = [
["Archive", "DISHA Intelligence Archive", "intelligence"],
["Framework", "Article 12 accountability", "article-12"],
["System", "DISHA Intelligence Architecture", "disha"]
];
function canonicalUrl() {
const canonical = document.querySelector('link[rel="canonical"]');
if (canonical && canonical.href) return canonical.href;
return window.location.href.replace(/\/index\.html$/, "/").replace(/\.html($|[?#])/, "$1");
}
function pageTitle() {
const ogTitle = document.querySelector('meta[property="og:title"]');
return (ogTitle && ogTitle.content) || document.title.replace(/\s*\|\s*thenitishkr.*$/i, "");
}
function currentSlug() {
const path = window.location.pathname.replace(/\/$/, "");
const slug = path.split("/").pop() || "index";
return slug.replace(/\.html$/, "");
}
function htmlToId(text) {
return text.toLowerCase().replace(/&/g, " and ").replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "").slice(0, 64);
}
function readingMinutes(main) {
const words = (main.textContent || "").trim().split(/\s+/).filter(Boolean).length;
return Math.max(1, Math.ceil(words / 220));
}
function installReadingProgress() {
if (document.querySelector(".reading-progress")) return;
const progress = document.createElement("div");
progress.className = "reading-progress";
progress.setAttribute("aria-hidden", "true");
progress.innerHTML = "<span></span>";
document.body.prepend(progress);
const bar = progress.querySelector("span");
const update = () => {
const max = document.documentElement.scrollHeight - window.innerHeight;
const value = max > 0 ? Math.min(100, Math.max(0, (window.scrollY / max) * 100)) : 0;
bar.style.width = `${value}%`;
};
update();
window.addEventListener("scroll", update, { passive: true });
window.addEventListener("resize", update);
}
function installRecordToolkit() {
const main = document.querySelector("main");
if (!main || document.querySelector(".record-toolkit")) return;
const slug = currentSlug();
const isCase = slug.startsWith("intelligence-") && slug !== "intelligence";
const isRecord = isCase || main.classList.contains("case-study-page") || document.querySelector("#source-pdf, #source-documents");
if (!isRecord) {
installReadingProgress();
return;
}
installReadingProgress();
const headings = Array.from(main.querySelectorAll("h2, h3"))
.filter((heading) => heading.textContent.trim().length > 0)
.slice(0, 9);
headings.forEach((heading) => {
if (!heading.id) heading.id = htmlToId(heading.textContent);
});
const sourceTarget = document.querySelector("#source-pdf, #source-documents, [id*='source']");
const toolkit = document.createElement("section");
toolkit.className = "section record-toolkit";
toolkit.setAttribute("aria-label", "Record reading tools");
const tocLinks = headings.map((heading) => `<a href="#${heading.id}">${escapeHtml(heading.textContent.trim())}</a>`).join("");
toolkit.innerHTML = `
<div class="record-toolkit-grid">
<div class="record-tool-card">
<span class="label">Reader tools</span>
<strong>${readingMinutes(main)} min read</strong>
<p>Follow the record, verify the source path, and cite the page from its canonical address.</p>
</div>
<nav class="record-toc" aria-label="On this record">
<span>On this record</span>
${tocLinks || '<a href="#top">Record overview</a>'}
</nav>
<div class="evidence-badges" aria-label="Evidence framing">
<span>Verified records</span>
<span>Documented allegations</span>
<span>Research analysis</span>
<span>Public commentary</span>
</div>
${sourceTarget ? `<a class="source-jump" href="#${sourceTarget.id}">Jump to source file</a>` : ""}
</div>`;
main.insertBefore(toolkit, main.children[1] || main.firstChild);
}
function installRelatedRecords() {
const slug = currentSlug();
if (!slug.startsWith("intelligence-") || slug === "intelligence" || document.querySelector(".related-records")) return;
const records = RELATED_RECORDS[slug] || DEFAULT_RELATED;
const block = document.createElement("section");
block.className = "section related-records";
block.setAttribute("aria-label", "Related records");
block.innerHTML = `
<p class="eyebrow">Related records</p>
<h2>Continue the evidence path.</h2>
<div class="related-record-grid">
${records.map(([label, title, href]) => `
<a class="related-record-card" href="${href}">
<span>${escapeHtml(label)}</span>
<strong>${escapeHtml(title)}</strong>
</a>`).join("")}
</div>`;
const share = document.querySelector(".share-follow");
if (share) share.parentNode.insertBefore(block, share);
}
function installTrustCitation() {
const slug = currentSlug();
const isRecord = slug.startsWith("intelligence-") && slug !== "intelligence";
if (!isRecord || document.querySelector(".trust-citation")) return;
const title = pageTitle();
const url = canonicalUrl();
const accessed = new Date().toLocaleDateString("en-IN", { year: "numeric", month: "long", day: "numeric" });
const citation = `Nitish Kumar (thenitishkr), "${title}", thenitishkr.in, ${url}, accessed ${accessed}.`;
const block = document.createElement("section");
block.className = "section trust-citation";
block.setAttribute("aria-label", "Trust and citation");
block.innerHTML = `
<div>
<p class="eyebrow">Trust / citation</p>
<h2>Cite this public-interest record.</h2>
<p>This record distinguishes verified source material, documented allegations, research analysis, and public commentary. It is maintained for research, documentation, journalism, and lawful public-interest review.</p>
<p class="citation-text">${escapeHtml(citation)}</p>
</div>
<div class="citation-actions">
<a class="btn ghost" href="https://orcid.org/0009-0004-6840-4463" target="_blank" rel="noopener">ORCID record</a>
<button class="btn" type="button" data-copy-citation="${escapeHtml(citation)}">Copy citation</button>
</div>`;
const related = document.querySelector(".related-records");
const share = document.querySelector(".share-follow");
if (share) share.parentNode.insertBefore(block, share);
else if (related) related.after(block);
}
installRecordToolkit();
installRelatedRecords();
installTrustCitation();
document.querySelectorAll('[data-share-x], [data-share-linkedin]').forEach((link) => {
const url = encodeURIComponent(canonicalUrl());
if (link.hasAttribute('data-share-x')) link.href = `https://x.com/intent/tweet?url=${url}&text=${encodeURIComponent(pageTitle())}`;
if (link.hasAttribute('data-share-linkedin')) link.href = `https://www.linkedin.com/sharing/share-offsite/?url=${url}`;
});
document.querySelectorAll('[data-copy-link], [data-copy-citation]').forEach((button) => {
button.addEventListener('click', async () => {
const original = button.textContent;
const value = button.getAttribute("data-copy-citation") || canonicalUrl();
try {
await navigator.clipboard.writeText(value);
button.textContent = button.hasAttribute("data-copy-citation") ? 'Citation copied' : 'Link copied';
setTimeout(() => { button.textContent = original; }, 1800);
} catch (_) {
button.textContent = 'Copy failed';
setTimeout(() => { button.textContent = original; }, 1800);
}
});
});
function installSwg() {
const ldJsonScripts = document.querySelectorAll('script[type="application/ld+json"]');
let isNewsArticle = false;
for (const script of ldJsonScripts) {
try {
const data = JSON.parse(script.textContent);
const types = [];
const extractTypes = (obj) => {
if (!obj || typeof obj !== "object") return;
if (Array.isArray(obj)) {
obj.forEach(extractTypes);
return;
}
if (obj["@type"]) {
if (Array.isArray(obj["@type"])) {
types.push(...obj["@type"]);
} else {
types.push(obj["@type"]);
}
}
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
extractTypes(obj[key]);
}
}
};
extractTypes(data);
if (types.some(t => ["NewsArticle", "AnalysisNewsArticle", "Article"].includes(t))) {
const path = window.location.pathname.replace(/\/$/, "");
const isStaticIndex = ["", "/about", "/media", "/news", "/editorial-standards", "/fact-check", "/start-here", "/privacy-policy", "/terms", "/intelligence"].includes(path);
if (!isStaticIndex) {
isNewsArticle = true;
break;
}
}
} catch (_) {}
}
if (!isNewsArticle) return;
const script = document.createElement("script");
script.async = true;
script.src = "https://news.google.com/swg/js/v1/swg-basic.js";
document.head.appendChild(script);
self.SWG_BASIC = self.SWG_BASIC || [];
self.SWG_BASIC.push((basicSubscriptions) => {
basicSubscriptions.init({
type: "NewsArticle",
isPartOfType: ["Product"],
isPartOfProductId: "CAowlPvGDA:openaccess",
clientOptions: { theme: "light", lang: "en" },
});
});
}
installSwg();