@jackwener/opencli 0.7.2 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,416 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ import type { IPage } from '../../types.js';
3
+
4
+ // ── Filter value mappings ──────────────────────────────────────────────
5
+
6
+ const EXPERIENCE_LEVELS: Record<string, string> = {
7
+ internship: '1',
8
+ entry: '2',
9
+ 'entry-level': '2',
10
+ associate: '3',
11
+ mid: '4',
12
+ senior: '4',
13
+ 'mid-senior': '4',
14
+ 'mid-senior-level': '4',
15
+ director: '5',
16
+ executive: '6',
17
+ };
18
+
19
+ const JOB_TYPES: Record<string, string> = {
20
+ 'full-time': 'F',
21
+ fulltime: 'F',
22
+ full: 'F',
23
+ 'part-time': 'P',
24
+ parttime: 'P',
25
+ part: 'P',
26
+ contract: 'C',
27
+ temporary: 'T',
28
+ temp: 'T',
29
+ volunteer: 'V',
30
+ internship: 'I',
31
+ other: 'O',
32
+ };
33
+
34
+ const DATE_POSTED: Record<string, string> = {
35
+ any: 'on',
36
+ month: 'r2592000',
37
+ 'past-month': 'r2592000',
38
+ week: 'r604800',
39
+ 'past-week': 'r604800',
40
+ day: 'r86400',
41
+ '24h': 'r86400',
42
+ 'past-24h': 'r86400',
43
+ };
44
+
45
+ const REMOTE_TYPES: Record<string, string> = {
46
+ onsite: '1',
47
+ 'on-site': '1',
48
+ hybrid: '3',
49
+ remote: '2',
50
+ };
51
+
52
+ // ── Helpers ────────────────────────────────────────────────────────────
53
+
54
+ function parseCsvArg(value: unknown): string[] {
55
+ if (value === undefined || value === null || value === '') return [];
56
+ return String(value)
57
+ .split(',')
58
+ .map(item => item.trim())
59
+ .filter(Boolean);
60
+ }
61
+
62
+ function mapFilterValues(input: unknown, mapping: Record<string, string>, label: string): string[] {
63
+ const values = parseCsvArg(input);
64
+ const resolved = values.map(value => {
65
+ const key = value.toLowerCase();
66
+ const mapped = mapping[key];
67
+ if (!mapped) throw new Error(`Unsupported ${label}: ${value}`);
68
+ return mapped;
69
+ });
70
+ return [...new Set(resolved)];
71
+ }
72
+
73
+ function normalizeWhitespace(value: unknown): string {
74
+ return String(value ?? '').replace(/\s+/g, ' ').trim();
75
+ }
76
+
77
+ function decodeLinkedinRedirect(url: string): string {
78
+ if (!url) return '';
79
+ try {
80
+ const parsed = new URL(url);
81
+ if (parsed.pathname === '/redir/redirect/') {
82
+ return parsed.searchParams.get('url') || url;
83
+ }
84
+ } catch {}
85
+ return url;
86
+ }
87
+
88
+ // ── Voyager query builder (runs in Node, NOT inside page.evaluate) ────
89
+
90
+ interface SearchInput {
91
+ keywords: string;
92
+ location: string;
93
+ limit: number;
94
+ start: number;
95
+ companyIds: string[];
96
+ experienceLevels: string[];
97
+ jobTypes: string[];
98
+ datePostedValues: string[];
99
+ remoteTypes: string[];
100
+ }
101
+
102
+ function buildVoyagerSearchQuery(input: SearchInput): string {
103
+ const hasFilters =
104
+ input.companyIds.length ||
105
+ input.experienceLevels.length ||
106
+ input.jobTypes.length ||
107
+ input.datePostedValues.length ||
108
+ input.remoteTypes.length;
109
+
110
+ const parts = [
111
+ 'origin:' + (hasFilters ? 'JOB_SEARCH_PAGE_JOB_FILTER' : 'JOB_SEARCH_PAGE_OTHER_ENTRY'),
112
+ 'keywords:' + input.keywords,
113
+ ];
114
+ if (input.location) {
115
+ parts.push('locationUnion:(seoLocation:(location:' + input.location + '))');
116
+ }
117
+ const filters: string[] = [];
118
+ if (input.companyIds.length) filters.push('company:List(' + input.companyIds.join(',') + ')');
119
+ if (input.experienceLevels.length) filters.push('experience:List(' + input.experienceLevels.join(',') + ')');
120
+ if (input.jobTypes.length) filters.push('jobType:List(' + input.jobTypes.join(',') + ')');
121
+ if (input.datePostedValues.length) filters.push('timePostedRange:List(' + input.datePostedValues.join(',') + ')');
122
+ if (input.remoteTypes.length) filters.push('workplaceType:List(' + input.remoteTypes.join(',') + ')');
123
+ if (filters.length) parts.push('selectedFilters:(' + filters.join(',') + ')');
124
+ parts.push('spellCorrectionEnabled:true');
125
+ return '(' + parts.join(',') + ')';
126
+ }
127
+
128
+ function buildVoyagerUrl(input: SearchInput, offset: number, count: number): string {
129
+ const params = new URLSearchParams({
130
+ decorationId: 'com.linkedin.voyager.dash.deco.jobs.search.JobSearchCardsCollection-220',
131
+ count: String(count),
132
+ q: 'jobSearch',
133
+ });
134
+ const query = encodeURIComponent(buildVoyagerSearchQuery(input))
135
+ .replace(/%3A/gi, ':')
136
+ .replace(/%2C/gi, ',')
137
+ .replace(/%28/gi, '(')
138
+ .replace(/%29/gi, ')');
139
+ return '/voyager/api/voyagerJobsDashJobCards?' + params.toString() + '&query=' + query + '&start=' + offset;
140
+ }
141
+
142
+ // ── Company ID resolution (requires DOM interaction) ──────────────────
143
+
144
+ async function resolveCompanyIds(page: IPage, input: unknown): Promise<string[]> {
145
+ const rawValues = parseCsvArg(input);
146
+ const ids = new Set<string>();
147
+ const names: string[] = [];
148
+
149
+ for (const value of rawValues) {
150
+ if (/^\d+$/.test(value)) ids.add(value);
151
+ else names.push(value);
152
+ }
153
+
154
+ if (!names.length) return [...ids];
155
+
156
+ const resolved = await page.evaluate(`(async () => {
157
+ const targets = ${JSON.stringify(names)};
158
+ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
159
+ const normalize = (v) => (v || '').toLowerCase().replace(/\\s+/g, ' ').trim();
160
+
161
+ // Open "All filters" panel to expose company filter inputs
162
+ const allBtn = [...document.querySelectorAll('button')]
163
+ .find(b => ((b.innerText || '').trim().replace(/\\s+/g, ' ')) === 'All filters');
164
+ if (allBtn) { allBtn.click(); await sleep(300); }
165
+
166
+ const getCompanyMap = () => {
167
+ const map = {};
168
+ for (const el of document.querySelectorAll('input[name="company-filter-value"]')) {
169
+ const text = (el.parentElement?.innerText || el.closest('label')?.innerText || '')
170
+ .replace(/\\s+/g, ' ').trim().replace(/\\s*Filter by.*$/i, '').trim();
171
+ if (text) map[normalize(text)] = el.value;
172
+ }
173
+ return map;
174
+ };
175
+
176
+ const match = (map, name) => {
177
+ const n = normalize(name);
178
+ if (map[n]) return map[n];
179
+ const k = Object.keys(map).find(e => e === n || e.includes(n) || n.includes(e));
180
+ return k ? map[k] : null;
181
+ };
182
+
183
+ const results = {};
184
+ let map = getCompanyMap();
185
+
186
+ for (const name of targets) {
187
+ let found = match(map, name);
188
+ if (!found) {
189
+ const inp = [...document.querySelectorAll('input')]
190
+ .find(el => el.getAttribute('aria-label') === 'Add a company');
191
+ if (inp) {
192
+ inp.focus();
193
+ inp.value = name;
194
+ inp.dispatchEvent(new Event('input', { bubbles: true }));
195
+ inp.dispatchEvent(new KeyboardEvent('keyup', { key: 'Enter', bubbles: true }));
196
+ await sleep(1200);
197
+ map = getCompanyMap();
198
+ found = match(map, name);
199
+ inp.value = '';
200
+ inp.dispatchEvent(new Event('input', { bubbles: true }));
201
+ await sleep(100);
202
+ }
203
+ }
204
+ results[name] = found || null;
205
+ }
206
+ return results;
207
+ })()`);
208
+
209
+ const unresolved: string[] = [];
210
+ for (const name of names) {
211
+ const id = resolved?.[name];
212
+ if (id) ids.add(id);
213
+ else unresolved.push(name);
214
+ }
215
+
216
+ if (unresolved.length) {
217
+ throw new Error(`Could not resolve LinkedIn company filter: ${unresolved.join(', ')}`);
218
+ }
219
+
220
+ return [...ids];
221
+ }
222
+
223
+ // ── Voyager API fetch (runs inside page context for cookie access) ────
224
+
225
+ async function fetchJobCards(
226
+ page: IPage,
227
+ input: SearchInput,
228
+ ): Promise<Array<Record<string, any>>> {
229
+ const MAX_BATCH = 25;
230
+ const allJobs: Array<Record<string, any>> = [];
231
+ let offset = input.start;
232
+
233
+ while (allJobs.length < input.limit) {
234
+ const count = Math.min(MAX_BATCH, input.limit - allJobs.length);
235
+ const apiPath = buildVoyagerUrl(input, offset, count);
236
+
237
+ const batch = await page.evaluate(`(async () => {
238
+ const jsession = document.cookie.split(';').map(p => p.trim())
239
+ .find(p => p.startsWith('JSESSIONID='))?.slice('JSESSIONID='.length);
240
+ if (!jsession) return { error: 'LinkedIn JSESSIONID cookie not found. Please sign in to LinkedIn in the browser.' };
241
+
242
+ const csrf = jsession.replace(/^"|"$/g, '');
243
+ const res = await fetch(${JSON.stringify(apiPath)}, {
244
+ credentials: 'include',
245
+ headers: { 'csrf-token': csrf, 'x-restli-protocol-version': '2.0.0' },
246
+ });
247
+ if (!res.ok) {
248
+ const text = await res.text();
249
+ return { error: 'LinkedIn API error: HTTP ' + res.status + ' ' + text.slice(0, 200) };
250
+ }
251
+ return res.json();
252
+ })()`);
253
+
254
+ if (!batch || batch.error) {
255
+ throw new Error(batch?.error || 'LinkedIn search returned an unexpected response');
256
+ }
257
+
258
+ const elements: any[] = Array.isArray(batch?.elements) ? batch.elements : [];
259
+ if (elements.length === 0) break;
260
+
261
+ for (const element of elements) {
262
+ const card = element?.jobCardUnion?.jobPostingCard;
263
+ if (!card) continue;
264
+
265
+ // Extract job ID from URN fields
266
+ const jobId = [card.jobPostingUrn, card.jobPosting?.entityUrn, card.entityUrn]
267
+ .filter(Boolean)
268
+ .map(s => String(s).match(/(\d+)/)?.[1])
269
+ .find(Boolean) ?? '';
270
+
271
+ // Extract listed date
272
+ const listedItem = (card.footerItems || []).find((i: any) => i?.type === 'LISTED_DATE' && i?.timeAt);
273
+ const listed = listedItem?.timeAt ? new Date(listedItem.timeAt).toISOString().slice(0, 10) : '';
274
+
275
+ allJobs.push({
276
+ title: card.jobPostingTitle || card.title?.text || '',
277
+ company: card.primaryDescription?.text || '',
278
+ location: card.secondaryDescription?.text || '',
279
+ listed,
280
+ salary: card.tertiaryDescription?.text || '',
281
+ url: jobId ? 'https://www.linkedin.com/jobs/view/' + jobId : '',
282
+ });
283
+ }
284
+
285
+ if (elements.length < count) break;
286
+ offset += elements.length;
287
+ }
288
+
289
+ return allJobs.slice(0, input.limit).map((item, index) => ({
290
+ rank: input.start + index + 1,
291
+ ...item,
292
+ }));
293
+ }
294
+
295
+ // ── Job detail enrichment (--details flag) ────────────────────────────
296
+
297
+ async function enrichJobDetails(
298
+ page: IPage,
299
+ jobs: Array<Record<string, any>>,
300
+ ): Promise<Array<Record<string, any>>> {
301
+ const enriched: Array<Record<string, any>> = [];
302
+
303
+ for (let i = 0; i < jobs.length; i++) {
304
+ const job = jobs[i];
305
+ console.error(`[opencli:linkedin] Fetching details ${i + 1}/${jobs.length}: ${job.title}`);
306
+
307
+ if (!job.url) {
308
+ enriched.push({ ...job, description: '', apply_url: '' });
309
+ continue;
310
+ }
311
+
312
+ try {
313
+ await page.goto(job.url);
314
+ await page.wait({ text: 'About the job', timeout: 8 });
315
+
316
+ // Expand "Show more" button if present
317
+ await page.evaluate(`(() => {
318
+ const norm = (v) => (v || '').replace(/\\s+/g, ' ').trim().toLowerCase();
319
+ const section = [...document.querySelectorAll('div, section, article')]
320
+ .find(el => norm(el.querySelector('h1,h2,h3,h4')?.textContent || '') === 'about the job');
321
+ const btn = [...(section?.querySelectorAll('button, a[role="button"]') || [])]
322
+ .find(el => /more/.test(norm(el.textContent || '')) || /more/.test(norm(el.getAttribute('aria-label') || '')));
323
+ if (btn) btn.click();
324
+ })()`);
325
+ await page.wait(1);
326
+
327
+ // Extract description and apply URL
328
+ const detail = await page.evaluate(`(() => {
329
+ const norm = (v) => (v || '').replace(/\\s+/g, ' ').trim();
330
+ // Find the most specific (shortest) container with "About the job" heading
331
+ // Shortest = most specific DOM node, avoiding outer wrappers that include unrelated text
332
+ const candidates = [...document.querySelectorAll('div, section, article')]
333
+ .map(el => ({
334
+ heading: norm(el.querySelector('h1,h2,h3,h4')?.textContent || ''),
335
+ text: norm(el.innerText || ''),
336
+ }))
337
+ .filter(c => c.text && c.heading.toLowerCase() === 'about the job' && c.text.length > 'About the job'.length)
338
+ .sort((a, b) => a.text.length - b.text.length);
339
+
340
+ const description = candidates[0]?.text.replace(/^About the job\\s*/i, '') || '';
341
+ const applyLink = [...document.querySelectorAll('a[href]')]
342
+ .map(a => ({ href: a.href || '', text: norm(a.textContent || ''), aria: norm(a.getAttribute('aria-label') || '') }))
343
+ .find(a => /apply/i.test(a.text) || /apply/i.test(a.aria));
344
+
345
+ return { description, applyUrl: applyLink?.href || '' };
346
+ })()`);
347
+
348
+ enriched.push({
349
+ ...job,
350
+ description: normalizeWhitespace(detail?.description),
351
+ apply_url: decodeLinkedinRedirect(String(detail?.applyUrl ?? '')),
352
+ });
353
+ } catch {
354
+ enriched.push({ ...job, description: '', apply_url: '' });
355
+ }
356
+ }
357
+
358
+ return enriched;
359
+ }
360
+
361
+ // ── CLI registration ──────────────────────────────────────────────────
362
+
363
+ cli({
364
+ site: 'linkedin',
365
+ name: 'search',
366
+ description: 'Search LinkedIn jobs',
367
+ domain: 'www.linkedin.com',
368
+ strategy: Strategy.HEADER,
369
+ browser: true,
370
+ args: [
371
+ { name: 'query', type: 'string', required: true, help: 'Job search keywords' },
372
+ { name: 'location', type: 'string', required: false, help: 'Location text such as San Francisco Bay Area' },
373
+ { name: 'limit', type: 'int', default: 10, help: 'Number of jobs to return (max 100)' },
374
+ { name: 'start', type: 'int', default: 0, help: 'Result offset for pagination' },
375
+ { name: 'details', type: 'bool', default: false, help: 'Include full job description and apply URL (slower)' },
376
+ { name: 'company', type: 'string', required: false, help: 'Comma-separated company names or LinkedIn company IDs' },
377
+ { name: 'experience_level', type: 'string', required: false, help: 'Comma-separated: internship, entry, associate, mid-senior, director, executive' },
378
+ { name: 'job_type', type: 'string', required: false, help: 'Comma-separated: full-time, part-time, contract, temporary, volunteer, internship, other' },
379
+ { name: 'date_posted', type: 'string', required: false, help: 'One of: any, month, week, 24h' },
380
+ { name: 'remote', type: 'string', required: false, help: 'Comma-separated: on-site, hybrid, remote' },
381
+ ],
382
+ columns: ['rank', 'title', 'company', 'location', 'listed', 'salary', 'url'],
383
+ func: async (page, kwargs) => {
384
+ const limit = Math.max(1, Math.min(kwargs.limit ?? 10, 100));
385
+ const start = Math.max(0, kwargs.start ?? 0);
386
+ const includeDetails = Boolean(kwargs.details);
387
+ const location = (kwargs.location ?? '').trim();
388
+ const keywords = String(kwargs.query ?? '').trim();
389
+
390
+ if (!keywords) throw new Error('query is required');
391
+
392
+ const searchParams = new URLSearchParams({ keywords });
393
+ if (location) searchParams.set('location', location);
394
+
395
+ await page.goto(`https://www.linkedin.com/jobs/search/?${searchParams.toString()}`);
396
+ await page.wait({ text: 'Jobs', timeout: 10 });
397
+ const companyIds = await resolveCompanyIds(page, kwargs.company);
398
+
399
+ const input: SearchInput = {
400
+ keywords,
401
+ location,
402
+ limit,
403
+ start,
404
+ companyIds,
405
+ experienceLevels: mapFilterValues(kwargs.experience_level, EXPERIENCE_LEVELS, 'experience_level'),
406
+ jobTypes: mapFilterValues(kwargs.job_type, JOB_TYPES, 'job_type'),
407
+ datePostedValues: mapFilterValues(kwargs.date_posted, DATE_POSTED, 'date_posted'),
408
+ remoteTypes: mapFilterValues(kwargs.remote, REMOTE_TYPES, 'remote'),
409
+ };
410
+
411
+ const data = await fetchJobCards(page, input);
412
+
413
+ if (!includeDetails) return data;
414
+ return enrichJobDetails(page, data);
415
+ },
416
+ });
@@ -0,0 +1,186 @@
1
+ /**
2
+ * Reddit post reader with threaded comment tree.
3
+ *
4
+ * Replaces the original flat read.yaml with recursive comment traversal:
5
+ * - Top-K comments by score at each level
6
+ * - Configurable depth and replies-per-level
7
+ * - Indented output showing conversation threads
8
+ */
9
+ import { cli, Strategy } from '../../registry.js';
10
+
11
+ cli({
12
+ site: 'reddit',
13
+ name: 'read',
14
+ description: 'Read a Reddit post and its comments',
15
+ domain: 'reddit.com',
16
+ strategy: Strategy.COOKIE,
17
+ args: [
18
+ { name: 'post_id', required: true, help: 'Post ID (e.g. 1abc123) or full URL' },
19
+ { name: 'sort', default: 'best', help: 'Comment sort: best, top, new, controversial, old, qa' },
20
+ { name: 'limit', type: 'int', default: 25, help: 'Number of top-level comments' },
21
+ { name: 'depth', type: 'int', default: 2, help: 'Max reply depth (1=no replies, 2=one level of replies, etc.)' },
22
+ { name: 'replies', type: 'int', default: 5, help: 'Max replies shown per comment at each level (sorted by score)' },
23
+ { name: 'max_length', type: 'int', default: 2000, help: 'Max characters per comment body (min 100)' },
24
+ ],
25
+ columns: ['type', 'author', 'score', 'text'],
26
+ func: async (page, kwargs) => {
27
+ const sort = kwargs.sort ?? 'best';
28
+ const limit = Math.max(1, kwargs.limit ?? 25);
29
+ const maxDepth = Math.max(1, kwargs.depth ?? 2);
30
+ const maxReplies = Math.max(1, kwargs.replies ?? 5);
31
+ const maxLength = Math.max(100, kwargs.max_length ?? 2000);
32
+
33
+ await page.goto('https://www.reddit.com');
34
+ await page.wait(2);
35
+
36
+ const data = await page.evaluate(`
37
+ (async function() {
38
+ var postId = ${JSON.stringify(kwargs.post_id)};
39
+ var urlMatch = postId.match(/comments\\/([a-z0-9]+)/);
40
+ if (urlMatch) postId = urlMatch[1];
41
+
42
+ var sort = ${JSON.stringify(sort)};
43
+ var limit = ${limit};
44
+ var maxDepth = ${maxDepth};
45
+ var maxReplies = ${maxReplies};
46
+ var maxLength = ${maxLength};
47
+
48
+ // Request more from API than top-level limit to get inline replies
49
+ // depth param tells Reddit how deep to inline replies vs "more" stubs
50
+ var apiLimit = Math.max(limit * 3, 100);
51
+ var res = await fetch(
52
+ '/comments/' + postId + '.json?sort=' + sort + '&limit=' + apiLimit + '&depth=' + (maxDepth + 1) + '&raw_json=1',
53
+ { credentials: 'include' }
54
+ );
55
+ if (!res.ok) return { error: 'Reddit API returned HTTP ' + res.status };
56
+
57
+ var data;
58
+ try { data = await res.json(); } catch(e) { return { error: 'Failed to parse response' }; }
59
+ if (!Array.isArray(data) || data.length < 2) return { error: 'Unexpected response format' };
60
+
61
+ var results = [];
62
+
63
+ // Post
64
+ var post = data[0] && data[0].data && data[0].data.children && data[0].data.children[0] && data[0].data.children[0].data;
65
+ if (post) {
66
+ var body = post.selftext || '';
67
+ if (body.length > maxLength) body = body.slice(0, maxLength) + '\\n... [truncated]';
68
+ results.push({
69
+ type: 'POST',
70
+ author: post.author || '[deleted]',
71
+ score: post.score || 0,
72
+ text: post.title + (body ? '\\n\\n' + body : '') + (post.url && !post.is_self ? '\\n' + post.url : ''),
73
+ });
74
+ }
75
+
76
+ // Recursive comment walker
77
+ // depth 0 = top-level comments; maxDepth is exclusive,
78
+ // so --depth 1 means top-level only, --depth 2 means one reply level, etc.
79
+ function walkComment(node, depth) {
80
+ if (!node || node.kind !== 't1') return;
81
+ var d = node.data;
82
+ var body = d.body || '';
83
+ if (body.length > maxLength) body = body.slice(0, maxLength) + '...';
84
+
85
+ // Indent prefix: apply to every line so multiline bodies stay aligned
86
+ var indent = '';
87
+ for (var i = 0; i < depth; i++) indent += ' ';
88
+ var prefix = depth === 0 ? '' : indent + '> ';
89
+ var indentedBody = depth === 0
90
+ ? body
91
+ : body.split('\\n').map(function(line) { return prefix + line; }).join('\\n');
92
+
93
+ results.push({
94
+ type: depth === 0 ? 'L0' : 'L' + depth,
95
+ author: d.author || '[deleted]',
96
+ score: d.score || 0,
97
+ text: indentedBody,
98
+ });
99
+
100
+ // Count all available replies (for accurate "more" count)
101
+ var t1Children = [];
102
+ var moreCount = 0;
103
+ if (d.replies && d.replies.data && d.replies.data.children) {
104
+ var children = d.replies.data.children;
105
+ for (var i = 0; i < children.length; i++) {
106
+ if (children[i].kind === 't1') {
107
+ t1Children.push(children[i]);
108
+ } else if (children[i].kind === 'more') {
109
+ moreCount += children[i].data.count || 0;
110
+ }
111
+ }
112
+ }
113
+
114
+ // At depth cutoff: don't recurse, but show all replies as hidden
115
+ if (depth + 1 >= maxDepth) {
116
+ var totalHidden = t1Children.length + moreCount;
117
+ if (totalHidden > 0) {
118
+ var cutoffIndent = '';
119
+ for (var j = 0; j <= depth; j++) cutoffIndent += ' ';
120
+ results.push({
121
+ type: 'L' + (depth + 1),
122
+ author: '',
123
+ score: '',
124
+ text: cutoffIndent + '[+' + totalHidden + ' more replies]',
125
+ });
126
+ }
127
+ return;
128
+ }
129
+
130
+ // Sort by score descending, take top N
131
+ t1Children.sort(function(a, b) { return (b.data.score || 0) - (a.data.score || 0); });
132
+ var toProcess = Math.min(t1Children.length, maxReplies);
133
+ for (var i = 0; i < toProcess; i++) {
134
+ walkComment(t1Children[i], depth + 1);
135
+ }
136
+
137
+ // Show hidden count (skipped replies + "more" stubs)
138
+ var hidden = t1Children.length - toProcess + moreCount;
139
+ if (hidden > 0) {
140
+ var moreIndent = '';
141
+ for (var j = 0; j <= depth; j++) moreIndent += ' ';
142
+ results.push({
143
+ type: 'L' + (depth + 1),
144
+ author: '',
145
+ score: '',
146
+ text: moreIndent + '[+' + hidden + ' more replies]',
147
+ });
148
+ }
149
+ }
150
+
151
+ // Walk top-level comments
152
+ var topLevel = data[1].data.children || [];
153
+ var t1TopLevel = [];
154
+ for (var i = 0; i < topLevel.length; i++) {
155
+ if (topLevel[i].kind === 't1') t1TopLevel.push(topLevel[i]);
156
+ }
157
+
158
+ // Top-level are already sorted by Reddit (sort param), take top N
159
+ for (var i = 0; i < Math.min(t1TopLevel.length, limit); i++) {
160
+ walkComment(t1TopLevel[i], 0);
161
+ }
162
+
163
+ // Count remaining
164
+ var moreTopLevel = topLevel.filter(function(c) { return c.kind === 'more'; })
165
+ .reduce(function(sum, c) { return sum + (c.data.count || 0); }, 0);
166
+ var hiddenTopLevel = Math.max(0, t1TopLevel.length - limit) + moreTopLevel;
167
+ if (hiddenTopLevel > 0) {
168
+ results.push({
169
+ type: '',
170
+ author: '',
171
+ score: '',
172
+ text: '[+' + hiddenTopLevel + ' more top-level comments]',
173
+ });
174
+ }
175
+
176
+ return results;
177
+ })()
178
+ `);
179
+
180
+ if (!data || typeof data !== 'object') throw new Error('Failed to fetch post data');
181
+ if (!Array.isArray(data) && data.error) throw new Error(data.error);
182
+ if (!Array.isArray(data)) throw new Error('Unexpected response');
183
+
184
+ return data;
185
+ },
186
+ });