@ever-works/scrapfly-plugin 1.0.0 → 1.0.3
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/dist/index.cjs +313 -0
- package/dist/index.d.cts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +308 -0
- package/package.json +90 -77
- package/LICENSE +0 -661
- package/src/__tests__/scrapfly.plugin.spec.ts +0 -288
- package/src/index.ts +0 -2
- package/src/scrapfly.plugin.ts +0 -324
- package/tsconfig.json +0 -22
- package/tsup.config.ts +0 -12
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var scrapflySdk = require('scrapfly-sdk');
|
|
6
|
+
var net = require('net');
|
|
7
|
+
|
|
8
|
+
// src/scrapfly.plugin.ts
|
|
9
|
+
function isSafeWebhookUrl(rawUrl) {
|
|
10
|
+
let url;
|
|
11
|
+
try {
|
|
12
|
+
url = new URL(rawUrl);
|
|
13
|
+
} catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
let host = url.hostname.toLowerCase();
|
|
20
|
+
if (host.startsWith("[") && host.endsWith("]")) {
|
|
21
|
+
host = host.slice(1, -1);
|
|
22
|
+
}
|
|
23
|
+
if (CLOUD_METADATA_HOSTNAMES.has(host)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const ipKind = net.isIP(host);
|
|
27
|
+
if (ipKind === 4 && isPrivateIPv4(host)) return false;
|
|
28
|
+
if (ipKind === 6 && isPrivateIPv6(host)) return false;
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
var CLOUD_METADATA_HOSTNAMES = /* @__PURE__ */ new Set([
|
|
32
|
+
// GCP
|
|
33
|
+
"metadata.google.internal",
|
|
34
|
+
"metadata.goog",
|
|
35
|
+
// AWS
|
|
36
|
+
"instance-data",
|
|
37
|
+
"instance-data.ec2.internal",
|
|
38
|
+
// Azure
|
|
39
|
+
"metadata.azure.internal"
|
|
40
|
+
]);
|
|
41
|
+
function isPrivateIPv4(ip) {
|
|
42
|
+
const parts = ip.split(".").map((p) => Number(p));
|
|
43
|
+
if (parts.length !== 4 || parts.some((n) => Number.isNaN(n))) return false;
|
|
44
|
+
const [a, b] = parts;
|
|
45
|
+
if (a === 10) return true;
|
|
46
|
+
if (a === 127) return true;
|
|
47
|
+
if (a === 0) return true;
|
|
48
|
+
if (a === 169 && b === 254) return true;
|
|
49
|
+
if (a === 172 && b >= 16 && b <= 31) return true;
|
|
50
|
+
if (a === 192 && b === 168) return true;
|
|
51
|
+
if (a === 192 && b === 0 && parts[2] === 0) return true;
|
|
52
|
+
if (a === 100 && b >= 64 && b <= 127) return true;
|
|
53
|
+
if (a >= 224) return true;
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
function isPrivateIPv6(ip) {
|
|
57
|
+
const lower = ip.toLowerCase();
|
|
58
|
+
if (lower === "::1" || lower === "::") return true;
|
|
59
|
+
if (lower.startsWith("fc") || lower.startsWith("fd")) return true;
|
|
60
|
+
const firstHextet = lower.split(":", 1)[0];
|
|
61
|
+
if (/^[0-9a-f]{1,4}$/.test(firstHextet)) {
|
|
62
|
+
const group0 = parseInt(firstHextet, 16);
|
|
63
|
+
if ((group0 & 65472) === 65152) return true;
|
|
64
|
+
}
|
|
65
|
+
if (lower.startsWith("::ffff:")) {
|
|
66
|
+
const v4 = lower.slice("::ffff:".length);
|
|
67
|
+
if (net.isIP(v4) === 4) return isPrivateIPv4(v4);
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/scrapfly.plugin.ts
|
|
73
|
+
var API_KEY_ERROR = "Scrapfly API key not configured. Set it in plugin settings or via PLUGIN_SCRAPFLY_API_KEY environment variable.";
|
|
74
|
+
var ScrapflyPlugin = class {
|
|
75
|
+
id = "scrapfly";
|
|
76
|
+
name = "Scrapfly";
|
|
77
|
+
version = "1.0.0";
|
|
78
|
+
category = "content-extractor";
|
|
79
|
+
capabilities = ["screenshot", "content-extractor"];
|
|
80
|
+
providerName = "Scrapfly";
|
|
81
|
+
settingsSchema = {
|
|
82
|
+
type: "object",
|
|
83
|
+
properties: {
|
|
84
|
+
apiKey: {
|
|
85
|
+
type: "string",
|
|
86
|
+
title: "API Key",
|
|
87
|
+
description: "Your Scrapfly API key. Get one at https://scrapfly.io",
|
|
88
|
+
"x-secret": true,
|
|
89
|
+
"x-envVar": "PLUGIN_SCRAPFLY_API_KEY",
|
|
90
|
+
"x-scope": "user"
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
required: ["apiKey"]
|
|
94
|
+
};
|
|
95
|
+
configurationMode = "hybrid";
|
|
96
|
+
context;
|
|
97
|
+
async capture(options) {
|
|
98
|
+
if (!isSafeWebhookUrl(options.url)) {
|
|
99
|
+
return {
|
|
100
|
+
success: false,
|
|
101
|
+
error: `URL host is not safe to capture (SSRF guard blocked: ${options.url})`
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const apiKey = this.getApiKey(options.settings);
|
|
105
|
+
try {
|
|
106
|
+
const client = new scrapflySdk.ScrapflyClient({ key: apiKey });
|
|
107
|
+
const width = options.viewportWidth ?? 1280;
|
|
108
|
+
const height = options.viewportHeight ?? 800;
|
|
109
|
+
const config = new scrapflySdk.ScrapeConfig({
|
|
110
|
+
url: options.url,
|
|
111
|
+
render_js: true,
|
|
112
|
+
rendering_wait: options.delay ? options.delay : void 0,
|
|
113
|
+
wait_for_selector: options.waitForSelector || void 0,
|
|
114
|
+
screenshots: { main: "fullpage" },
|
|
115
|
+
country: "us"
|
|
116
|
+
});
|
|
117
|
+
const response = await client.scrape(config);
|
|
118
|
+
const screenshotData = response.result.screenshots;
|
|
119
|
+
if (screenshotData?.main) {
|
|
120
|
+
const screenshotUrl = screenshotData.main.url;
|
|
121
|
+
const fileSize = screenshotData.main.size || void 0;
|
|
122
|
+
return {
|
|
123
|
+
success: true,
|
|
124
|
+
imageUrl: screenshotUrl,
|
|
125
|
+
width,
|
|
126
|
+
height,
|
|
127
|
+
fileSize
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
success: false,
|
|
132
|
+
error: "No screenshot data returned from Scrapfly"
|
|
133
|
+
};
|
|
134
|
+
} catch (error) {
|
|
135
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
136
|
+
this.context?.logger.error(`Scrapfly screenshot failed: ${errorMessage}`);
|
|
137
|
+
return {
|
|
138
|
+
success: false,
|
|
139
|
+
error: errorMessage
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async getScreenshotUrl(options) {
|
|
144
|
+
const result = await this.capture(options);
|
|
145
|
+
return result.imageUrl || null;
|
|
146
|
+
}
|
|
147
|
+
async isAvailable() {
|
|
148
|
+
if (!this.context) return false;
|
|
149
|
+
const settings = await this.context.getSettings();
|
|
150
|
+
return Boolean(settings?.apiKey);
|
|
151
|
+
}
|
|
152
|
+
async validateConnection(settings) {
|
|
153
|
+
const apiKey = settings.apiKey;
|
|
154
|
+
if (!apiKey) {
|
|
155
|
+
return { success: false, message: "Scrapfly API key is not configured." };
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
const response = await fetch(`https://api.scrapfly.io/account?key=${apiKey}`);
|
|
159
|
+
if (!response.ok) {
|
|
160
|
+
return {
|
|
161
|
+
success: false,
|
|
162
|
+
message: `Scrapfly connection failed (${response.status}): ${response.statusText}`
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return { success: true, message: "Scrapfly connection verified." };
|
|
166
|
+
} catch (error) {
|
|
167
|
+
return {
|
|
168
|
+
success: false,
|
|
169
|
+
message: `Scrapfly connection failed: ${error instanceof Error ? error.message : String(error)}`
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
getMaxDimensions() {
|
|
174
|
+
return { width: 3840, height: 2160 };
|
|
175
|
+
}
|
|
176
|
+
async extract(options) {
|
|
177
|
+
const startTime = Date.now();
|
|
178
|
+
if (!isSafeWebhookUrl(options.url)) {
|
|
179
|
+
return {
|
|
180
|
+
success: false,
|
|
181
|
+
url: options.url,
|
|
182
|
+
error: `URL host is not safe to fetch (SSRF guard blocked: ${options.url})`,
|
|
183
|
+
duration: Date.now() - startTime
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const apiKey = this.getApiKey(options.settings);
|
|
187
|
+
try {
|
|
188
|
+
const client = new scrapflySdk.ScrapflyClient({ key: apiKey });
|
|
189
|
+
const config = new scrapflySdk.ScrapeConfig({
|
|
190
|
+
url: options.url,
|
|
191
|
+
render_js: options.waitForJs !== false,
|
|
192
|
+
asp: true,
|
|
193
|
+
format: "markdown",
|
|
194
|
+
country: "us"
|
|
195
|
+
});
|
|
196
|
+
const response = await client.scrape(config);
|
|
197
|
+
const content = response.result.content || "";
|
|
198
|
+
if (!content) {
|
|
199
|
+
return {
|
|
200
|
+
success: false,
|
|
201
|
+
url: options.url,
|
|
202
|
+
error: "No content returned from Scrapfly",
|
|
203
|
+
duration: Date.now() - startTime
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
const wordCount = content.split(/\s+/).filter((w) => w.length > 0).length;
|
|
207
|
+
return {
|
|
208
|
+
success: true,
|
|
209
|
+
url: options.url,
|
|
210
|
+
content,
|
|
211
|
+
markdown: content,
|
|
212
|
+
duration: Date.now() - startTime,
|
|
213
|
+
wordCount,
|
|
214
|
+
readingTime: Math.ceil(wordCount / 200)
|
|
215
|
+
};
|
|
216
|
+
} catch (error) {
|
|
217
|
+
return {
|
|
218
|
+
success: false,
|
|
219
|
+
url: options.url,
|
|
220
|
+
error: error instanceof Error ? error.message : String(error),
|
|
221
|
+
duration: Date.now() - startTime
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async extractBatch(urls, options) {
|
|
226
|
+
const batchSize = 5;
|
|
227
|
+
const results = [];
|
|
228
|
+
for (let i = 0; i < urls.length; i += batchSize) {
|
|
229
|
+
const batch = urls.slice(i, i + batchSize);
|
|
230
|
+
const batchResults = await Promise.all(batch.map((url) => this.extract({ url, ...options })));
|
|
231
|
+
results.push(...batchResults);
|
|
232
|
+
if (i + batchSize < urls.length) {
|
|
233
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return results;
|
|
237
|
+
}
|
|
238
|
+
async canExtract(url) {
|
|
239
|
+
try {
|
|
240
|
+
const parsed = new URL(url);
|
|
241
|
+
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
|
242
|
+
} catch {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
getApiKey(settings) {
|
|
247
|
+
const apiKey = settings?.apiKey;
|
|
248
|
+
if (!apiKey) {
|
|
249
|
+
throw new Error(API_KEY_ERROR);
|
|
250
|
+
}
|
|
251
|
+
return apiKey;
|
|
252
|
+
}
|
|
253
|
+
async onLoad(context) {
|
|
254
|
+
this.context = context;
|
|
255
|
+
context.logger.log("Scrapfly Plugin loaded");
|
|
256
|
+
}
|
|
257
|
+
async onUnload() {
|
|
258
|
+
this.context = void 0;
|
|
259
|
+
}
|
|
260
|
+
async healthCheck() {
|
|
261
|
+
return {
|
|
262
|
+
status: "healthy",
|
|
263
|
+
message: "Scrapfly plugin is ready (API key required for operations)",
|
|
264
|
+
checkedAt: Date.now()
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
getManifest() {
|
|
268
|
+
return {
|
|
269
|
+
id: this.id,
|
|
270
|
+
name: this.name,
|
|
271
|
+
version: this.version,
|
|
272
|
+
description: "Screenshot capture and content extraction using the Scrapfly API",
|
|
273
|
+
category: this.category,
|
|
274
|
+
capabilities: [...this.capabilities],
|
|
275
|
+
author: { name: "Ever Works Team" },
|
|
276
|
+
license: "AGPL-3.0",
|
|
277
|
+
builtIn: true,
|
|
278
|
+
systemPlugin: false,
|
|
279
|
+
autoEnable: false,
|
|
280
|
+
homepage: "https://scrapfly.io",
|
|
281
|
+
icon: {
|
|
282
|
+
type: "url",
|
|
283
|
+
value: "https://cdn.scrapfly.io/0.1.156/www/public/favicon.ico?version=0.1.156"
|
|
284
|
+
},
|
|
285
|
+
readme: [
|
|
286
|
+
"## What does Scrapfly do?",
|
|
287
|
+
"",
|
|
288
|
+
"Scrapfly is a web scraping and screenshot API that handles JavaScript rendering, anti-bot bypass, and proxy rotation. It can capture screenshots of any web page and extract content from even heavily protected sites.",
|
|
289
|
+
"",
|
|
290
|
+
"## Why use it?",
|
|
291
|
+
"",
|
|
292
|
+
"- **Anti-bot bypass** \u2014 handles CAPTCHAs, JavaScript challenges, and bot detection",
|
|
293
|
+
"- **Screenshot capture** \u2014 full-page screenshots with JavaScript rendering",
|
|
294
|
+
"- **Content extraction** \u2014 pulls raw HTML from any page, including SPAs",
|
|
295
|
+
"- **Global proxy network** \u2014 access region-locked content from any country",
|
|
296
|
+
"",
|
|
297
|
+
"## How it works in Ever Works",
|
|
298
|
+
"",
|
|
299
|
+
"Scrapfly serves dual purposes during work generation: capturing screenshots for item preview images and extracting content from web pages. Its anti-bot capabilities make it effective for scraping sites that block standard HTTP requests.",
|
|
300
|
+
"",
|
|
301
|
+
"## Getting started",
|
|
302
|
+
"",
|
|
303
|
+
"1. Sign up at [scrapfly.io](https://scrapfly.io)",
|
|
304
|
+
"2. Copy your API key from the dashboard",
|
|
305
|
+
"3. Enter the key in the **API Key** field below",
|
|
306
|
+
"4. Enable this plugin for screenshot capture and/or content extraction"
|
|
307
|
+
].join("\n")
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
exports.ScrapflyPlugin = ScrapflyPlugin;
|
|
313
|
+
exports.default = ScrapflyPlugin;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { IPlugin, IScreenshotPlugin, IContentExtractorPlugin, PluginCategory, JsonSchema, ScreenshotOptions, ScreenshotResult, ConnectionValidationResult, ContentExtractionOptions, ContentExtractionResult, PluginContext, PluginHealthCheck, PluginManifest } from '@ever-works/plugin';
|
|
2
|
+
|
|
3
|
+
declare class ScrapflyPlugin implements IPlugin, IScreenshotPlugin, IContentExtractorPlugin {
|
|
4
|
+
readonly id = "scrapfly";
|
|
5
|
+
readonly name = "Scrapfly";
|
|
6
|
+
readonly version = "1.0.0";
|
|
7
|
+
readonly category: PluginCategory;
|
|
8
|
+
readonly capabilities: readonly string[];
|
|
9
|
+
readonly providerName = "Scrapfly";
|
|
10
|
+
readonly settingsSchema: JsonSchema;
|
|
11
|
+
readonly configurationMode: 'admin-only' | 'user-required' | 'hybrid';
|
|
12
|
+
private context?;
|
|
13
|
+
capture(options: ScreenshotOptions): Promise<ScreenshotResult>;
|
|
14
|
+
getScreenshotUrl(options: ScreenshotOptions): Promise<string | null>;
|
|
15
|
+
isAvailable(): Promise<boolean>;
|
|
16
|
+
validateConnection(settings: Record<string, unknown>): Promise<ConnectionValidationResult>;
|
|
17
|
+
getMaxDimensions(): {
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
};
|
|
21
|
+
extract(options: ContentExtractionOptions): Promise<ContentExtractionResult>;
|
|
22
|
+
extractBatch(urls: readonly string[], options?: Partial<ContentExtractionOptions>): Promise<readonly ContentExtractionResult[]>;
|
|
23
|
+
canExtract(url: string): Promise<boolean>;
|
|
24
|
+
private getApiKey;
|
|
25
|
+
onLoad(context: PluginContext): Promise<void>;
|
|
26
|
+
onUnload(): Promise<void>;
|
|
27
|
+
healthCheck(): Promise<PluginHealthCheck>;
|
|
28
|
+
getManifest(): PluginManifest;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { ScrapflyPlugin, ScrapflyPlugin as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { IPlugin, IScreenshotPlugin, IContentExtractorPlugin, PluginCategory, JsonSchema, ScreenshotOptions, ScreenshotResult, ConnectionValidationResult, ContentExtractionOptions, ContentExtractionResult, PluginContext, PluginHealthCheck, PluginManifest } from '@ever-works/plugin';
|
|
2
|
+
|
|
3
|
+
declare class ScrapflyPlugin implements IPlugin, IScreenshotPlugin, IContentExtractorPlugin {
|
|
4
|
+
readonly id = "scrapfly";
|
|
5
|
+
readonly name = "Scrapfly";
|
|
6
|
+
readonly version = "1.0.0";
|
|
7
|
+
readonly category: PluginCategory;
|
|
8
|
+
readonly capabilities: readonly string[];
|
|
9
|
+
readonly providerName = "Scrapfly";
|
|
10
|
+
readonly settingsSchema: JsonSchema;
|
|
11
|
+
readonly configurationMode: 'admin-only' | 'user-required' | 'hybrid';
|
|
12
|
+
private context?;
|
|
13
|
+
capture(options: ScreenshotOptions): Promise<ScreenshotResult>;
|
|
14
|
+
getScreenshotUrl(options: ScreenshotOptions): Promise<string | null>;
|
|
15
|
+
isAvailable(): Promise<boolean>;
|
|
16
|
+
validateConnection(settings: Record<string, unknown>): Promise<ConnectionValidationResult>;
|
|
17
|
+
getMaxDimensions(): {
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
};
|
|
21
|
+
extract(options: ContentExtractionOptions): Promise<ContentExtractionResult>;
|
|
22
|
+
extractBatch(urls: readonly string[], options?: Partial<ContentExtractionOptions>): Promise<readonly ContentExtractionResult[]>;
|
|
23
|
+
canExtract(url: string): Promise<boolean>;
|
|
24
|
+
private getApiKey;
|
|
25
|
+
onLoad(context: PluginContext): Promise<void>;
|
|
26
|
+
onUnload(): Promise<void>;
|
|
27
|
+
healthCheck(): Promise<PluginHealthCheck>;
|
|
28
|
+
getManifest(): PluginManifest;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { ScrapflyPlugin, ScrapflyPlugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { ScrapflyClient, ScrapeConfig } from 'scrapfly-sdk';
|
|
2
|
+
import { isIP } from 'net';
|
|
3
|
+
|
|
4
|
+
// src/scrapfly.plugin.ts
|
|
5
|
+
function isSafeWebhookUrl(rawUrl) {
|
|
6
|
+
let url;
|
|
7
|
+
try {
|
|
8
|
+
url = new URL(rawUrl);
|
|
9
|
+
} catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
let host = url.hostname.toLowerCase();
|
|
16
|
+
if (host.startsWith("[") && host.endsWith("]")) {
|
|
17
|
+
host = host.slice(1, -1);
|
|
18
|
+
}
|
|
19
|
+
if (CLOUD_METADATA_HOSTNAMES.has(host)) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
const ipKind = isIP(host);
|
|
23
|
+
if (ipKind === 4 && isPrivateIPv4(host)) return false;
|
|
24
|
+
if (ipKind === 6 && isPrivateIPv6(host)) return false;
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
var CLOUD_METADATA_HOSTNAMES = /* @__PURE__ */ new Set([
|
|
28
|
+
// GCP
|
|
29
|
+
"metadata.google.internal",
|
|
30
|
+
"metadata.goog",
|
|
31
|
+
// AWS
|
|
32
|
+
"instance-data",
|
|
33
|
+
"instance-data.ec2.internal",
|
|
34
|
+
// Azure
|
|
35
|
+
"metadata.azure.internal"
|
|
36
|
+
]);
|
|
37
|
+
function isPrivateIPv4(ip) {
|
|
38
|
+
const parts = ip.split(".").map((p) => Number(p));
|
|
39
|
+
if (parts.length !== 4 || parts.some((n) => Number.isNaN(n))) return false;
|
|
40
|
+
const [a, b] = parts;
|
|
41
|
+
if (a === 10) return true;
|
|
42
|
+
if (a === 127) return true;
|
|
43
|
+
if (a === 0) return true;
|
|
44
|
+
if (a === 169 && b === 254) return true;
|
|
45
|
+
if (a === 172 && b >= 16 && b <= 31) return true;
|
|
46
|
+
if (a === 192 && b === 168) return true;
|
|
47
|
+
if (a === 192 && b === 0 && parts[2] === 0) return true;
|
|
48
|
+
if (a === 100 && b >= 64 && b <= 127) return true;
|
|
49
|
+
if (a >= 224) return true;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
function isPrivateIPv6(ip) {
|
|
53
|
+
const lower = ip.toLowerCase();
|
|
54
|
+
if (lower === "::1" || lower === "::") return true;
|
|
55
|
+
if (lower.startsWith("fc") || lower.startsWith("fd")) return true;
|
|
56
|
+
const firstHextet = lower.split(":", 1)[0];
|
|
57
|
+
if (/^[0-9a-f]{1,4}$/.test(firstHextet)) {
|
|
58
|
+
const group0 = parseInt(firstHextet, 16);
|
|
59
|
+
if ((group0 & 65472) === 65152) return true;
|
|
60
|
+
}
|
|
61
|
+
if (lower.startsWith("::ffff:")) {
|
|
62
|
+
const v4 = lower.slice("::ffff:".length);
|
|
63
|
+
if (isIP(v4) === 4) return isPrivateIPv4(v4);
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/scrapfly.plugin.ts
|
|
69
|
+
var API_KEY_ERROR = "Scrapfly API key not configured. Set it in plugin settings or via PLUGIN_SCRAPFLY_API_KEY environment variable.";
|
|
70
|
+
var ScrapflyPlugin = class {
|
|
71
|
+
id = "scrapfly";
|
|
72
|
+
name = "Scrapfly";
|
|
73
|
+
version = "1.0.0";
|
|
74
|
+
category = "content-extractor";
|
|
75
|
+
capabilities = ["screenshot", "content-extractor"];
|
|
76
|
+
providerName = "Scrapfly";
|
|
77
|
+
settingsSchema = {
|
|
78
|
+
type: "object",
|
|
79
|
+
properties: {
|
|
80
|
+
apiKey: {
|
|
81
|
+
type: "string",
|
|
82
|
+
title: "API Key",
|
|
83
|
+
description: "Your Scrapfly API key. Get one at https://scrapfly.io",
|
|
84
|
+
"x-secret": true,
|
|
85
|
+
"x-envVar": "PLUGIN_SCRAPFLY_API_KEY",
|
|
86
|
+
"x-scope": "user"
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
required: ["apiKey"]
|
|
90
|
+
};
|
|
91
|
+
configurationMode = "hybrid";
|
|
92
|
+
context;
|
|
93
|
+
async capture(options) {
|
|
94
|
+
if (!isSafeWebhookUrl(options.url)) {
|
|
95
|
+
return {
|
|
96
|
+
success: false,
|
|
97
|
+
error: `URL host is not safe to capture (SSRF guard blocked: ${options.url})`
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const apiKey = this.getApiKey(options.settings);
|
|
101
|
+
try {
|
|
102
|
+
const client = new ScrapflyClient({ key: apiKey });
|
|
103
|
+
const width = options.viewportWidth ?? 1280;
|
|
104
|
+
const height = options.viewportHeight ?? 800;
|
|
105
|
+
const config = new ScrapeConfig({
|
|
106
|
+
url: options.url,
|
|
107
|
+
render_js: true,
|
|
108
|
+
rendering_wait: options.delay ? options.delay : void 0,
|
|
109
|
+
wait_for_selector: options.waitForSelector || void 0,
|
|
110
|
+
screenshots: { main: "fullpage" },
|
|
111
|
+
country: "us"
|
|
112
|
+
});
|
|
113
|
+
const response = await client.scrape(config);
|
|
114
|
+
const screenshotData = response.result.screenshots;
|
|
115
|
+
if (screenshotData?.main) {
|
|
116
|
+
const screenshotUrl = screenshotData.main.url;
|
|
117
|
+
const fileSize = screenshotData.main.size || void 0;
|
|
118
|
+
return {
|
|
119
|
+
success: true,
|
|
120
|
+
imageUrl: screenshotUrl,
|
|
121
|
+
width,
|
|
122
|
+
height,
|
|
123
|
+
fileSize
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
success: false,
|
|
128
|
+
error: "No screenshot data returned from Scrapfly"
|
|
129
|
+
};
|
|
130
|
+
} catch (error) {
|
|
131
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
132
|
+
this.context?.logger.error(`Scrapfly screenshot failed: ${errorMessage}`);
|
|
133
|
+
return {
|
|
134
|
+
success: false,
|
|
135
|
+
error: errorMessage
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
async getScreenshotUrl(options) {
|
|
140
|
+
const result = await this.capture(options);
|
|
141
|
+
return result.imageUrl || null;
|
|
142
|
+
}
|
|
143
|
+
async isAvailable() {
|
|
144
|
+
if (!this.context) return false;
|
|
145
|
+
const settings = await this.context.getSettings();
|
|
146
|
+
return Boolean(settings?.apiKey);
|
|
147
|
+
}
|
|
148
|
+
async validateConnection(settings) {
|
|
149
|
+
const apiKey = settings.apiKey;
|
|
150
|
+
if (!apiKey) {
|
|
151
|
+
return { success: false, message: "Scrapfly API key is not configured." };
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
const response = await fetch(`https://api.scrapfly.io/account?key=${apiKey}`);
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
return {
|
|
157
|
+
success: false,
|
|
158
|
+
message: `Scrapfly connection failed (${response.status}): ${response.statusText}`
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
return { success: true, message: "Scrapfly connection verified." };
|
|
162
|
+
} catch (error) {
|
|
163
|
+
return {
|
|
164
|
+
success: false,
|
|
165
|
+
message: `Scrapfly connection failed: ${error instanceof Error ? error.message : String(error)}`
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
getMaxDimensions() {
|
|
170
|
+
return { width: 3840, height: 2160 };
|
|
171
|
+
}
|
|
172
|
+
async extract(options) {
|
|
173
|
+
const startTime = Date.now();
|
|
174
|
+
if (!isSafeWebhookUrl(options.url)) {
|
|
175
|
+
return {
|
|
176
|
+
success: false,
|
|
177
|
+
url: options.url,
|
|
178
|
+
error: `URL host is not safe to fetch (SSRF guard blocked: ${options.url})`,
|
|
179
|
+
duration: Date.now() - startTime
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
const apiKey = this.getApiKey(options.settings);
|
|
183
|
+
try {
|
|
184
|
+
const client = new ScrapflyClient({ key: apiKey });
|
|
185
|
+
const config = new ScrapeConfig({
|
|
186
|
+
url: options.url,
|
|
187
|
+
render_js: options.waitForJs !== false,
|
|
188
|
+
asp: true,
|
|
189
|
+
format: "markdown",
|
|
190
|
+
country: "us"
|
|
191
|
+
});
|
|
192
|
+
const response = await client.scrape(config);
|
|
193
|
+
const content = response.result.content || "";
|
|
194
|
+
if (!content) {
|
|
195
|
+
return {
|
|
196
|
+
success: false,
|
|
197
|
+
url: options.url,
|
|
198
|
+
error: "No content returned from Scrapfly",
|
|
199
|
+
duration: Date.now() - startTime
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
const wordCount = content.split(/\s+/).filter((w) => w.length > 0).length;
|
|
203
|
+
return {
|
|
204
|
+
success: true,
|
|
205
|
+
url: options.url,
|
|
206
|
+
content,
|
|
207
|
+
markdown: content,
|
|
208
|
+
duration: Date.now() - startTime,
|
|
209
|
+
wordCount,
|
|
210
|
+
readingTime: Math.ceil(wordCount / 200)
|
|
211
|
+
};
|
|
212
|
+
} catch (error) {
|
|
213
|
+
return {
|
|
214
|
+
success: false,
|
|
215
|
+
url: options.url,
|
|
216
|
+
error: error instanceof Error ? error.message : String(error),
|
|
217
|
+
duration: Date.now() - startTime
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async extractBatch(urls, options) {
|
|
222
|
+
const batchSize = 5;
|
|
223
|
+
const results = [];
|
|
224
|
+
for (let i = 0; i < urls.length; i += batchSize) {
|
|
225
|
+
const batch = urls.slice(i, i + batchSize);
|
|
226
|
+
const batchResults = await Promise.all(batch.map((url) => this.extract({ url, ...options })));
|
|
227
|
+
results.push(...batchResults);
|
|
228
|
+
if (i + batchSize < urls.length) {
|
|
229
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return results;
|
|
233
|
+
}
|
|
234
|
+
async canExtract(url) {
|
|
235
|
+
try {
|
|
236
|
+
const parsed = new URL(url);
|
|
237
|
+
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
|
238
|
+
} catch {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
getApiKey(settings) {
|
|
243
|
+
const apiKey = settings?.apiKey;
|
|
244
|
+
if (!apiKey) {
|
|
245
|
+
throw new Error(API_KEY_ERROR);
|
|
246
|
+
}
|
|
247
|
+
return apiKey;
|
|
248
|
+
}
|
|
249
|
+
async onLoad(context) {
|
|
250
|
+
this.context = context;
|
|
251
|
+
context.logger.log("Scrapfly Plugin loaded");
|
|
252
|
+
}
|
|
253
|
+
async onUnload() {
|
|
254
|
+
this.context = void 0;
|
|
255
|
+
}
|
|
256
|
+
async healthCheck() {
|
|
257
|
+
return {
|
|
258
|
+
status: "healthy",
|
|
259
|
+
message: "Scrapfly plugin is ready (API key required for operations)",
|
|
260
|
+
checkedAt: Date.now()
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
getManifest() {
|
|
264
|
+
return {
|
|
265
|
+
id: this.id,
|
|
266
|
+
name: this.name,
|
|
267
|
+
version: this.version,
|
|
268
|
+
description: "Screenshot capture and content extraction using the Scrapfly API",
|
|
269
|
+
category: this.category,
|
|
270
|
+
capabilities: [...this.capabilities],
|
|
271
|
+
author: { name: "Ever Works Team" },
|
|
272
|
+
license: "AGPL-3.0",
|
|
273
|
+
builtIn: true,
|
|
274
|
+
systemPlugin: false,
|
|
275
|
+
autoEnable: false,
|
|
276
|
+
homepage: "https://scrapfly.io",
|
|
277
|
+
icon: {
|
|
278
|
+
type: "url",
|
|
279
|
+
value: "https://cdn.scrapfly.io/0.1.156/www/public/favicon.ico?version=0.1.156"
|
|
280
|
+
},
|
|
281
|
+
readme: [
|
|
282
|
+
"## What does Scrapfly do?",
|
|
283
|
+
"",
|
|
284
|
+
"Scrapfly is a web scraping and screenshot API that handles JavaScript rendering, anti-bot bypass, and proxy rotation. It can capture screenshots of any web page and extract content from even heavily protected sites.",
|
|
285
|
+
"",
|
|
286
|
+
"## Why use it?",
|
|
287
|
+
"",
|
|
288
|
+
"- **Anti-bot bypass** \u2014 handles CAPTCHAs, JavaScript challenges, and bot detection",
|
|
289
|
+
"- **Screenshot capture** \u2014 full-page screenshots with JavaScript rendering",
|
|
290
|
+
"- **Content extraction** \u2014 pulls raw HTML from any page, including SPAs",
|
|
291
|
+
"- **Global proxy network** \u2014 access region-locked content from any country",
|
|
292
|
+
"",
|
|
293
|
+
"## How it works in Ever Works",
|
|
294
|
+
"",
|
|
295
|
+
"Scrapfly serves dual purposes during work generation: capturing screenshots for item preview images and extracting content from web pages. Its anti-bot capabilities make it effective for scraping sites that block standard HTTP requests.",
|
|
296
|
+
"",
|
|
297
|
+
"## Getting started",
|
|
298
|
+
"",
|
|
299
|
+
"1. Sign up at [scrapfly.io](https://scrapfly.io)",
|
|
300
|
+
"2. Copy your API key from the dashboard",
|
|
301
|
+
"3. Enter the key in the **API Key** field below",
|
|
302
|
+
"4. Enable this plugin for screenshot capture and/or content extraction"
|
|
303
|
+
].join("\n")
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
export { ScrapflyPlugin, ScrapflyPlugin as default };
|