@xbrowser/pexels 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.
- package/index.ts +93 -0
- package/package.json +20 -0
package/index.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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 site = xcli.createSite({
|
|
7
|
+
name: 'pexels', url: 'https://www.pexels.com',
|
|
8
|
+
description: 'Pexels - Free stock photos and videos', requiresLogin: false,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
site.command('search-image', {
|
|
12
|
+
description: 'Search Pexels photos with metadata',
|
|
13
|
+
scope: 'browser',
|
|
14
|
+
parameters: z.object({
|
|
15
|
+
query: z.string(), limit: z.number().optional().default(20),
|
|
16
|
+
color: z.string().optional(), page: z.any().optional(), timeout: z.number().optional().default(20000),
|
|
17
|
+
}),
|
|
18
|
+
result: z.any(),
|
|
19
|
+
handler: async (params, ctx) => {
|
|
20
|
+
const page = (params.page as import('playwright').Page) || (ctx as Record<string, unknown>).page as import('playwright').Page;
|
|
21
|
+
if (!page) throw new Error('需要浏览器页面');
|
|
22
|
+
try {
|
|
23
|
+
let url = `https://www.pexels.com/search/${encodeURIComponent(params.query)}/`;
|
|
24
|
+
if (params.color) url += `?color=${params.color}`;
|
|
25
|
+
await page.goto(url, { waitUntil: 'domcontentloaded', timeout: params.timeout });
|
|
26
|
+
await page.waitForTimeout(3000);
|
|
27
|
+
for (let i = 0; i < Math.ceil(params.limit / 10); i++) {
|
|
28
|
+
await page.evaluate(() => window.scrollBy(0, 1000));
|
|
29
|
+
await page.waitForTimeout(800);
|
|
30
|
+
}
|
|
31
|
+
const results = await page.evaluate((limit: number) => {
|
|
32
|
+
const images: any[] = [];
|
|
33
|
+
// Pexels 的图片在 article 或 .MediaCard 中,排除导航/header 中的小图标
|
|
34
|
+
const containers = document.querySelectorAll('article, .MediaCard, [data-testid="photo-card"], .photos__photo-item');
|
|
35
|
+
containers.forEach((container) => {
|
|
36
|
+
if (images.length >= limit) return;
|
|
37
|
+
const imgs = container.querySelectorAll('img');
|
|
38
|
+
imgs.forEach((img) => {
|
|
39
|
+
if (images.length >= limit) return;
|
|
40
|
+
const el = img;
|
|
41
|
+
const src = el.src || '';
|
|
42
|
+
// 只保留 Pexels CDN 图片,排除 flag/svg/avatar
|
|
43
|
+
if (!src.includes('images.pexels.com') && !src.includes('static.pexels.com')) return;
|
|
44
|
+
if (src.includes('/flags/') || src.includes('/user/') || src.includes('avatar')) return;
|
|
45
|
+
if (el.width < 100 || el.height < 100) return;
|
|
46
|
+
|
|
47
|
+
const srcset = el.getAttribute('srcset') || '';
|
|
48
|
+
let originalUrl = el.getAttribute('data-big-src') || '';
|
|
49
|
+
if (!originalUrl && srcset) {
|
|
50
|
+
const entries = srcset.split(',').map((s) => s.trim().split(' '));
|
|
51
|
+
const largest = entries.sort((a, b) => (parseInt(b[1] || '0', 10) || 0) - (parseInt(a[1] || '0', 10) || 0))[0];
|
|
52
|
+
originalUrl = largest?.[0] || src;
|
|
53
|
+
}
|
|
54
|
+
if (!originalUrl) originalUrl = src;
|
|
55
|
+
// 获取高清大图:Pexels CDN URL 追加高清参数
|
|
56
|
+
const cleanUrl = originalUrl.split('?')[0];
|
|
57
|
+
originalUrl = cleanUrl + '?auto=compress&cs=tinysrgb&dpr=2&w=2400';
|
|
58
|
+
const midUrl = cleanUrl + '?auto=compress&cs=tinysrgb&dpr=2&w=800';
|
|
59
|
+
images.push({
|
|
60
|
+
title: el.alt || '', thumbnailUrl: midUrl,
|
|
61
|
+
sourceUrl: el.closest('a')?.href || '', originalUrl,
|
|
62
|
+
width: el.naturalWidth || 0, height: el.naturalHeight || 0,
|
|
63
|
+
format: 'jpg', sourceSite: 'pexels',
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Fallback: 如果容器选择器没找到,直接找所有 pexels CDN 图片
|
|
69
|
+
if (images.length === 0) {
|
|
70
|
+
document.querySelectorAll('img').forEach((img) => {
|
|
71
|
+
if (images.length >= limit) return;
|
|
72
|
+
const el = img;
|
|
73
|
+
const src = el.src || '';
|
|
74
|
+
if (!src.includes('images.pexels.com')) return;
|
|
75
|
+
if (src.includes('/flags/') || src.includes('/user/')) return;
|
|
76
|
+
if (el.width < 100) return;
|
|
77
|
+
const cleanFallbackUrl = src.split('?')[0];
|
|
78
|
+
images.push({
|
|
79
|
+
title: el.alt || '', thumbnailUrl: cleanFallbackUrl + '?auto=compress&cs=tinysrgb&dpr=2&w=800',
|
|
80
|
+
sourceUrl: el.closest('a')?.href || '',
|
|
81
|
+
originalUrl: cleanFallbackUrl + '?auto=compress&cs=tinysrgb&dpr=2&w=2400',
|
|
82
|
+
width: el.naturalWidth || 0, height: el.naturalHeight || 0,
|
|
83
|
+
format: 'jpg', sourceSite: 'pexels',
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return images.slice(0, limit);
|
|
88
|
+
}, params.limit);
|
|
89
|
+
return ok({ query: params.query, engine: 'pexels', results, total: results.length, timestamp: Date.now() }, [`Pexels "${params.query}",共 ${results.length} 张`]);
|
|
90
|
+
} catch (error) { return { data: null, message: error instanceof Error ? error.message : '未知错误' }; }
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xbrowser/pexels",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Pexels - Free stock photos and videos",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"xbrowser",
|
|
8
|
+
"xbrowser-plugin",
|
|
9
|
+
"pexels"
|
|
10
|
+
],
|
|
11
|
+
"author": "dyyz1993",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"xbrowser": {
|
|
14
|
+
"site": "https://www.pexels.com",
|
|
15
|
+
"requiresLogin": false,
|
|
16
|
+
"commands": [
|
|
17
|
+
"search-image"
|
|
18
|
+
]
|
|
19
|
+
}
|
|
20
|
+
}
|