@xbrowser/behance 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 +83 -0
  2. package/package.json +20 -0
package/index.ts ADDED
@@ -0,0 +1,83 @@
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 behance = xcli.createSite({
7
+ name: 'behance',
8
+ url: 'https://www.behance.net',
9
+ description: 'Behance Creative Portfolio Search',
10
+ requiresLogin: false,
11
+ });
12
+
13
+ behance.command('search-image', {
14
+ description: 'Search images on Behance',
15
+ scope: 'browser',
16
+ parameters: z.object({
17
+ query: z.string().describe('Search query'),
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 browserPage = (params.page as import('playwright').Page)
25
+ || (ctx as Record<string, unknown>).page as import('playwright').Page;
26
+ if (!browserPage) throw new Error('需要浏览器页面');
27
+
28
+ try {
29
+ const url = `https://www.behance.net/search?search=${encodeURIComponent(params.query)}`;
30
+ await browserPage.goto(url, { waitUntil: 'domcontentloaded', timeout: params.timeout });
31
+ await browserPage.waitForTimeout(3000);
32
+
33
+ for (let i = 0; i < 2; i++) {
34
+ await browserPage.evaluate(() => window.scrollBy(0, window.innerHeight));
35
+ await browserPage.waitForTimeout(1000);
36
+ }
37
+
38
+ const results = await browserPage.evaluate((limit: number) => {
39
+ const images: Array<{
40
+ title: string; thumbnailUrl: string; sourceUrl: string;
41
+ originalUrl: string; width: number; height: number;
42
+ format: string; sourceSite: string;
43
+ }> = [];
44
+
45
+ const imgs = document.querySelectorAll(
46
+ 'img[src*="behance"], .ProjectCoverNeue img, .cover img'
47
+ );
48
+ imgs.forEach((img, idx) => {
49
+ if (idx >= limit) return;
50
+ const el = img as HTMLImageElement;
51
+ if (el.width < 50 || el.height < 50) return;
52
+
53
+ const src = el.src;
54
+ const anchor = el.closest('a');
55
+
56
+ images.push({
57
+ title: el.alt || '',
58
+ thumbnailUrl: src,
59
+ sourceUrl: anchor?.href || '',
60
+ originalUrl: src,
61
+ width: el.naturalWidth || el.width,
62
+ height: el.naturalHeight || el.height,
63
+ format: src.split('.').pop()?.split('?')[0] || 'jpg',
64
+ sourceSite: 'behance',
65
+ });
66
+ });
67
+
68
+ return images.slice(0, limit);
69
+ }, params.limit);
70
+
71
+ return ok({
72
+ query: params.query,
73
+ engine: 'behance',
74
+ results,
75
+ total: results.length,
76
+ timestamp: Date.now(),
77
+ }, [`Behance "${params.query}",共 ${results.length} 张`]);
78
+ } catch (error) {
79
+ return fail(error instanceof Error ? error.message : '未知错误');
80
+ }
81
+ },
82
+ });
83
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@xbrowser/behance",
3
+ "version": "1.0.0",
4
+ "description": "Behance Creative Portfolio Search",
5
+ "main": "index.ts",
6
+ "keywords": [
7
+ "xbrowser",
8
+ "xbrowser-plugin",
9
+ "behance"
10
+ ],
11
+ "author": "dyyz1993",
12
+ "license": "MIT",
13
+ "xbrowser": {
14
+ "site": "https://www.behance.net",
15
+ "requiresLogin": false,
16
+ "commands": [
17
+ "search-image"
18
+ ]
19
+ }
20
+ }