-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
586 lines (542 loc) · 17.3 KB
/
Copy pathmain.js
File metadata and controls
586 lines (542 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
const translations = {
zh: {
nav: {
features: "特性",
architecture: "架构",
quickstart: "快速开始",
resources: "资源",
api: "API",
github: "GitHub"
},
common: {
copy: "Copy",
copied: "Copied"
},
hero: {
meta: {
status: "Status · Operational",
build: "Last build · stable"
},
titleLine1: "抓取即",
titleLine2: "魔法",
descriptionPrefix: "一个灵活、可扩展的 Java 爬虫框架。覆盖下载、URL 管理、内容抽取与持久化的完整生命周期,",
descriptionEmphasis: "几行代码,一个爬虫。",
primaryCta: "快速开始",
secondaryCta: "查看源码 ↗",
windowTitle: "crawl · live"
},
stats: {
stars: "Stars",
forks: "Forks",
contributors: "Contributors",
license: "License"
},
features: {
title: "核心特性",
items: {
simple: {
title: "简单易用",
desc: "链式 API,十几行代码就能构建一个可工作的爬虫。"
},
flexible: {
title: "高度灵活",
desc: "四大组件彼此独立,任一层都可以替换、扩展与测试。"
},
thread: {
title: "多线程与分布式",
desc: "内置线程池;切换 Redis Scheduler 即可扩展为分布式抓取。"
},
annotation: {
title: "注解驱动",
desc: "@TargetUrl、@ExtractBy 让 POJO 既是模型也是爬虫描述。"
},
extract: {
title: "强大提取",
desc: "XPath、CSS、正则、JsonPath 可组合使用,适应复杂页面结构。"
},
integration: {
title: "易于集成",
desc: "结果可以流向 Console、文件、MySQL、Redis 或自定义 Pipeline。"
}
}
},
architecture: {
title: "架构一览"
},
quickstart: {
title: "快速开始"
},
resources: {
title: "资源与生态",
items: {
zh: {
title: "中文文档",
desc: "完整的中文使用指南、概念说明与分章节教程。",
link: "查看文档"
},
en: {
title: "English Docs",
desc: "English reference for setup, concepts, annotations and examples.",
link: "View docs"
},
api: {
title: "JavaDoc",
desc: "按包、类与方法浏览 API 细节,适合查类型签名与扩展点。",
link: "打开 API"
},
community: {
title: "社区支持",
desc: "在 GitHub Issues 中反馈问题、讨论实现细节或参与贡献。",
link: "提交问题"
}
}
},
footer: {
copy: "一个安静运行了很多年的开源 Java 爬虫框架。灵活、易用、可扩展。",
project: "项目",
releases: "版本发布",
docs: "文档",
zh: "中文文档",
en: "English",
community: "社区",
source: "源码仓库",
guide: "贡献入口",
bottom: "© 2013–2024 WebMagic · Apache 2.0"
}
},
en: {
nav: {
features: "Features",
architecture: "Architecture",
quickstart: "Quick Start",
resources: "Resources",
api: "API",
github: "GitHub"
},
common: {
copy: "Copy",
copied: "Copied"
},
hero: {
meta: {
status: "Status · Operational",
build: "Last build · stable"
},
titleLine1: "Crawl is",
titleLine2: "Magic",
descriptionPrefix: "A flexible, extensible Java crawler framework that covers downloading, URL management, content extraction and persistence across the full lifecycle.",
descriptionEmphasis: "A crawler in just a few lines.",
primaryCta: "Quick Start",
secondaryCta: "View Source ↗",
windowTitle: "crawl · live"
},
stats: {
stars: "Stars",
forks: "Forks",
contributors: "Contributors",
license: "License"
},
features: {
title: "Core Features",
items: {
simple: {
title: "Simple by Default",
desc: "A fluent API lets you launch a working crawler in only a handful of lines."
},
flexible: {
title: "Highly Composable",
desc: "Downloader, Scheduler, Processor and Pipeline stay isolated and easy to replace."
},
thread: {
title: "Multi-threaded and Distributed",
desc: "Built-in threading scales locally, and Redis Scheduler extends to distributed jobs."
},
annotation: {
title: "Annotation Driven",
desc: "@TargetUrl and @ExtractBy make POJOs double as crawler declarations."
},
extract: {
title: "Powerful Extraction",
desc: "Mix XPath, CSS selectors, regex and JsonPath to handle uneven page structures."
},
integration: {
title: "Easy to Integrate",
desc: "Send results to Console, files, MySQL, Redis or your own custom pipeline."
}
}
},
architecture: {
title: "Pipeline Overview"
},
quickstart: {
title: "Quick Start"
},
resources: {
title: "Resources and Ecosystem",
items: {
zh: {
title: "Chinese Docs",
desc: "Full Chinese guides, concepts and chapter-based tutorials.",
link: "Open docs"
},
en: {
title: "English Docs",
desc: "English reference for setup, concepts, annotations and examples.",
link: "View docs"
},
api: {
title: "JavaDoc",
desc: "Browse packages, classes and methods when you need exact API details.",
link: "Open API"
},
community: {
title: "Community Support",
desc: "Use GitHub Issues to report bugs, discuss implementation details and contribute.",
link: "Open issues"
}
}
},
footer: {
copy: "An open source Java crawler framework that has stayed quiet, stable and extensible for years.",
project: "Project",
releases: "Releases",
docs: "Docs",
zh: "Chinese Docs",
en: "English",
community: "Community",
source: "Source Repo",
guide: "Contribution Guide",
bottom: "© 2013–2024 WebMagic · Apache 2.0"
}
}
};
const quickstartSteps = {
zh: [
{
id: "maven",
label: "01 · 依赖管理",
sub: "pom.xml",
command: "cat pom.xml",
path: "~/webmagic-demo",
meta: "pom.xml",
caption: "先加入核心依赖与扩展包。WebMagic 核心保持轻量,额外能力通过 extension 组合。",
code: `<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId>
<version>1.0.3</version>
</dependency>`
},
{
id: "processor",
label: "02 · 编写爬虫",
sub: "PageProcessor",
command: "cat GithubRepoProcessor.java",
path: "~/webmagic-demo/src/main/java/demo",
meta: "GithubRepoProcessor.java",
caption: "核心逻辑通常写在 PageProcessor 中:定义抓取规则、抽取字段、发现下一跳链接。",
code: `public class GithubRepoProcessor implements PageProcessor {
private final Site site = Site.me()
.setRetryTimes(3)
.setSleepTime(100);
@Override
public void process(Page page) {
page.addTargetRequests(page.getHtml().links()
.regex("(https://github\\\\.com/\\\\w+/\\\\w+)")
.all());
page.putField("author", page.getUrl()
.regex("https://github\\\\.com/(\\\\w+)/.*")
.toString());
page.putField("name", page.getHtml()
.xpath("//h1[@class='public']/strong/a/text()")
.toString());
}
@Override
public Site getSite() {
return site;
}
}`
},
{
id: "run",
label: "03 · 启动爬虫",
sub: "Spider",
command: "cat Main.java",
path: "~/webmagic-demo/src/main/java/demo",
meta: "Main.java",
caption: "Spider 把调度、下载、处理与输出串成一条流水线,启动后即可开始抓取。",
code: `public class Main {
public static void main(String[] args) {
Spider.create(new GithubRepoProcessor())
.addUrl("https://github.com/code4craft")
.addPipeline(new ConsolePipeline())
.thread(5)
.run();
}
}`
}
],
en: [
{
id: "maven",
label: "01 · Dependencies",
sub: "pom.xml",
command: "cat pom.xml",
path: "~/webmagic-demo",
meta: "pom.xml",
caption: "Start with the core dependency and add extension modules only when you need them.",
code: `<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId>
<version>1.0.3</version>
</dependency>`
},
{
id: "processor",
label: "02 · Processor",
sub: "PageProcessor",
command: "cat GithubRepoProcessor.java",
path: "~/webmagic-demo/src/main/java/demo",
meta: "GithubRepoProcessor.java",
caption: "Most crawling logic lives in PageProcessor: discover links, extract fields and define selectors.",
code: `public class GithubRepoProcessor implements PageProcessor {
private final Site site = Site.me()
.setRetryTimes(3)
.setSleepTime(100);
@Override
public void process(Page page) {
page.addTargetRequests(page.getHtml().links()
.regex("(https://github\\\\.com/\\\\w+/\\\\w+)")
.all());
page.putField("author", page.getUrl()
.regex("https://github\\\\.com/(\\\\w+)/.*")
.toString());
page.putField("name", page.getHtml()
.xpath("//h1[@class='public']/strong/a/text()")
.toString());
}
@Override
public Site getSite() {
return site;
}
}`
},
{
id: "run",
label: "03 · Run Spider",
sub: "Spider",
command: "cat Main.java",
path: "~/webmagic-demo/src/main/java/demo",
meta: "Main.java",
caption: "Spider connects scheduler, downloader, processor and output into a single crawl pipeline.",
code: `public class Main {
public static void main(String[] args) {
Spider.create(new GithubRepoProcessor())
.addUrl("https://github.com/code4craft")
.addPipeline(new ConsolePipeline())
.thread(5)
.run();
}
}`
}
]
};
let currentLanguage = "zh";
let currentStepIndex = 0;
function getNestedValue(source, path) {
return path.split(".").reduce((value, key) => (value ? value[key] : undefined), source);
}
function updateI18n() {
document.documentElement.lang = currentLanguage === "zh" ? "zh-CN" : "en";
document.title = currentLanguage === "zh" ? "WebMagic · 抓取即魔法" : "WebMagic · Crawl is Magic";
document.querySelectorAll("[data-i18n]").forEach((element) => {
const key = element.dataset.i18n;
const value = getNestedValue(translations[currentLanguage], key);
if (typeof value === "string") {
element.textContent = value;
}
});
const langToggle = document.getElementById("langToggle");
if (langToggle) {
langToggle.textContent = currentLanguage === "zh" ? "EN" : "中";
}
renderQuickstartTabs();
renderQuickstartContent(currentStepIndex);
}
function renderQuickstartTabs() {
const nav = document.getElementById("quickstartNav");
if (!nav) return;
const steps = quickstartSteps[currentLanguage];
nav.innerHTML = "";
steps.forEach((step, index) => {
const button = document.createElement("button");
button.type = "button";
button.className = index === currentStepIndex ? "is-active" : "";
button.setAttribute("role", "tab");
button.setAttribute("aria-selected", String(index === currentStepIndex));
button.innerHTML = `${step.label}<span class="step-sub">${step.sub}</span>`;
button.addEventListener("click", () => {
currentStepIndex = index;
renderQuickstartTabs();
renderQuickstartContent(index);
});
nav.appendChild(button);
});
}
function renderQuickstartContent(index) {
const steps = quickstartSteps[currentLanguage];
const step = steps[index] || steps[0];
if (!step) return;
const path = document.getElementById("quickstartPath");
const command = document.getElementById("quickstartCommand");
const meta = document.getElementById("quickstartMeta");
const lines = document.getElementById("quickstartLines");
const code = document.getElementById("quickstartCode");
const caption = document.getElementById("quickstartCaption");
if (!path || !command || !meta || !lines || !code || !caption) return;
path.textContent = step.path;
command.textContent = step.command;
meta.textContent = step.meta;
code.textContent = step.code;
code.dataset.command = step.code;
caption.textContent = step.caption;
const lineCount = step.code.split("\n").length;
lines.textContent = Array.from({ length: lineCount }, (_, i) => String(i + 1)).join("\n");
}
function setupTypewriter() {
const source = document.getElementById("installCommand");
const target = document.getElementById("typewriterText");
if (!source || !target) return;
const text = source.dataset.command || "";
let index = 0;
target.textContent = "";
const tick = () => {
if (index <= text.length) {
target.textContent = text.slice(0, index);
index += 1;
window.setTimeout(tick, 26);
}
};
tick();
}
function setupCopyButtons() {
document.querySelectorAll("[data-copy-target]").forEach((button) => {
button.addEventListener("click", async () => {
const targetId = button.dataset.copyTarget;
const source = document.getElementById(targetId);
if (!source) return;
const text = source.dataset.command || source.textContent || "";
try {
await navigator.clipboard.writeText(text.trimEnd());
button.classList.add("is-copied");
button.textContent = getNestedValue(translations[currentLanguage], "common.copied");
window.setTimeout(() => {
button.classList.remove("is-copied");
button.textContent = getNestedValue(translations[currentLanguage], "common.copy");
}, 1600);
} catch (error) {
button.textContent = "Error";
window.setTimeout(() => {
button.textContent = getNestedValue(translations[currentLanguage], "common.copy");
}, 1400);
}
});
});
}
function setupHeaderState() {
const header = document.getElementById("siteHeader");
if (!header) return;
const update = () => {
header.classList.toggle("is-scrolled", window.scrollY > 18);
};
update();
window.addEventListener("scroll", update, { passive: true });
}
function setupReveal() {
const items = document.querySelectorAll(".reveal");
if (!items.length) return;
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("is-visible");
observer.unobserve(entry.target);
}
});
},
{ threshold: 0.15, rootMargin: "0px 0px -48px 0px" }
);
items.forEach((item) => observer.observe(item));
}
function setupMobileNav() {
const toggle = document.getElementById("mobileToggle");
const navLinks = document.getElementById("navLinks");
const navActions = document.getElementById("navActions");
if (!toggle || !navLinks || !navActions) return;
const setOpen = (open) => {
toggle.classList.toggle("is-open", open);
toggle.setAttribute("aria-expanded", String(open));
navLinks.classList.toggle("is-open", open);
navActions.classList.toggle("is-open", open);
};
toggle.addEventListener("click", () => {
setOpen(!toggle.classList.contains("is-open"));
});
navLinks.querySelectorAll("a").forEach((link) => {
link.addEventListener("click", () => {
if (window.innerWidth <= 840) {
setOpen(false);
}
});
});
window.addEventListener("resize", () => {
if (window.innerWidth > 840) {
setOpen(false);
}
});
}
function setupActiveNav() {
const sections = [...document.querySelectorAll("main section[id]")];
const links = [...document.querySelectorAll(".nav-link[href^='#']")];
if (!sections.length || !links.length) return;
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
const id = entry.target.id;
links.forEach((link) => {
link.classList.toggle("is-active", link.getAttribute("href") === `#${id}`);
});
});
},
{ threshold: 0.45 }
);
sections.forEach((section) => observer.observe(section));
}
function setupLanguageToggle() {
const button = document.getElementById("langToggle");
if (!button) return;
button.addEventListener("click", () => {
currentLanguage = currentLanguage === "zh" ? "en" : "zh";
updateI18n();
});
}
document.addEventListener("DOMContentLoaded", () => {
updateI18n();
setupTypewriter();
setupCopyButtons();
setupHeaderState();
setupReveal();
setupMobileNav();
setupActiveNav();
setupLanguageToggle();
});