@xbrowser/reddit 1.0.0

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.
Files changed (2) hide show
  1. package/index.ts +70 -0
  2. package/package.json +20 -0
package/index.ts ADDED
@@ -0,0 +1,70 @@
1
+ import { z } from 'zod';
2
+ import type { XCLIAPI } from '@dyyz1993/xcli-core';
3
+ import { ok, fail } from '@dyyz1993/xcli-core';
4
+
5
+ export default function (xcli: XCLIAPI): void {
6
+ const reddit = xcli.createSite({
7
+ name: 'reddit',
8
+ url: 'https://www.reddit.com',
9
+ description: 'Reddit 图片搜索',
10
+ requiresLogin: false,
11
+ });
12
+
13
+ reddit.command('search-image', {
14
+ description: 'Reddit 图片搜索 - 搜索 Reddit 中的图片帖子',
15
+ scope: 'browser',
16
+ parameters: z.object({
17
+ query: z.string().describe('搜索关键词'),
18
+ limit: z.number().optional().default(10),
19
+ page: z.any().optional(),
20
+ timeout: z.number().optional().default(20000),
21
+ }),
22
+ result: z.any(),
23
+ handler: async (params, ctx) => {
24
+ const page = (params.page as import('playwright').Page)
25
+ || (ctx as Record<string, unknown>).page as import('playwright').Page;
26
+ if (!page) throw new Error('需要浏览器页面');
27
+
28
+ try {
29
+ const url = `https://www.reddit.com/search/?q=${encodeURIComponent(params.query)}&type=image`;
30
+ await page.goto(url, { waitUntil: 'domcontentloaded', timeout: params.timeout });
31
+ await page.waitForTimeout(3000);
32
+ await page.evaluate(() => window.scrollTo(0, document.body.scrollHeight / 2));
33
+ await page.waitForTimeout(1000);
34
+
35
+ const results = await page.evaluate((limit: number) => {
36
+ const images: Array<{
37
+ title: string; thumbnailUrl: string; sourceUrl: string;
38
+ width: number; height: number;
39
+ }> = [];
40
+
41
+ const selectors = 'img[src*="reddit"], img[src*="redd.it"]';
42
+ document.querySelectorAll(selectors).forEach((img, idx) => {
43
+ if (idx >= limit) return;
44
+ const el = img as HTMLImageElement;
45
+ if (el.naturalWidth < 80) return;
46
+ images.push({
47
+ title: el.alt || '',
48
+ thumbnailUrl: el.src,
49
+ sourceUrl: el.closest('a')?.getAttribute('href') || '',
50
+ width: el.naturalWidth,
51
+ height: el.naturalHeight,
52
+ });
53
+ });
54
+
55
+ return images;
56
+ }, params.limit);
57
+
58
+ return ok({
59
+ query: params.query,
60
+ engine: 'reddit',
61
+ results: results.map(r => ({ ...r, sourceSite: 'reddit' })),
62
+ total: results.length,
63
+ timestamp: Date.now(),
64
+ });
65
+ } catch (error) {
66
+ return fail(error instanceof Error ? error.message : '未知错误');
67
+ }
68
+ },
69
+ });
70
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@xbrowser/reddit",
3
+ "version": "1.0.0",
4
+ "description": "Reddit 图片搜索",
5
+ "main": "index.ts",
6
+ "keywords": [
7
+ "xbrowser",
8
+ "xbrowser-plugin",
9
+ "reddit"
10
+ ],
11
+ "author": "dyyz1993",
12
+ "license": "MIT",
13
+ "xbrowser": {
14
+ "site": "https://www.reddit.com",
15
+ "requiresLogin": false,
16
+ "commands": [
17
+ "search-image"
18
+ ]
19
+ }
20
+ }