@jackwener/opencli 1.7.8 → 1.7.10
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/README.md +49 -14
- package/README.zh-CN.md +30 -10
- package/cli-manifest.json +646 -30
- package/clis/36kr/news.js +1 -1
- package/clis/apple-podcasts/commands.test.js +4 -4
- package/clis/apple-podcasts/episodes.js +1 -1
- package/clis/apple-podcasts/search.js +1 -1
- package/clis/apple-podcasts/top.js +1 -1
- package/clis/arxiv/paper.js +1 -1
- package/clis/arxiv/search.js +1 -1
- package/clis/band/mentions.js +3 -3
- package/clis/bbc/news.js +1 -1
- package/clis/bilibili/subtitle.js +2 -2
- package/clis/bloomberg/businessweek.js +1 -1
- package/clis/bloomberg/economics.js +1 -1
- package/clis/bloomberg/industries.js +1 -1
- package/clis/bloomberg/main.js +1 -1
- package/clis/bloomberg/markets.js +1 -1
- package/clis/bloomberg/opinions.js +1 -1
- package/clis/bloomberg/politics.js +1 -1
- package/clis/bloomberg/tech.js +1 -1
- package/clis/boss/search.js +49 -8
- package/clis/boss/search.test.js +78 -0
- package/clis/boss/send.js +3 -3
- package/clis/chatgpt/image.js +37 -8
- package/clis/chatgpt/image.test.js +92 -0
- package/clis/chatgpt/utils.js +39 -6
- package/clis/chatgpt/utils.test.js +63 -0
- package/clis/chatgpt-app/ask.js +1 -1
- package/clis/chatgpt-app/ax.js +4 -2
- package/clis/chatgpt-app/ax.test.js +12 -0
- package/clis/chatgpt-app/model.js +1 -1
- package/clis/chatgpt-app/new.js +1 -1
- package/clis/chatgpt-app/read.js +1 -1
- package/clis/chatgpt-app/send.js +1 -1
- package/clis/chatgpt-app/status.js +1 -1
- package/clis/chatwise/ask.js +2 -2
- package/clis/chatwise/model.js +2 -2
- package/clis/chatwise/send.js +2 -2
- package/clis/claude/ask.js +128 -0
- package/clis/claude/ask.test.js +338 -0
- package/clis/claude/commands.test.js +118 -0
- package/clis/claude/detail.js +29 -0
- package/clis/claude/history.js +31 -0
- package/clis/claude/new.js +21 -0
- package/clis/claude/read.js +24 -0
- package/clis/claude/send.js +41 -0
- package/clis/claude/status.js +24 -0
- package/clis/claude/utils.js +440 -0
- package/clis/claude/utils.test.js +148 -0
- package/clis/codex/ask.js +2 -2
- package/clis/codex/send.js +2 -2
- package/clis/ctrip/search.js +1 -1
- package/clis/ctrip/search.test.js +4 -4
- package/clis/cursor/ask.js +2 -2
- package/clis/cursor/composer.js +2 -2
- package/clis/cursor/send.js +2 -2
- package/clis/deepseek/ask.js +17 -4
- package/clis/deepseek/ask.test.js +46 -0
- package/clis/deepseek/utils.js +55 -16
- package/clis/deepseek/utils.test.js +124 -5
- package/clis/doubao/utils.js +53 -11
- package/clis/doubao/utils.test.js +22 -2
- package/clis/eastmoney/announcement.js +1 -1
- package/clis/eastmoney/convertible.js +1 -1
- package/clis/eastmoney/etf.js +1 -1
- package/clis/eastmoney/holders.js +1 -1
- package/clis/eastmoney/index-board.js +1 -1
- package/clis/eastmoney/kline.js +1 -1
- package/clis/eastmoney/kuaixun.js +1 -1
- package/clis/eastmoney/longhu.js +1 -1
- package/clis/eastmoney/money-flow.js +1 -1
- package/clis/eastmoney/northbound.js +1 -1
- package/clis/eastmoney/quote.js +1 -1
- package/clis/eastmoney/rank.js +1 -1
- package/clis/eastmoney/sectors.js +1 -1
- package/clis/facebook/marketplace-inbox.js +83 -0
- package/clis/facebook/marketplace-listings.js +83 -0
- package/clis/facebook/marketplace.test.js +91 -0
- package/clis/google/news.js +1 -1
- package/clis/google/suggest.js +1 -1
- package/clis/google/trends.js +1 -1
- package/clis/google-scholar/cite.js +74 -0
- package/clis/google-scholar/cite.test.js +47 -0
- package/clis/google-scholar/profile.js +92 -0
- package/clis/google-scholar/profile.test.js +49 -0
- package/clis/google-scholar/search.js +1 -1
- package/clis/google-scholar/search.test.js +15 -0
- package/clis/hf/top.js +1 -1
- package/clis/instagram/collection-create.js +57 -0
- package/clis/instagram/saved.js +21 -7
- package/clis/jd/item.js +679 -47
- package/clis/jd/item.test.js +318 -7
- package/clis/jd/item.test.ts +517 -0
- package/clis/lesswrong/comments.js +1 -1
- package/clis/lesswrong/curated.js +1 -1
- package/clis/lesswrong/frontpage.js +1 -1
- package/clis/lesswrong/new.js +1 -1
- package/clis/lesswrong/read.js +1 -1
- package/clis/lesswrong/sequences.js +1 -1
- package/clis/lesswrong/shortform.js +1 -1
- package/clis/lesswrong/tag.js +1 -1
- package/clis/lesswrong/tags.js +1 -1
- package/clis/lesswrong/top-month.js +1 -1
- package/clis/lesswrong/top-week.js +1 -1
- package/clis/lesswrong/top-year.js +1 -1
- package/clis/lesswrong/top.js +1 -1
- package/clis/lesswrong/user-posts.js +1 -1
- package/clis/lesswrong/user.js +1 -1
- package/clis/paperreview/commands.test.js +6 -6
- package/clis/paperreview/feedback.js +1 -1
- package/clis/paperreview/review.js +1 -1
- package/clis/paperreview/submit.js +1 -1
- package/clis/producthunt/posts.js +1 -1
- package/clis/producthunt/today.js +1 -1
- package/clis/sinablog/search.js +1 -1
- package/clis/sinafinance/news.js +1 -1
- package/clis/sinafinance/stock.js +1 -1
- package/clis/sinafinance/stock.test.js +2 -2
- package/clis/spotify/spotify.js +6 -6
- package/clis/substack/search.js +1 -1
- package/clis/toutiao/articles.js +5 -6
- package/clis/toutiao/articles.test.js +22 -15
- package/clis/twitter/followers.js +2 -2
- package/clis/twitter/following.js +224 -73
- package/clis/twitter/following.test.js +277 -0
- package/clis/twitter/post.js +184 -47
- package/clis/twitter/post.test.js +114 -34
- package/clis/uiverse/_shared.js +63 -4
- package/clis/uiverse/_shared.test.js +7 -0
- package/clis/uiverse/code.js +1 -0
- package/clis/uiverse/navigation.test.js +12 -0
- package/clis/uiverse/preview.js +1 -0
- package/clis/web/read.js +319 -81
- package/clis/web/read.test.js +221 -5
- package/clis/weibo/favorites.js +169 -0
- package/clis/weibo/favorites.test.js +114 -0
- package/clis/weibo/publish.js +282 -0
- package/clis/weibo/publish.test.js +183 -0
- package/clis/weread/ranking.js +1 -1
- package/clis/weread/search-regression.test.js +8 -8
- package/clis/weread/search.js +1 -1
- package/clis/wikipedia/random.js +1 -1
- package/clis/wikipedia/search.js +1 -1
- package/clis/wikipedia/summary.js +1 -1
- package/clis/wikipedia/trending.js +1 -1
- package/clis/xianyu/chat.js +3 -3
- package/clis/xianyu/item.js +2 -2
- package/clis/xianyu/item.test.js +3 -3
- package/clis/xiaohongshu/search.js +17 -2
- package/clis/xiaohongshu/search.test.js +37 -1
- package/clis/xiaoyuzhou/download.js +1 -1
- package/clis/xiaoyuzhou/download.test.js +3 -3
- package/clis/xiaoyuzhou/episode.js +1 -1
- package/clis/xiaoyuzhou/podcast-episodes.js +1 -1
- package/clis/xiaoyuzhou/podcast-episodes.test.js +2 -2
- package/clis/xiaoyuzhou/podcast.js +1 -1
- package/clis/xiaoyuzhou/transcript.js +1 -1
- package/clis/xiaoyuzhou/transcript.test.js +5 -5
- package/clis/yollomi/models.js +1 -1
- package/clis/youtube/channel.js +24 -1
- package/clis/youtube/channel.test.js +59 -0
- package/clis/zhihu/answer.js +21 -162
- package/clis/zhihu/answer.test.js +26 -53
- package/clis/zhihu/collection.js +197 -0
- package/clis/zhihu/collection.test.js +290 -0
- package/clis/zhihu/collections.js +127 -0
- package/clis/zhihu/collections.test.js +182 -0
- package/clis/zhihu/comment.js +24 -305
- package/clis/zhihu/comment.test.js +31 -35
- package/clis/zhihu/favorite.js +44 -182
- package/clis/zhihu/favorite.test.js +30 -167
- package/clis/zhihu/follow.js +25 -56
- package/clis/zhihu/follow.test.js +20 -23
- package/clis/zhihu/like.js +22 -67
- package/clis/zhihu/like.test.js +19 -42
- package/clis/zhihu/search.js +3 -2
- package/clis/zhihu/write-shared.js +8 -1
- package/clis/zhihu/write-shared.test.js +1 -0
- package/clis/zlibrary/commands.test.js +75 -0
- package/clis/zlibrary/info.js +47 -0
- package/clis/zlibrary/search.js +46 -0
- package/clis/zlibrary/utils.js +136 -0
- package/dist/src/adapter-source.d.ts +11 -0
- package/dist/src/adapter-source.js +24 -0
- package/dist/src/adapter-source.test.js +29 -0
- package/dist/src/browser/base-page.d.ts +3 -1
- package/dist/src/browser/base-page.js +76 -1
- package/dist/src/browser/base-page.test.d.ts +1 -0
- package/dist/src/browser/base-page.test.js +74 -0
- package/dist/src/browser/bridge.d.ts +1 -2
- package/dist/src/browser/bridge.js +40 -41
- package/dist/src/browser/cdp.d.ts +1 -0
- package/dist/src/browser/cdp.js +3 -3
- package/dist/src/browser/daemon-client.d.ts +38 -4
- package/dist/src/browser/daemon-client.js +24 -7
- package/dist/src/browser/daemon-client.test.js +49 -0
- package/dist/src/browser/daemon-lifecycle.d.ts +23 -0
- package/dist/src/browser/daemon-lifecycle.js +67 -0
- package/dist/src/browser/daemon-version.d.ts +4 -0
- package/dist/src/browser/daemon-version.js +12 -0
- package/dist/src/browser/errors.js +3 -0
- package/dist/src/browser/errors.test.js +3 -0
- package/dist/src/browser/network-cache.d.ts +1 -0
- package/dist/src/browser/page.d.ts +3 -1
- package/dist/src/browser/page.js +10 -2
- package/dist/src/browser/profile.d.ts +14 -0
- package/dist/src/browser/profile.js +85 -0
- package/dist/src/build-manifest.d.ts +2 -0
- package/dist/src/build-manifest.js +13 -3
- package/dist/src/build-manifest.test.js +20 -2
- package/dist/src/cli.d.ts +6 -0
- package/dist/src/cli.js +477 -35
- package/dist/src/cli.test.js +303 -2
- package/dist/src/commanderAdapter.js +17 -9
- package/dist/src/commanderAdapter.test.js +67 -2
- package/dist/src/commands/daemon.d.ts +2 -0
- package/dist/src/commands/daemon.js +42 -1
- package/dist/src/commands/daemon.test.js +103 -2
- package/dist/src/completion-shared.js +1 -2
- package/dist/src/completion.test.js +3 -2
- package/dist/src/daemon.js +125 -41
- package/dist/src/doctor.d.ts +5 -6
- package/dist/src/doctor.js +77 -19
- package/dist/src/doctor.test.js +117 -0
- package/dist/src/engine.test.js +6 -5
- package/dist/src/errors.d.ts +14 -8
- package/dist/src/errors.js +36 -30
- package/dist/src/errors.test.js +5 -5
- package/dist/src/execution.d.ts +4 -0
- package/dist/src/execution.js +173 -25
- package/dist/src/execution.test.js +171 -1
- package/dist/src/main.js +10 -0
- package/dist/src/observation/artifact.d.ts +16 -0
- package/dist/src/observation/artifact.js +260 -0
- package/dist/src/observation/artifact.test.d.ts +1 -0
- package/dist/src/observation/artifact.test.js +121 -0
- package/dist/src/observation/events.d.ts +89 -0
- package/dist/src/observation/events.js +1 -0
- package/dist/src/observation/index.d.ts +7 -0
- package/dist/src/observation/index.js +7 -0
- package/dist/src/observation/manager.d.ts +9 -0
- package/dist/src/observation/manager.js +27 -0
- package/dist/src/observation/manager.test.d.ts +1 -0
- package/dist/src/observation/manager.test.js +13 -0
- package/dist/src/observation/redaction.d.ts +11 -0
- package/dist/src/observation/redaction.js +81 -0
- package/dist/src/observation/redaction.test.d.ts +1 -0
- package/dist/src/observation/redaction.test.js +32 -0
- package/dist/src/observation/retention.d.ts +32 -0
- package/dist/src/observation/retention.js +160 -0
- package/dist/src/observation/retention.test.d.ts +1 -0
- package/dist/src/observation/retention.test.js +118 -0
- package/dist/src/observation/ring-buffer.d.ts +22 -0
- package/dist/src/observation/ring-buffer.js +45 -0
- package/dist/src/observation/ring-buffer.test.d.ts +1 -0
- package/dist/src/observation/ring-buffer.test.js +22 -0
- package/dist/src/observation/session.d.ts +25 -0
- package/dist/src/observation/session.js +50 -0
- package/dist/src/pipeline/executor.test.js +1 -0
- package/dist/src/pipeline/steps/download.test.js +1 -0
- package/dist/src/pipeline/steps/fetch.js +1 -21
- package/dist/src/pipeline/steps/fetch.test.js +6 -12
- package/dist/src/plugin-scaffold.js +1 -1
- package/dist/src/plugin-scaffold.test.js +1 -1
- package/dist/src/registry.d.ts +40 -9
- package/dist/src/registry.js +3 -1
- package/dist/src/runtime-detect.d.ts +10 -0
- package/dist/src/runtime-detect.js +19 -0
- package/dist/src/runtime-detect.test.js +12 -1
- package/dist/src/runtime.d.ts +2 -0
- package/dist/src/runtime.js +1 -0
- package/dist/src/types.d.ts +22 -0
- package/dist/src/update-check.d.ts +31 -1
- package/dist/src/update-check.js +62 -16
- package/dist/src/update-check.test.js +86 -1
- package/package.json +1 -1
- package/dist/src/diagnostic.d.ts +0 -63
- package/dist/src/diagnostic.js +0 -292
- package/dist/src/diagnostic.test.js +0 -302
- /package/dist/src/{diagnostic.test.d.ts → adapter-source.test.d.ts} +0 -0
package/clis/jd/item.js
CHANGED
|
@@ -5,16 +5,545 @@
|
|
|
5
5
|
* 用法: opencli jd item 100291143898
|
|
6
6
|
*/
|
|
7
7
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
8
|
+
import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
|
|
9
|
+
function normalizePositiveInt(value, fallback) {
|
|
10
|
+
const n = Number(value);
|
|
11
|
+
return Number.isFinite(n) && n >= 0 ? Math.floor(n) : fallback;
|
|
12
|
+
}
|
|
13
|
+
function normalizeJdSkuInput(input) {
|
|
14
|
+
const text = String(input || '').trim();
|
|
15
|
+
const itemMatch = text.match(/item\.jd\.com\/(\d+)\.html/i);
|
|
16
|
+
if (itemMatch)
|
|
17
|
+
return itemMatch[1];
|
|
18
|
+
if (/^\d+$/.test(text))
|
|
19
|
+
return text;
|
|
20
|
+
const paramMatch = text.match(/(?:skuId|sku|wareId|productId)[=:](\d+)/i);
|
|
21
|
+
if (paramMatch)
|
|
22
|
+
return paramMatch[1];
|
|
23
|
+
return text;
|
|
24
|
+
}
|
|
25
|
+
function normalizeJdImageUrl(rawUrl) {
|
|
26
|
+
if (!rawUrl || typeof rawUrl !== 'string')
|
|
27
|
+
return '';
|
|
28
|
+
let url = rawUrl.trim();
|
|
29
|
+
if (!url)
|
|
30
|
+
return '';
|
|
31
|
+
if (url.startsWith('//'))
|
|
32
|
+
url = `https:${url}`;
|
|
33
|
+
if (!/^https?:\/\//.test(url))
|
|
34
|
+
return '';
|
|
35
|
+
return url;
|
|
36
|
+
}
|
|
37
|
+
function normalizeJdImageSize(url) {
|
|
38
|
+
return normalizeJdImageUrl(url)
|
|
39
|
+
.replace(/\/pcpubliccms\/s\d+x\d+_jfs\//, '/pcpubliccms/jfs/')
|
|
40
|
+
.replace(/\/(n\d+)\/s\d+x\d+_jfs\//, '/$1/jfs/')
|
|
41
|
+
.replace(/\/s\d+x\d+_jfs\//, '/jfs/');
|
|
42
|
+
}
|
|
43
|
+
function isJdMainImage(url) {
|
|
44
|
+
const normalized = normalizeJdImageSize(url);
|
|
45
|
+
return /360buyimg\.com\/(?:pcpubliccms|n\d+)\/jfs\//.test(normalized) &&
|
|
46
|
+
!/\/(?:s\d+x\d+_|n\d\/s\d+x\d+_)/.test(normalized) &&
|
|
47
|
+
!/\/(?:imgzone|sku|shaidan|popWaterMark|babel|jdcms|cms|ddimg|vc)\//.test(normalized);
|
|
48
|
+
}
|
|
49
|
+
function collectImageUrlsFrom(root, options = {}) {
|
|
50
|
+
if (!root)
|
|
51
|
+
return [];
|
|
52
|
+
const urls = [];
|
|
53
|
+
const ignoredContexts = '#spec-list, [class*="spec-list"], [class*="recommend"], [id*="recommend"], [class*="shaidan"], [id*="shaidan"], [class*="review"], [id*="review"], [class*="comment"], [id*="comment"], [class*="thumb"], [id*="thumb"], [class*="related"], [id*="related"]';
|
|
54
|
+
const ignoreContexts = Boolean(options.ignoreContexts);
|
|
55
|
+
const isIgnoredContext = (el) => {
|
|
56
|
+
if (!ignoreContexts)
|
|
57
|
+
return false;
|
|
58
|
+
try {
|
|
59
|
+
return !!el?.closest?.(ignoredContexts);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const pushUrlsFromText = (text) => {
|
|
66
|
+
for (const match of String(text || '').matchAll(/url\(["']?([^"')]+360buyimg\.com[^"')]+)["']?\)/g)) {
|
|
67
|
+
push(match[1]);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const push = (value) => {
|
|
71
|
+
const url = normalizeJdImageUrl(value);
|
|
72
|
+
if (url && url.includes('360buyimg.com'))
|
|
73
|
+
urls.push(url);
|
|
74
|
+
};
|
|
75
|
+
for (const img of root.querySelectorAll?.('img') || []) {
|
|
76
|
+
if (isIgnoredContext(img))
|
|
77
|
+
continue;
|
|
78
|
+
push(img.currentSrc || img.src);
|
|
79
|
+
push(img.getAttribute('data-src'));
|
|
80
|
+
push(img.getAttribute('data-lazy-img'));
|
|
81
|
+
push(img.getAttribute('data-lazyload'));
|
|
82
|
+
push(img.getAttribute('data-original'));
|
|
83
|
+
}
|
|
84
|
+
for (const source of root.querySelectorAll?.('source') || []) {
|
|
85
|
+
if (isIgnoredContext(source))
|
|
86
|
+
continue;
|
|
87
|
+
push(source.getAttribute('src'));
|
|
88
|
+
push(source.getAttribute('srcset')?.split(/\s+/)[0]);
|
|
89
|
+
push(source.getAttribute('data-src'));
|
|
90
|
+
push(source.getAttribute('data-srcset')?.split(/\s+/)[0]);
|
|
91
|
+
}
|
|
92
|
+
for (const el of root.querySelectorAll?.('[style*="360buyimg.com"]') || []) {
|
|
93
|
+
if (isIgnoredContext(el))
|
|
94
|
+
continue;
|
|
95
|
+
const style = el.getAttribute('style') || '';
|
|
96
|
+
pushUrlsFromText(style);
|
|
97
|
+
}
|
|
98
|
+
if (typeof getComputedStyle === 'function') {
|
|
99
|
+
const elements = [root, ...Array.from(root.querySelectorAll?.('*') || [])];
|
|
100
|
+
for (const el of elements) {
|
|
101
|
+
if (isIgnoredContext(el))
|
|
102
|
+
continue;
|
|
103
|
+
try {
|
|
104
|
+
const style = getComputedStyle(el);
|
|
105
|
+
pushUrlsFromText(style?.backgroundImage);
|
|
106
|
+
pushUrlsFromText(style?.background);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
// ignore inaccessible/computed-style edge cases in the page context
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return [...new Set(urls)];
|
|
114
|
+
}
|
|
115
|
+
function collectImageUrlsFromText(text) {
|
|
116
|
+
const urls = [];
|
|
117
|
+
const push = (value) => {
|
|
118
|
+
const url = normalizeJdImageUrl(value);
|
|
119
|
+
if (url && url.includes('360buyimg.com'))
|
|
120
|
+
urls.push(url);
|
|
121
|
+
};
|
|
122
|
+
for (const match of String(text || '').matchAll(/(?:https?:)?\/\/[^"'`\s<>)\\]+360buyimg\.com[^"'`\s<>)\\]*/g)) {
|
|
123
|
+
push(match[0]);
|
|
124
|
+
}
|
|
125
|
+
for (const match of String(text || '').matchAll(/url\(["']?([^"')]+360buyimg\.com[^"')]+)["']?\)/g)) {
|
|
126
|
+
push(match[1]);
|
|
127
|
+
}
|
|
128
|
+
return urls;
|
|
129
|
+
}
|
|
130
|
+
function collectImageUrlsFromFramesAndScripts() {
|
|
131
|
+
const urls = [];
|
|
132
|
+
for (const script of document.scripts || []) {
|
|
133
|
+
const text = script.textContent || '';
|
|
134
|
+
if (!/360buyimg\.com/.test(text))
|
|
135
|
+
continue;
|
|
136
|
+
urls.push(...collectImageUrlsFromText(text));
|
|
137
|
+
}
|
|
138
|
+
for (const iframe of document.querySelectorAll('iframe')) {
|
|
139
|
+
try {
|
|
140
|
+
const frameDoc = iframe.contentDocument || iframe.contentWindow?.document;
|
|
141
|
+
if (!frameDoc)
|
|
142
|
+
continue;
|
|
143
|
+
urls.push(...collectImageUrlsFrom(frameDoc.body || frameDoc.documentElement || frameDoc));
|
|
144
|
+
for (const script of frameDoc.scripts || []) {
|
|
145
|
+
const text = script.textContent || '';
|
|
146
|
+
if (!/360buyimg\.com/.test(text))
|
|
147
|
+
continue;
|
|
148
|
+
urls.push(...collectImageUrlsFromText(text));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// ignore cross-origin or not-yet-loaded iframe content
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return [...new Set(urls)];
|
|
156
|
+
}
|
|
157
|
+
function collectImageUrlsFromPayload(payload, seen = new Set(), depth = 0) {
|
|
158
|
+
if (payload == null || depth > 4)
|
|
159
|
+
return [];
|
|
160
|
+
const urls = [];
|
|
161
|
+
const push = (value) => {
|
|
162
|
+
const url = normalizeJdImageUrl(value);
|
|
163
|
+
if (url && url.includes('360buyimg.com'))
|
|
164
|
+
urls.push(url);
|
|
165
|
+
};
|
|
166
|
+
if (typeof payload === 'string') {
|
|
167
|
+
urls.push(...collectImageUrlsFromText(payload));
|
|
168
|
+
return [...new Set(urls)];
|
|
169
|
+
}
|
|
170
|
+
if (typeof payload !== 'object')
|
|
171
|
+
return [];
|
|
172
|
+
if (seen.has(payload))
|
|
173
|
+
return [];
|
|
174
|
+
seen.add(payload);
|
|
175
|
+
if (Array.isArray(payload)) {
|
|
176
|
+
for (const item of payload)
|
|
177
|
+
urls.push(...collectImageUrlsFromPayload(item, seen, depth + 1));
|
|
178
|
+
return [...new Set(urls)];
|
|
179
|
+
}
|
|
180
|
+
for (const value of Object.values(payload)) {
|
|
181
|
+
if (typeof value === 'string') {
|
|
182
|
+
push(value);
|
|
183
|
+
urls.push(...collectImageUrlsFromText(value));
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
urls.push(...collectImageUrlsFromPayload(value, seen, depth + 1));
|
|
187
|
+
}
|
|
188
|
+
return [...new Set(urls)];
|
|
189
|
+
}
|
|
190
|
+
function collectImageUrlsFromPageDataObjects() {
|
|
191
|
+
const urls = [];
|
|
192
|
+
const keys = ['__NEXT_DATA__', '__NUXT__', '__INITIAL_STATE__', '__INITIAL_DATA__', '__APOLLO_STATE__', '__INITIAL_PROPS__', '__REDUX_STATE__', '__PAGE_DATA__', 'pageData', '__data__', 'detailData'];
|
|
193
|
+
for (const key of keys) {
|
|
194
|
+
try {
|
|
195
|
+
if (typeof globalThis !== 'undefined' && key in globalThis) {
|
|
196
|
+
urls.push(...collectImageUrlsFromPayload(globalThis[key]));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
// ignore inaccessible globals
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return [...new Set(urls)];
|
|
204
|
+
}
|
|
205
|
+
async function collectImageUrlsFromNetworkResources() {
|
|
206
|
+
const urls = [];
|
|
207
|
+
const entries = typeof performance !== 'undefined' && typeof performance.getEntriesByType === 'function'
|
|
208
|
+
? performance.getEntriesByType('resource')
|
|
209
|
+
: [];
|
|
210
|
+
const candidates = [...new Set(entries
|
|
211
|
+
.map((entry) => entry?.name)
|
|
212
|
+
.filter((name) => typeof name === 'string' && /(?:jd\.com|360buyimg\.com)/.test(name) && /(?:detail|desc|ware|item|product|sku)/i.test(name) && !/pc_item_getWareGraphic/i.test(name)))].slice(0, 12);
|
|
213
|
+
for (const url of candidates) {
|
|
214
|
+
try {
|
|
215
|
+
const resp = await fetch(url, { credentials: 'include' });
|
|
216
|
+
if (!resp.ok)
|
|
217
|
+
continue;
|
|
218
|
+
const text = await resp.text();
|
|
219
|
+
const contentType = resp.headers.get('content-type') || '';
|
|
220
|
+
if (/json/i.test(contentType) || /^\s*[\[{]/.test(text)) {
|
|
221
|
+
try {
|
|
222
|
+
urls.push(...collectImageUrlsFromPayload(JSON.parse(text)));
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
// fall back to text scanning
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
urls.push(...collectImageUrlsFromText(text));
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
// ignore request failures and cross-origin oddities
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return [...new Set(urls)];
|
|
236
|
+
}
|
|
237
|
+
function collectImageUrlsFromWareGraphicText(text) {
|
|
238
|
+
let graphicContent = '';
|
|
239
|
+
try {
|
|
240
|
+
const payload = JSON.parse(String(text || ''));
|
|
241
|
+
graphicContent = payload?.data?.graphicContent || payload?.graphicContent || '';
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
graphicContent = String(text || '');
|
|
245
|
+
}
|
|
246
|
+
return [...new Set(collectImageUrlsFromText(graphicContent))];
|
|
247
|
+
}
|
|
248
|
+
async function collectImageUrlsFromWareGraphicResources(sku) {
|
|
249
|
+
const urls = [];
|
|
250
|
+
const entries = typeof performance !== 'undefined' && typeof performance.getEntriesByType === 'function'
|
|
251
|
+
? performance.getEntriesByType('resource')
|
|
252
|
+
: [];
|
|
253
|
+
const candidates = [...new Set(entries
|
|
254
|
+
.map((entry) => entry?.name)
|
|
255
|
+
.filter((name) => typeof name === 'string' && /pc_item_getWareGraphic/i.test(name)))];
|
|
256
|
+
if (sku) {
|
|
257
|
+
const body = encodeURIComponent(JSON.stringify({ skuId: String(sku) }));
|
|
258
|
+
candidates.unshift(`https://api.m.jd.com/client.action?appid=item-v3&functionId=pc_item_getWareGraphic&client=pc&clientVersion=1.0.0&body=${body}`);
|
|
259
|
+
}
|
|
260
|
+
for (const url of candidates) {
|
|
261
|
+
try {
|
|
262
|
+
const resp = await fetch(url, { credentials: 'include' });
|
|
263
|
+
if (!resp.ok)
|
|
264
|
+
continue;
|
|
265
|
+
urls.push(...collectImageUrlsFromWareGraphicText(await resp.text()));
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
// ignore request failures and keep DOM/script fallbacks available
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return [...new Set(urls)];
|
|
272
|
+
}
|
|
273
|
+
async function collectImageUrlsFromFallbackSources() {
|
|
274
|
+
return [
|
|
275
|
+
...collectImageUrlsFromPageDataObjects(),
|
|
276
|
+
...await collectImageUrlsFromNetworkResources(),
|
|
277
|
+
];
|
|
278
|
+
}
|
|
279
|
+
function isJdDetailImage(url) {
|
|
280
|
+
const normalized = normalizeJdImageSize(url);
|
|
281
|
+
return /360buyimg\.com\/(?:imgzone|skuimg|babel|jdcms|cms|popWaterMark|vc|ddimg)\//.test(normalized) &&
|
|
282
|
+
!/\/shaidan\//.test(normalized) &&
|
|
283
|
+
!/\/(?:s\d+x\d+_|n\d\/s\d+x\d+_|sku)\//.test(normalized);
|
|
284
|
+
}
|
|
285
|
+
function isJdWareGraphicDetailImage(url) {
|
|
286
|
+
const raw = normalizeJdImageUrl(url);
|
|
287
|
+
if (/\/(?:s\d+x\d+_|n\d\/s\d+x\d+_|sku\/s\d+x\d+_)jfs\//.test(raw))
|
|
288
|
+
return false;
|
|
289
|
+
const normalized = normalizeJdImageSize(url);
|
|
290
|
+
return /360buyimg\.com\/sku\/jfs\//.test(normalized) &&
|
|
291
|
+
!/\/shaidan\//.test(normalized);
|
|
292
|
+
}
|
|
293
|
+
function rankJdDetailImage(url) {
|
|
294
|
+
const normalized = normalizeJdImageSize(url);
|
|
295
|
+
if (/\.jpe?g(?:\.avif)?(?:$|[?#])/.test(normalized))
|
|
296
|
+
return 0;
|
|
297
|
+
if (/\.(?:png|webp)(?:\.avif)?(?:$|[?#])/.test(normalized))
|
|
298
|
+
return 1;
|
|
299
|
+
if (/\.gif(?:$|[?#])/.test(normalized))
|
|
300
|
+
return 3;
|
|
301
|
+
if (/\.avif(?:$|[?#])/.test(normalized))
|
|
302
|
+
return 2;
|
|
303
|
+
return 4;
|
|
304
|
+
}
|
|
305
|
+
function orderJdDetailImages(urls, options = {}) {
|
|
306
|
+
const allowWareGraphicSku = Boolean(options.allowWareGraphicSku);
|
|
307
|
+
return [...new Set(urls)]
|
|
308
|
+
.filter((url) => isJdDetailImage(url) || (allowWareGraphicSku && isJdWareGraphicDetailImage(url)))
|
|
309
|
+
.map((url, index) => ({ url, index, rank: rankJdDetailImage(url) }))
|
|
310
|
+
.sort((a, b) => a.rank - b.rank || a.index - b.index)
|
|
311
|
+
.map((item) => item.url);
|
|
312
|
+
}
|
|
313
|
+
function extractDetailImagesFromDom(maxImages) {
|
|
314
|
+
const detailTitleParent = document.querySelector('#SPXQ-title')?.parentElement;
|
|
315
|
+
const safeDetailTitleParent = detailTitleParent && detailTitleParent !== document.body && detailTitleParent !== document.documentElement
|
|
316
|
+
? detailTitleParent
|
|
317
|
+
: null;
|
|
318
|
+
const selectorRoots = [
|
|
319
|
+
'#J-detail',
|
|
320
|
+
'#J-detail-content',
|
|
321
|
+
'#detail',
|
|
322
|
+
'.detail',
|
|
323
|
+
'.detail-content',
|
|
324
|
+
'.detail-content-wrap',
|
|
325
|
+
'.ssd-module-wrap',
|
|
326
|
+
'#SPXQ-title + *',
|
|
327
|
+
];
|
|
328
|
+
const scopedRoots = [
|
|
329
|
+
safeDetailTitleParent,
|
|
330
|
+
...selectorRoots.flatMap((selector) => Array.from(document.querySelectorAll(selector))),
|
|
331
|
+
].filter((root) => root && root !== document.body && root !== document.documentElement);
|
|
332
|
+
const scoped = [
|
|
333
|
+
...scopedRoots.flatMap((root) => collectImageUrlsFrom(root, { ignoreContexts: true })),
|
|
334
|
+
...collectImageUrlsFromFramesAndScripts(),
|
|
335
|
+
];
|
|
336
|
+
return orderJdDetailImages(scoped).slice(0, maxImages);
|
|
337
|
+
}
|
|
338
|
+
async function extractDetailImagesFromPage(maxImages, sku) {
|
|
339
|
+
const scoped = extractDetailImagesFromDom(maxImages);
|
|
340
|
+
const fallback = await collectImageUrlsFromFallbackSources();
|
|
341
|
+
const wareGraphic = await collectImageUrlsFromWareGraphicResources(sku);
|
|
342
|
+
const regularImages = orderJdDetailImages([...scoped, ...fallback]);
|
|
343
|
+
const wareGraphicImages = orderJdDetailImages(wareGraphic, { allowWareGraphicSku: true });
|
|
344
|
+
return orderJdDetailImages([...regularImages, ...wareGraphicImages], { allowWareGraphicSku: true }).slice(0, maxImages);
|
|
345
|
+
}
|
|
346
|
+
function getJdDetailScrollSnapshot(maxImages) {
|
|
347
|
+
const doc = document.scrollingElement || document.documentElement || document.body;
|
|
348
|
+
const scrollY = window.scrollY || window.pageYOffset || doc?.scrollTop || 0;
|
|
349
|
+
const viewportHeight = window.innerHeight || document.documentElement?.clientHeight || 0;
|
|
350
|
+
const scrollHeight = Math.max(doc?.scrollHeight || 0, document.documentElement?.scrollHeight || 0, document.body?.scrollHeight || 0);
|
|
351
|
+
return {
|
|
352
|
+
detailImageCount: extractDetailImagesFromDom(maxImages).length,
|
|
353
|
+
scrollY,
|
|
354
|
+
viewportHeight,
|
|
355
|
+
scrollHeight,
|
|
356
|
+
nearBottom: scrollY + viewportHeight >= scrollHeight - 120,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function scrollJdDetailStep() {
|
|
360
|
+
const step = Math.max(900, Math.floor((window.innerHeight || 900) * 0.9));
|
|
361
|
+
window.scrollBy(0, step);
|
|
362
|
+
return step;
|
|
363
|
+
}
|
|
364
|
+
async function scrollJdDetailIntoView() {
|
|
365
|
+
const tab = document.querySelector('#SPXQ-tab-column');
|
|
366
|
+
const title = document.querySelector('#SPXQ-title');
|
|
367
|
+
if (tab)
|
|
368
|
+
tab.click();
|
|
369
|
+
title?.scrollIntoView({ block: 'start' });
|
|
370
|
+
const step = Math.max(900, Math.floor((window.innerHeight || 900) * 0.9));
|
|
371
|
+
for (let i = 0; i < 2; i++) {
|
|
372
|
+
window.scrollBy(0, step);
|
|
373
|
+
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
function extractMainImages(maxImages) {
|
|
377
|
+
const roots = [
|
|
378
|
+
document.querySelector('._gallery_116km_1'),
|
|
379
|
+
...Array.from(document.querySelectorAll('[class*="_gallery_"]')),
|
|
380
|
+
document.querySelector('.preview-wrap'),
|
|
381
|
+
document.querySelector('#spec-img')?.parentElement,
|
|
382
|
+
].filter(Boolean);
|
|
383
|
+
const urls = roots.flatMap((root) => collectImageUrlsFrom(root).map(normalizeJdImageSize));
|
|
384
|
+
return [...new Set(urls)]
|
|
385
|
+
.filter(isJdMainImage)
|
|
386
|
+
.slice(0, maxImages);
|
|
387
|
+
}
|
|
8
388
|
function extractAvifImages(imageUrls, maxImages) {
|
|
9
|
-
const unique = [...new Set(imageUrls.filter(Boolean))];
|
|
389
|
+
const unique = [...new Set(imageUrls.map(normalizeJdImageSize).filter(Boolean))];
|
|
10
390
|
return unique
|
|
11
|
-
.filter((url) => url.includes('.avif') && url
|
|
391
|
+
.filter((url) => url.includes('.avif') && isJdDetailImage(url))
|
|
12
392
|
.slice(0, maxImages);
|
|
13
393
|
}
|
|
394
|
+
function extractPriceFromPayload(payload) {
|
|
395
|
+
const items = Array.isArray(payload) ? payload : [];
|
|
396
|
+
const item = items.find((entry) => entry && typeof entry === 'object');
|
|
397
|
+
for (const key of ['p', 'op', 'm']) {
|
|
398
|
+
const value = item?.[key];
|
|
399
|
+
if (value && value !== '-1.00')
|
|
400
|
+
return String(value);
|
|
401
|
+
}
|
|
402
|
+
return '';
|
|
403
|
+
}
|
|
404
|
+
function normalizePriceText(text) {
|
|
405
|
+
const match = String(text || '').replace(/\s+/g, '').match(/(?:¥|¥)?(\d{2,7}(?:\.\d{1,2})?)/);
|
|
406
|
+
return match ? match[1] : '';
|
|
407
|
+
}
|
|
408
|
+
function extractPriceFromDom(sku) {
|
|
409
|
+
const selectors = [
|
|
410
|
+
`.J-p-${sku}`,
|
|
411
|
+
'[class*="price"] [class*="num"]',
|
|
412
|
+
'[class*="price"]',
|
|
413
|
+
'.p-price strong',
|
|
414
|
+
'.price.jd-price',
|
|
415
|
+
];
|
|
416
|
+
for (const selector of selectors) {
|
|
417
|
+
for (const el of document.querySelectorAll(selector)) {
|
|
418
|
+
const price = normalizePriceText(el.textContent || '');
|
|
419
|
+
if (price)
|
|
420
|
+
return price;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
for (const el of document.querySelectorAll('span, strong, div')) {
|
|
424
|
+
const text = (el.textContent || '').trim();
|
|
425
|
+
if (!/预售价|到手价|秒杀价|京东价|¥|¥/.test(text))
|
|
426
|
+
continue;
|
|
427
|
+
const direct = normalizePriceText(text);
|
|
428
|
+
if (direct)
|
|
429
|
+
return direct;
|
|
430
|
+
const parentPrice = normalizePriceText(el.parentElement?.textContent || '');
|
|
431
|
+
if (parentPrice)
|
|
432
|
+
return parentPrice;
|
|
433
|
+
}
|
|
434
|
+
return '';
|
|
435
|
+
}
|
|
436
|
+
async function fetchJdPrice(sku) {
|
|
437
|
+
const controller = new AbortController();
|
|
438
|
+
const timeout = setTimeout(() => controller.abort(), 3000);
|
|
439
|
+
try {
|
|
440
|
+
const resp = await fetch(`https://p.3.cn/prices/mgets?skuIds=J_${encodeURIComponent(sku)}&type=1`, {
|
|
441
|
+
credentials: 'include',
|
|
442
|
+
signal: controller.signal,
|
|
443
|
+
});
|
|
444
|
+
if (!resp.ok)
|
|
445
|
+
return '';
|
|
446
|
+
return extractPriceFromPayload(await resp.json());
|
|
447
|
+
}
|
|
448
|
+
catch {
|
|
449
|
+
return '';
|
|
450
|
+
}
|
|
451
|
+
finally {
|
|
452
|
+
clearTimeout(timeout);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
function extractSpecsFromText(text) {
|
|
456
|
+
const specs = {};
|
|
457
|
+
const lines = String(text || '').split('\n').map((line) => line.trim()).filter(Boolean);
|
|
458
|
+
const allowedKeys = new Set(['品牌', '商品名称', '商品编号', '商品毛重', '商品产地', '货号', '类型', '能效等级', '洗涤容量', '烘干容量', '排水方式', '颜色', '型号', '系列', '系列品', '款式', '版本', '规格', '容量']);
|
|
459
|
+
const setSpec = (key, val) => {
|
|
460
|
+
const normalizedKey = String(key || '').trim().replace(/[::]+$/, '');
|
|
461
|
+
const normalizedVal = String(val || '').trim();
|
|
462
|
+
if (!allowedKeys.has(normalizedKey))
|
|
463
|
+
return;
|
|
464
|
+
if (!normalizedVal || normalizedVal.length > 120)
|
|
465
|
+
return;
|
|
466
|
+
if (/^(服务|支付定金|加入购物车|立即购买|首页|购物车|我的|客服|品牌闪购|以旧换新)$/.test(normalizedVal))
|
|
467
|
+
return;
|
|
468
|
+
if (!specs[normalizedKey])
|
|
469
|
+
specs[normalizedKey] = normalizedVal;
|
|
470
|
+
};
|
|
471
|
+
for (const line of lines) {
|
|
472
|
+
const compactMatch = line.match(/^([^::]{1,12})[::]\s*(.{1,120})$/);
|
|
473
|
+
if (compactMatch) {
|
|
474
|
+
setSpec(compactMatch[1], compactMatch[2]);
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
479
|
+
const key = lines[i].replace(/[::]+$/, '');
|
|
480
|
+
const val = lines[i + 1];
|
|
481
|
+
if (allowedKeys.has(key) && !allowedKeys.has(val)) {
|
|
482
|
+
setSpec(key, val);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return specs;
|
|
486
|
+
}
|
|
487
|
+
function extractSpecs() {
|
|
488
|
+
const specs = {};
|
|
489
|
+
const setSpec = (label, value) => {
|
|
490
|
+
const normalizedLabel = String(label || '').trim().replace(/[::]+$/, '');
|
|
491
|
+
const normalizedValue = String(value || '').replace(/\s+/g, ' ').trim();
|
|
492
|
+
if (!normalizedLabel || !normalizedValue)
|
|
493
|
+
return;
|
|
494
|
+
specs[normalizedLabel] = normalizedValue;
|
|
495
|
+
};
|
|
496
|
+
const selectedText = (root) => {
|
|
497
|
+
const selected = root.querySelector('.specification-item-sku--selected, .specification-series-item--selected, .selected, [class*="selected"]');
|
|
498
|
+
if (!selected)
|
|
499
|
+
return '';
|
|
500
|
+
return selected.querySelector('[class*="text"]')?.textContent?.trim() ||
|
|
501
|
+
selected.textContent?.trim() ||
|
|
502
|
+
selected.querySelector('img')?.getAttribute('alt')?.trim() ||
|
|
503
|
+
'';
|
|
504
|
+
};
|
|
505
|
+
for (const el of document.querySelectorAll('.specification-series-layout')) {
|
|
506
|
+
const label = el.querySelector('.layout-label')?.textContent?.trim();
|
|
507
|
+
setSpec(label, selectedText(el));
|
|
508
|
+
}
|
|
509
|
+
for (const el of document.querySelectorAll('.specification-group')) {
|
|
510
|
+
const label = el.querySelector('.specification-label, .specification-group-label, .label')?.textContent?.trim();
|
|
511
|
+
setSpec(label, selectedText(el));
|
|
512
|
+
}
|
|
513
|
+
const attrsRoot = document.querySelector('#SPXQ-title')?.parentElement?.querySelector('.attrs') ||
|
|
514
|
+
document.querySelector('#parameter2') ||
|
|
515
|
+
document.querySelector('.Ptable');
|
|
516
|
+
if (attrsRoot) {
|
|
517
|
+
Object.assign(specs, extractSpecsFromText(attrsRoot.innerText || attrsRoot.textContent || ''));
|
|
518
|
+
}
|
|
519
|
+
return specs;
|
|
520
|
+
}
|
|
521
|
+
function detectJdPageState(expectedSku) {
|
|
522
|
+
const href = location.href;
|
|
523
|
+
const title = document.title || '';
|
|
524
|
+
const bodyText = document.body?.innerText || document.body?.textContent || '';
|
|
525
|
+
const hasProductMarker = Boolean(document.querySelector('.product-title, .sku-title, #spec-list, #J-detail, #SPXQ-title, [class*="_gallery_"]'));
|
|
526
|
+
const text = `${title}\n${bodyText}`;
|
|
527
|
+
const isLoginPage = /passport\.jd\.com|\/login\.aspx/.test(href) || /京东-欢迎登录|京东登录/.test(title);
|
|
528
|
+
const hasSecurityChallenge = /risk_handler|安全验证|安全校验|完成安全验证|滑块|captcha|访问过于频繁|验证中心|京东验证/.test(`${href}\n${text}`);
|
|
529
|
+
const loginOnlyWithoutProduct = /请登录|登录/.test(text) && !hasProductMarker;
|
|
530
|
+
const looksBlocked = isLoginPage || hasSecurityChallenge || loginOnlyWithoutProduct;
|
|
531
|
+
const onExpectedItemUrl = new RegExp(`item\.jd\.com/${expectedSku}\.html`).test(href);
|
|
532
|
+
return {
|
|
533
|
+
href,
|
|
534
|
+
title,
|
|
535
|
+
isProductPage: hasProductMarker && onExpectedItemUrl && !looksBlocked,
|
|
536
|
+
hasProductMarker,
|
|
537
|
+
onExpectedItemUrl,
|
|
538
|
+
looksBlocked,
|
|
539
|
+
isLoginPage,
|
|
540
|
+
hasSecurityChallenge,
|
|
541
|
+
};
|
|
542
|
+
}
|
|
14
543
|
cli({
|
|
15
544
|
site: 'jd',
|
|
16
545
|
name: 'item',
|
|
17
|
-
description: '
|
|
546
|
+
description: '京东商品详情(价格、店铺、规格参数、主图、详情图)',
|
|
18
547
|
domain: 'item.jd.com',
|
|
19
548
|
strategy: Strategy.COOKIE,
|
|
20
549
|
args: [
|
|
@@ -27,71 +556,174 @@ cli({
|
|
|
27
556
|
{
|
|
28
557
|
name: 'images',
|
|
29
558
|
type: 'int',
|
|
30
|
-
default:
|
|
31
|
-
help: '
|
|
559
|
+
default: 200,
|
|
560
|
+
help: '图片数量上限(默认200)',
|
|
32
561
|
},
|
|
33
562
|
],
|
|
34
|
-
columns: ['title', 'price', 'shop', 'specs', '
|
|
563
|
+
columns: ['title', 'price', 'shop', 'specs', 'mainImages', 'detailImages'],
|
|
35
564
|
func: async (page, kwargs) => {
|
|
36
|
-
const sku = kwargs.sku;
|
|
37
|
-
const maxImages = kwargs.images;
|
|
565
|
+
const sku = normalizeJdSkuInput(kwargs.sku);
|
|
566
|
+
const maxImages = normalizePositiveInt(kwargs.images, 200);
|
|
38
567
|
const url = `https://item.jd.com/${sku}.html`;
|
|
39
|
-
await page.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
568
|
+
const currentHref = await page.evaluate(`location.href`).catch(() => '');
|
|
569
|
+
if (!currentHref.includes(`item.jd.com/${sku}.html`)) {
|
|
570
|
+
await page.goto(url, { waitUntil: 'load' });
|
|
571
|
+
await page.wait(2);
|
|
572
|
+
}
|
|
573
|
+
const initialState = await page.evaluate(`(() => {
|
|
574
|
+
const detectJdPageState = ${detectJdPageState.toString()};
|
|
575
|
+
return detectJdPageState(${JSON.stringify(sku)});
|
|
576
|
+
})()`).catch(() => null);
|
|
577
|
+
if (!initialState?.looksBlocked) {
|
|
578
|
+
await page.evaluate(`
|
|
579
|
+
(async () => {
|
|
580
|
+
const scrollJdDetailIntoView = ${scrollJdDetailIntoView.toString()};
|
|
581
|
+
return scrollJdDetailIntoView();
|
|
582
|
+
})()
|
|
583
|
+
`);
|
|
584
|
+
await page.wait(1.5);
|
|
585
|
+
let previousDetailImageCount = -1;
|
|
586
|
+
let stableRounds = 0;
|
|
587
|
+
for (let i = 0; i < 30; i++) {
|
|
588
|
+
const snapshot = await page.evaluate(`(() => {
|
|
589
|
+
const normalizeJdImageUrl = ${normalizeJdImageUrl.toString()};
|
|
590
|
+
const normalizeJdImageSize = ${normalizeJdImageSize.toString()};
|
|
591
|
+
const collectImageUrlsFrom = ${collectImageUrlsFrom.toString()};
|
|
592
|
+
const collectImageUrlsFromText = ${collectImageUrlsFromText.toString()};
|
|
593
|
+
const collectImageUrlsFromFramesAndScripts = ${collectImageUrlsFromFramesAndScripts.toString()};
|
|
594
|
+
const collectImageUrlsFromPayload = ${collectImageUrlsFromPayload.toString()};
|
|
595
|
+
const collectImageUrlsFromPageDataObjects = ${collectImageUrlsFromPageDataObjects.toString()};
|
|
596
|
+
const collectImageUrlsFromNetworkResources = ${collectImageUrlsFromNetworkResources.toString()};
|
|
597
|
+
const collectImageUrlsFromWareGraphicText = ${collectImageUrlsFromWareGraphicText.toString()};
|
|
598
|
+
const collectImageUrlsFromWareGraphicResources = ${collectImageUrlsFromWareGraphicResources.toString()};
|
|
599
|
+
const collectImageUrlsFromFallbackSources = ${collectImageUrlsFromFallbackSources.toString()};
|
|
600
|
+
const isJdDetailImage = ${isJdDetailImage.toString()};
|
|
601
|
+
const isJdWareGraphicDetailImage = ${isJdWareGraphicDetailImage.toString()};
|
|
602
|
+
const rankJdDetailImage = ${rankJdDetailImage.toString()};
|
|
603
|
+
const orderJdDetailImages = ${orderJdDetailImages.toString()};
|
|
604
|
+
const extractDetailImagesFromDom = ${extractDetailImagesFromDom.toString()};
|
|
605
|
+
const extractDetailImagesFromPage = ${extractDetailImagesFromPage.toString()};
|
|
606
|
+
const getJdDetailScrollSnapshot = ${getJdDetailScrollSnapshot.toString()};
|
|
607
|
+
const scrollJdDetailIntoView = ${scrollJdDetailIntoView.toString()};
|
|
608
|
+
if (document.querySelector('#SPXQ-title') && window.scrollY < 1000) {
|
|
609
|
+
return scrollJdDetailIntoView().then(() => getJdDetailScrollSnapshot(${maxImages}));
|
|
610
|
+
}
|
|
611
|
+
return getJdDetailScrollSnapshot(${maxImages});
|
|
612
|
+
})()`).catch(() => null);
|
|
613
|
+
if (!snapshot)
|
|
614
|
+
break;
|
|
615
|
+
stableRounds = snapshot.detailImageCount === previousDetailImageCount ? stableRounds + 1 : 0;
|
|
616
|
+
previousDetailImageCount = snapshot.detailImageCount;
|
|
617
|
+
if (snapshot.nearBottom && stableRounds >= 2)
|
|
618
|
+
break;
|
|
619
|
+
await page.evaluate(`(() => {
|
|
620
|
+
const scrollJdDetailStep = ${scrollJdDetailStep.toString()};
|
|
621
|
+
return scrollJdDetailStep();
|
|
622
|
+
})()`);
|
|
623
|
+
await page.wait(0.8);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
48
626
|
const data = await page.evaluate(`
|
|
49
|
-
(() => {
|
|
627
|
+
(async () => {
|
|
50
628
|
const maxImg = ${maxImages};
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
629
|
+
const normalizeJdImageUrl = ${normalizeJdImageUrl.toString()};
|
|
630
|
+
const normalizeJdImageSize = ${normalizeJdImageSize.toString()};
|
|
631
|
+
const isJdMainImage = ${isJdMainImage.toString()};
|
|
632
|
+
const collectImageUrlsFrom = ${collectImageUrlsFrom.toString()};
|
|
633
|
+
const collectImageUrlsFromText = ${collectImageUrlsFromText.toString()};
|
|
634
|
+
const collectImageUrlsFromFramesAndScripts = ${collectImageUrlsFromFramesAndScripts.toString()};
|
|
635
|
+
const collectImageUrlsFromPayload = ${collectImageUrlsFromPayload.toString()};
|
|
636
|
+
const collectImageUrlsFromPageDataObjects = ${collectImageUrlsFromPageDataObjects.toString()};
|
|
637
|
+
const collectImageUrlsFromNetworkResources = ${collectImageUrlsFromNetworkResources.toString()};
|
|
638
|
+
const collectImageUrlsFromWareGraphicText = ${collectImageUrlsFromWareGraphicText.toString()};
|
|
639
|
+
const collectImageUrlsFromWareGraphicResources = ${collectImageUrlsFromWareGraphicResources.toString()};
|
|
640
|
+
const collectImageUrlsFromFallbackSources = ${collectImageUrlsFromFallbackSources.toString()};
|
|
641
|
+
const isJdDetailImage = ${isJdDetailImage.toString()};
|
|
642
|
+
const isJdWareGraphicDetailImage = ${isJdWareGraphicDetailImage.toString()};
|
|
643
|
+
const rankJdDetailImage = ${rankJdDetailImage.toString()};
|
|
644
|
+
const orderJdDetailImages = ${orderJdDetailImages.toString()};
|
|
645
|
+
const extractMainImages = ${extractMainImages.toString()};
|
|
646
|
+
const extractDetailImagesFromDom = ${extractDetailImagesFromDom.toString()};
|
|
647
|
+
const extractDetailImagesFromPage = ${extractDetailImagesFromPage.toString()};
|
|
648
|
+
const extractPriceFromPayload = ${extractPriceFromPayload.toString()};
|
|
649
|
+
const fetchJdPrice = ${fetchJdPrice.toString()};
|
|
650
|
+
const extractSpecsFromText = ${extractSpecsFromText.toString()};
|
|
651
|
+
const extractSpecs = ${extractSpecs.toString()};
|
|
652
|
+
const detectJdPageState = ${detectJdPageState.toString()};
|
|
653
|
+
const pageState = detectJdPageState(${JSON.stringify(sku)});
|
|
654
|
+
const normalizePriceText = ${normalizePriceText.toString()};
|
|
655
|
+
const extractPriceFromDom = ${extractPriceFromDom.toString()};
|
|
656
|
+
const apiPrice = await fetchJdPrice(${JSON.stringify(sku)});
|
|
657
|
+
const domPrice = extractPriceFromDom(${JSON.stringify(sku)});
|
|
658
|
+
const price = apiPrice || domPrice || 'not found';
|
|
59
659
|
|
|
60
|
-
// 标题
|
|
61
660
|
const title = document.querySelector('.product-title')?.textContent?.trim() ||
|
|
661
|
+
document.querySelector('.sku-title')?.textContent?.trim() ||
|
|
62
662
|
document.title.split('-')[0].trim();
|
|
63
663
|
|
|
64
|
-
|
|
65
|
-
|
|
664
|
+
const shop = document.querySelector('.J-shop-name')?.textContent?.trim() ||
|
|
665
|
+
document.querySelector('.top-name')?.textContent?.trim() ||
|
|
666
|
+
document.querySelector('[class*="shop"] [class*="name"]')?.textContent?.trim() ||
|
|
667
|
+
'京东自营';
|
|
66
668
|
|
|
67
|
-
// 所有图片
|
|
68
669
|
const allImgs = Array.from(document.querySelectorAll('img[src*="360buyimg.com"]'));
|
|
69
670
|
const srcs = allImgs.map(img => img.src).filter(Boolean);
|
|
70
671
|
|
|
71
|
-
|
|
72
|
-
const
|
|
672
|
+
const mainImages = extractMainImages(maxImg);
|
|
673
|
+
const detailImages = await extractDetailImagesFromPage(maxImg, ${JSON.stringify(sku)});
|
|
674
|
+
const specs = extractSpecs();
|
|
73
675
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
for (let i = 0; i < lines.length - 1; i += 2) {
|
|
81
|
-
const key = lines[i].trim();
|
|
82
|
-
const val = lines[i + 1]?.trim() || '';
|
|
83
|
-
if (key && val && key !== '商品编号') {
|
|
84
|
-
specs[key] = val;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
676
|
+
const result = { title, price, shop, specs, mainImages, detailImages, totalImages: new Set(srcs).size, pageState };
|
|
677
|
+
if (!pageState.isProductPage) {
|
|
678
|
+
result.error = pageState.looksBlocked
|
|
679
|
+
? 'JD page is blocked by login/security verification'
|
|
680
|
+
: 'JD product page was not loaded';
|
|
681
|
+
result.pageState = pageState;
|
|
87
682
|
}
|
|
88
|
-
|
|
89
|
-
return { title, price, shop, specs, avifImages, totalImages: new Set(srcs).size };
|
|
683
|
+
return result;
|
|
90
684
|
})()
|
|
91
685
|
`);
|
|
686
|
+
if (data?.error) {
|
|
687
|
+
if (data?.pageState?.looksBlocked) {
|
|
688
|
+
throw new AuthRequiredError('item.jd.com', data.error);
|
|
689
|
+
}
|
|
690
|
+
throw new CommandExecutionError(data.error);
|
|
691
|
+
}
|
|
692
|
+
if (maxImages > 0 && data?.pageState?.isProductPage && (!Array.isArray(data.detailImages) || data.detailImages.length === 0)) {
|
|
693
|
+
throw new CommandExecutionError('JD item detail images were not found', 'The product page loaded, but no detail images were detected from DOM, scripts, frames, page data, or WareGraphic fallback.');
|
|
694
|
+
}
|
|
92
695
|
return [data];
|
|
93
696
|
},
|
|
94
697
|
});
|
|
95
698
|
export const __test__ = {
|
|
699
|
+
normalizePositiveInt,
|
|
700
|
+
normalizeJdSkuInput,
|
|
701
|
+
normalizeJdImageUrl,
|
|
702
|
+
normalizeJdImageSize,
|
|
703
|
+
isJdMainImage,
|
|
704
|
+
collectImageUrlsFrom,
|
|
705
|
+
collectImageUrlsFromText,
|
|
706
|
+
collectImageUrlsFromFramesAndScripts,
|
|
707
|
+
collectImageUrlsFromPayload,
|
|
708
|
+
collectImageUrlsFromPageDataObjects,
|
|
709
|
+
collectImageUrlsFromNetworkResources,
|
|
710
|
+
collectImageUrlsFromWareGraphicText,
|
|
711
|
+
collectImageUrlsFromWareGraphicResources,
|
|
712
|
+
collectImageUrlsFromFallbackSources,
|
|
713
|
+
isJdDetailImage,
|
|
714
|
+
isJdWareGraphicDetailImage,
|
|
715
|
+
rankJdDetailImage,
|
|
716
|
+
orderJdDetailImages,
|
|
717
|
+
getJdDetailScrollSnapshot,
|
|
718
|
+
scrollJdDetailStep,
|
|
719
|
+
extractMainImages,
|
|
720
|
+
extractDetailImagesFromDom,
|
|
721
|
+
extractDetailImagesFromPage,
|
|
96
722
|
extractAvifImages,
|
|
723
|
+
extractPriceFromPayload,
|
|
724
|
+
normalizePriceText,
|
|
725
|
+
extractPriceFromDom,
|
|
726
|
+
extractSpecsFromText,
|
|
727
|
+
extractSpecs,
|
|
728
|
+
detectJdPageState,
|
|
97
729
|
};
|