Skip to content

Commit

Permalink
添加 自定义下载设置 功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm0206 committed Dec 28, 2024
1 parent c67ef7f commit b2db01a
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 6 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ EPUB 文件请使用相应阅读器阅读。

设置菜单中按击开启。

### 自定义下载设置

在设置面板中,选中“启用自定义下载设置”以使自定义下载参数生效。

以下是自定义下载参数的介绍:

- 并行下载线程数:可以理解为同时下载的章节数量。数值需设置为 > 0 的整数。非法数值可能会导致下载出错。
- 下载间隔:仅当“并行下载线程数” = 1时 生效。为每下载一章节后增加的下载间隔时间,单位为毫秒(1 秒= 1000 毫秒)。数值需设置为 > 0 的整数。
- 最大下载间隔:仅当“并行下载线程数” = 1 时生效。为下载两章节之间最大的间隔时间,单位为毫秒(1 秒= 1000 毫秒)。数值需设置为 > 0 的整数。

该设置将应用于所有网站,一般不需要修改此设置,我们已经为每个网站设置好了比较合适的数值。但当网站添加反爬检测时,你可以使用本设置进行调整,我们也欢迎将新的数值提交到issue中。

### 自定义筛选函数

如欲只下载部分章节,请在点击运行按钮前,按下 F12 打开开发者工具,在 `window` 下创建 `chapterFilter` 函数,具体格式如下:
Expand Down
16 changes: 14 additions & 2 deletions src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
import { GmWindow } from "./global";
import { _GM_info } from "./lib/GM";
import { storageAvailable } from "./lib/localStorageExpired";
import { enableDebug, TxtDownload, EpubDownload } from "./setting";
import {
enableDebug,
TxtDownload,
EpubDownload,
customDownload,
concurrencyLimit,
sleepTime,
maxSleepTime
} from "./setting";
import { fetchWithRetry, fetchWithTimeout } from "./lib/http";

Check warning on line 15 in src/detect.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

'fetchWithRetry' is defined but never used

function checkObjct(name: string) {
Expand Down Expand Up @@ -53,7 +61,7 @@ async function TM_4_14_bug_Detect() {
`检测到您当前使用的脚本管理器为 Tampermonkey 4.14。
Tampermonkey 4.14 因存在 Bug 将导致小说下载器脚本无法正常运行,详情可参见:https://github.com/Tampermonkey/tampermonkey/issues/1418 。
如您想继续使用小说下载器脚本,请您更换 Tampermonkey 版本,或使用 Violentmonkey 脚本管理器。
如果您不欲降级或更换脚本管理器,同时不想再看到本提示,您可以暂时禁用小说下载器脚本。`
如果您不欲更改版本或更换脚本管理器,同时不想再看到本提示,您可以暂时禁用小说下载器脚本。`
);
throw new Error("Tampermonkey 4.14 Bug Detect");
}
Expand Down Expand Up @@ -86,6 +94,10 @@ export const environments = async () => {
enableDebug: enableDebug.value,
TxtDownload: TxtDownload.value,
EpubDownload: EpubDownload.value,
customDownload: customDownload.value,
concurrencyLimit: concurrencyLimit.value,
sleepTime: sleepTime.value,
maxSleepTime: maxSleepTime.value,
ScriptHandler: _GM_info.scriptHandler,
"ScriptHandler version": _GM_info.version,
"Novel-downloader version": _GM_info.script.version,
Expand Down
15 changes: 14 additions & 1 deletion src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import { Book, saveType } from "./main/Book";
import { SaveBook } from "./save/save";
import { SaveOptions, saveOptionsValidate } from "./save/options";
import {
customDownload,
concurrencyLimit,
sleepTime,
maxSleepTime,
enableCustomChapterFilter,
enableCustomFinishCallback,
enableCustomSaveOptions,
enableSaveToArchiveOrg,
getCustomEnableSaveToArchiveOrg,
} from "./setting";

import { failedPlus, printStat, successPlus } from "./stat";
import { ProgressVM, vm as progress } from "./ui/progress";
import { setStreamSaverSetting } from "./lib/zip";

Check warning on line 25 in src/rules.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

'setStreamSaverSetting' is defined but never used
Expand Down Expand Up @@ -98,6 +103,7 @@ export abstract class BaseRuleClass {
await self.preHook();
await initBook();
const saveBookObj = initSave(self.book as Book);
initDownload();
await saveHook();
await self.initChapters(self.book as Book, saveBookObj).catch((error) => {
if (error instanceof ExpectError) {
Expand All @@ -112,7 +118,14 @@ export abstract class BaseRuleClass {
} catch (error) {
self.catchError(error as Error);
}

function initDownload() {
if (customDownload.value) {
log.info("[run]发现自定义下载设置,将进行覆盖");
self.concurrencyLimit = concurrencyLimit.value;
self.sleepTime = sleepTime.value;
self.maxSleepTime = maxSleepTime.value;
}
}
async function initBook(): Promise<void> {
if (
(window as GmWindow)._book &&
Expand Down
6 changes: 5 additions & 1 deletion src/save/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { logText } from "../log";
import { Book, saveType } from "../main/Book";
import { Chapter } from "../main/Chapter";
import { Status } from "../main/main";
import { enableDebug, TxtDownload,EpubDownload } from "../setting";
import {
enableDebug,
TxtDownload,
EpubDownload,
} from "../setting";
import { SaveOptions } from "./options";
import { EPUB } from "./epub";
import { TXT } from "./txt";
Expand Down
12 changes: 12 additions & 0 deletions src/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ export const TxtDownload = {
export const EpubDownload = {
value: true,
};
export const customDownload = {
value: false,
};
export const concurrencyLimit = {
value: 1,
};
export const sleepTime = {
value: 500,
};
export const maxSleepTime = {
value: 2000,
};
export const enableCustomFinishCallback = true;
export const enableCustomChapterFilter = true;
export const enableCustomSaveOptions = true;
Expand Down
23 changes: 23 additions & 0 deletions src/ui/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@
<label for="test-page">启用测试视图</label>
</div>
<hr class="hr-twill-colorful" />
<div>
<h3>自定义下载参数</h3>
<table style="border:0px">
<tr>
<th>
<input id="customDownload" v-model="setting.customDownload" type="checkbox" />
<label for="customDownload">启用自定义下载设置</label></th>
<th>
<input
id="downloadConcurrency"
v-model="setting.concurrencyLimit"
type="number"
/>
<label for="downloadConcurrency">并行下载线程数</label></th>
</tr><th>
<input id="downloadSleeptime" v-model="setting.sleepTime" type="number" />
<label for="downloadSleeptime">下载间隔</label>
</th><th>
<input id="downloadMaxsleeptime" v-model="setting.maxSleepTime" type="number" />
<label for="downloadMaxsleeptime">最大下载间隔</label>
</th>
</table>
<hr class="hr-twill-colorful" />
<div>
<h3>自定义保存参数</h3>
<ul>
Expand Down
46 changes: 44 additions & 2 deletions src/ui/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import { log } from "../log";
import { Status } from "../main/main";
import { Chapter } from "../main/Chapter";
import { SaveOptions as globalSaveOptions } from "../save/options";
import { enableDebug, TxtDownload, EpubDownload } from "../setting";
import {
enableDebug,
TxtDownload,
EpubDownload,
customDownload,
concurrencyLimit,
sleepTime,
maxSleepTime
} from "../setting";
import FilterTab, {
FilterSetting as filterSettingGlobal,
getFilterFunction,
Expand All @@ -30,6 +38,10 @@ export const vm = createApp({
enableDebug?: boolean;
TxtDownload?: boolean;
EpubDownload?: boolean;
customDownload?: boolean;
concurrencyLimit?: number;
sleepTime?: number;
maxSleepTime?: number;
enableTestPage?: boolean;
chooseSaveOption?: string;
filterSetting?: filterSettingGlobal;
Expand Down Expand Up @@ -122,6 +134,14 @@ export const vm = createApp({
TxtDownload.value = setting.TxtDownload ?? TxtDownload.value;
setting.EpubDownload = GM_getValue('EpubDownload', EpubDownload.value);
EpubDownload.value = setting.EpubDownload ?? EpubDownload.value;
setting.customDownload = GM_getValue('customDownload', false);
customDownload.value = setting.customDownload ?? customDownload.value;
setting.concurrencyLimit = GM_getValue('concurrencyLimit', concurrencyLimit.value);
concurrencyLimit.value = setting.concurrencyLimit ?? concurrencyLimit.value;
setting.sleepTime = GM_getValue('sleepTime', sleepTime.value);
sleepTime.value = setting.sleepTime ?? sleepTime.value;
setting.maxSleepTime = GM_getValue('maxSleepTime', maxSleepTime.value);
maxSleepTime.value = setting.maxSleepTime ?? maxSleepTime.value;
setting.enableTestPage = GM_getValue('enableTestPage', false);
setting.chooseSaveOption = GM_getValue('chooseSaveOption', 'null');
setting.filterSetting = GM_getValue('filterSetting', undefined);
Expand Down Expand Up @@ -155,6 +175,7 @@ export const vm = createApp({
setEnableDebug();
setTxtDownload();
setEpubDownload();
setCustomDownloadOption();
setCustomSaveOption();
setCustomFilter();
saveAllSettings();
Expand Down Expand Up @@ -183,7 +204,24 @@ export const vm = createApp({
log.info(`[Init]EpubDownload: ${EpubDownload.value}`);
}
}

function setCustomDownloadOption() {
if (typeof config.customDownload === "boolean") {
customDownload.value = config.customDownload;
log.info(`[Init]customDownload: ${customDownload.value}`);
}
if (typeof config.concurrencyLimit === "number") {
concurrencyLimit.value = config.concurrencyLimit;
log.info(`[Init]concurrencyLimit: ${concurrencyLimit.value}`);
}
if (typeof config.sleepTime === "number") {
sleepTime.value = config.sleepTime;
log.info(`[Init]sleepTime: ${sleepTime.value}`);
}
if (typeof config.maxSleepTime === "number") {
maxSleepTime.value = config.maxSleepTime;
log.info(`[Init]maxSleepTime: ${maxSleepTime.value}`);
}
}
function setCustomSaveOption() {
(unsafeWindow as UnsafeWindow).saveOptions = curSaveOption();
}
Expand Down Expand Up @@ -215,6 +253,10 @@ export const vm = createApp({
GM_setValue('enableDebug', config.enableDebug);
GM_setValue('TxtDownload', config.TxtDownload);
GM_setValue('EpubDownload', config.EpubDownload);
GM_setValue('customDownload', config.customDownload);
GM_setValue('concurrencyLimit', config.concurrencyLimit);
GM_setValue('sleepTime', config.sleepTime);
GM_setValue('maxSleepTime', config.maxSleepTime);
GM_setValue('enableTestPage', config.enableTestPage);
GM_setValue('chooseSaveOption', config.chooseSaveOption);
GM_setValue('filterSetting', config.filterSetting);
Expand Down

0 comments on commit b2db01a

Please sign in to comment.