Skip to content

Commit 8611267

Browse files
committed
feat(script): 支持设置视频下载偏好
1 parent ebd69d5 commit 8611267

2 files changed

Lines changed: 56 additions & 11 deletions

File tree

static/Release_Notes.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212

1313
**用户脚本更新内容:**
1414

15-
**版本号:2.4.2**
15+
**版本号:2.4.3**
1616

1717
1. 修复部分视频下载失败的问题
1818
2. 优化视频下载链接提取逻辑
1919
3. 优化显示位置,避免遮挡内容
2020
4. 支持自定义文件名称格式
21-
5. 脚本图标支持移动位置
22-
6. 新增对 RedNote 的支持
23-
7. 调整图标显示位置
21+
5. 支持设置视频下载偏好
22+
6. 脚本图标支持移动位置
23+
7. 新增对 RedNote 的支持
24+
8. 调整图标显示位置

static/XHS-Downloader.js

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// @name XHS-Downloader
33
// @namespace xhs_downloader
44
// @homepage https://github.com/JoeanAmier/XHS-Downloader
5-
// @version 2.4.2
5+
// @version 2.4.3
66
// @tag 小红书
77
// @tag RedNote
88
// @tag XiaoHongShu
@@ -130,6 +130,13 @@ KS-Downloader(快手、KuaiShou):https://github.com/JoeanAmier/KS-Download
130130
scriptServerSwitchDesc: '启用后,可以把作品下载任务推送至服务器',
131131
imageDownloadFormatLabel: '图片下载格式',
132132
imageDownloadFormatDesc: '图文作品文件下载格式',
133+
videoPreferenceLabel: '视频下载偏好',
134+
videoPreferenceDesc: '视频作品文件下载策略',
135+
videoPreferenceOptions: {
136+
resolution: '分辨率优先',
137+
bitrate: '码率优先',
138+
size: '文件大小优先',
139+
},
133140
fileNameFormatLabel: '作品文件名称格式',
134141
fileNameFormatDesc: '点击可选字段添加,拖拽已选字段调整顺序',
135142
fileNameSelectedLabel: '已选字段',
@@ -286,6 +293,13 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
286293
scriptServerSwitchDesc: 'When enabled, download tasks can be pushed to the server',
287294
imageDownloadFormatLabel: 'Image Download Format',
288295
imageDownloadFormatDesc: 'Preferred file format for downloading images',
296+
videoPreferenceLabel: 'Video download preferences',
297+
videoPreferenceDesc: 'Video Notes File Download Strategy',
298+
videoPreferenceOptions: {
299+
resolution: 'Resolution priority',
300+
bitrate: 'Bitrate priority',
301+
size: 'File size priority',
302+
},
289303
fileNameFormatLabel: 'Note File Name Format',
290304
fileNameFormatDesc: 'Click available fields to add them. Drag selected fields to reorder.',
291305
fileNameSelectedLabel: 'Selected Fields',
@@ -420,6 +434,7 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
420434
linkCheckboxSwitch: GM_getValue("linkCheckboxSwitch", true),
421435
imageCheckboxSwitch: GM_getValue("imageCheckboxSwitch", true),
422436
imageDownloadFormat: GM_getValue("imageDownloadFormat", "jpeg"),
437+
videoPreference: GM_getValue("videoPreference", "resolution"),
423438
scriptServerURL: GM_getValue("scriptServerURL", defaultsWebSocketURL),
424439
scriptServerSwitch: GM_getValue("scriptServerSwitch", false),
425440
fileNameFormatKeys: storedFileNameFormatKeys,
@@ -560,6 +575,11 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
560575
GM_setValue("imageDownloadFormat", config.imageDownloadFormat);
561576
}
562577

578+
const updateVideoPreference = (value) => {
579+
config.videoPreference = value;
580+
GM_setValue("videoPreference", value);
581+
};
582+
563583
// 更新文件名格式配置。
564584
const updateFileNameFormat = (value) => {
565585
config.fileNameFormatKeys = parseFileNameFormat(value);
@@ -592,13 +612,21 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
592612
}
593613
}
594614

615+
const preferenceKey = {
616+
resolution: "height",
617+
bitrate: "videoBitrate",
618+
size: "size",
619+
};
620+
595621
const generateVideoUrl = note => {
596622
try {
597623
const key = note.video?.consumer?.originVideoKey;
598624
if (key) return [`https://sns-video-bd.xhscdn.com/${key}`];
599625
const allStreams = Object.values(note.video.media.stream).flat();
600-
sortArray(allStreams, "height");
601-
return [allStreams[0].backupUrls[0] || allStreams[0].masterUrl];
626+
if (allStreams.length === 0) return [];
627+
sortArray(allStreams, preferenceKey[config.videoPreference]);
628+
const stream = allStreams[0];
629+
return [stream.backupUrls[0] || stream.masterUrl];
602630
} catch (error) {
603631
console.error("Error deal video URL:", error);
604632
return [];
@@ -1442,7 +1470,8 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
14421470
box-shadow: 0 0 4px rgba(33, 150, 243, 0.3);
14431471
}
14441472
.select-input {
1445-
width: 100px;
1473+
min-width: 130px;
1474+
width: auto;
14461475
padding: 8px 12px;
14471476
border: 1px solid #ddd;
14481477
border-radius: 4px;
@@ -1919,8 +1948,11 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
19191948
item.style.opacity = disabled ? 0.6 : 1;
19201949

19211950
// 生成选项HTML
1922-
const optionsHtml = options.map(
1923-
option => `<option value="${option}" ${option === value ? 'selected' : ''}>${option}</option>`).join('');
1951+
const optionsHtml = options.map(option => {
1952+
const optionValue = typeof option === 'object' ? option.value : option;
1953+
const optionLabel = typeof option === 'object' ? option.label : option;
1954+
return `<option value="${optionValue}" ${optionValue === value ? 'selected' : ''}>${optionLabel}</option>`;
1955+
}).join('');
19241956

19251957
item.innerHTML = `
19261958
<label>
@@ -2033,6 +2065,17 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
20332065
.toUpperCase(),
20342066
});
20352067

2068+
const videoPreference = createSelectItem({
2069+
label: t.videoPreferenceLabel,
2070+
description: t.videoPreferenceDesc,
2071+
options: Object.entries(t.videoPreferenceOptions)
2072+
.map(([value, label]) => ({
2073+
value,
2074+
label,
2075+
})),
2076+
value: config.videoPreference,
2077+
});
2078+
20362079
const nameFormat = createFileNameFormatEditor({
20372080
label: t.fileNameFormatLabel,
20382081
description: t.fileNameFormatDesc,
@@ -2068,7 +2111,7 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
20682111
// 组合内容
20692112
body.appendChild(createSettingsGroup(
20702113
t.downloadSettingsGroup,
2071-
[filePack, imageCheckboxSwitch, imageDownloadFormat, nameFormat],
2114+
[filePack, imageCheckboxSwitch, imageDownloadFormat, videoPreference, nameFormat],
20722115
true
20732116
));
20742117
body.appendChild(createSettingsGroup(t.extractSettingsGroup, [autoScroll, scrollCount, linkCheckboxSwitch]));
@@ -2105,6 +2148,7 @@ Discord Community: https://discord.com/invite/ZYtmgKud9Y
21052148
updateScriptServerURL(scriptServerURL.querySelector('.text-input').value.trim() || defaultsWebSocketURL);
21062149
updateScriptServerSwitch(scriptServerSwitch.querySelector('input').checked);
21072150
updateImageDownloadFormat(imageDownloadFormat.querySelector('select').value.trim() || "jpeg");
2151+
updateVideoPreference(videoPreference.querySelector('select').value);
21082152
updateFileNameFormat(nameFormat.getFileNameFormatKeys());
21092153
closeSettingsModal();
21102154
});

0 commit comments

Comments
 (0)