Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
{ "name": "nothing0074", "email": "phong322010@proton.me" },
{ "name": "Justin Mott", "email": "justinmmott@gmail.com" },
{ "name": "mobedoor" },
{ "name": "nitramkh", "email": "nitramkh@gmail.com" }
{ "name": "nitramkh", "email": "nitramkh@gmail.com" },
{ "name": "Adoldev40", "email": "ladolfojafet@hotmail.com" }
],
"license": "GPL-3.0-only",
"bugs": {
Expand Down
4 changes: 4 additions & 0 deletions plugin/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@
"message": "Max chapters per EPUB",
"description": "Label for control to set max chapters to put in single EPUB"
},
"__MSG_label_Split_chapters_per_epub__": {
"message": "Split EPUB every N chapters (0 = disabled)",
"description": "Label for control to split output into multiple EPUB files after N chapters"
},
"__MSG_label_Max_pages_to_fetch_simultaneously__": {
"message": "Max web pages to fetch simultaneously",
"description": "Label for drop down for maximum number of web pages to request at same time"
Expand Down
1 change: 1 addition & 0 deletions plugin/js/UserPreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class UserPreferences { // eslint-disable-line no-unused-vars
this.removeTranslated = this.addPreference("removeTranslated", "removeTranslatedCheckbox", false);
this.skipChaptersThatFailFetch = this.addPreference("skipChaptersThatFailFetch", "skipChaptersThatFailFetchCheckbox", false);
this.maxChaptersPerEpub = this.addPreference("maxChaptersPerEpub", "maxChaptersPerEpubTag", "10,000");
this.splitChaptersPerEpub = this.addPreference("splitChaptersPerEpub", "splitChaptersPerEpubTag", "0");
this.manualDelayPerChapter = this.addPreference("manualDelayPerChapter", "manualDelayPerChapterTag", "0");
this.overrideMinimumDelay = this.addPreference("overrideMinimumDelay", "overrideMinimumDelayCheckbox", false);
this.skipImages = this.addPreference("skipImages", "skipImagesCheckbox", false);
Expand Down
64 changes: 61 additions & 3 deletions plugin/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,56 @@ var main = (function() {
}
}

function makeNovelBaseName() {
let baseName = getValueFromUiField("fileNameInput") ?? "web";
let base = baseName
.replace(/[<>:"/\\|?*~]/g, "")
.split("")
.filter(character => character.charCodeAt(0) >= 32)
.join("")
.replace(/\s+/g, " ")
.trim();
return util.isNullOrEmpty(base) ? "web" : base;
}

function getSplitChapterCount() {
let splitSize = parseInt(userPreferences.splitChaptersPerEpub.value, 10);
return isNaN(splitSize) ? 0 : Math.max(0, splitSize);
}

async function saveSplitEpubs(metaInfo, splitSize, overwriteExisting, backgroundDownload, novelBaseName) {
let pages = [...parser.getPagesToFetch().values()];
let selectedPages = pages.filter(page => page.isIncludeable);
if (selectedPages.length === 0) {
throw new Error("No chapters selected for split output.");
}

let originalIsIncludeable = new Map(pages.map(page => [page.sourceUrl, page.isIncludeable]));
try {
let totalSelected = selectedPages.length;
for (let index = 0; index < totalSelected; index += splitSize) {
let chunk = selectedPages.slice(index, index + splitSize);
let chunkUrls = new Set(chunk.map(page => page.sourceUrl));
pages.forEach(page => page.isIncludeable = chunkUrls.has(page.sourceUrl));

let content = await packEpub(metaInfo);
let startChapter = index + 1;
let endChapter = Math.min(index + splitSize, totalSelected);
let fileName = makeSplitFileName(novelBaseName, startChapter, endChapter, totalSelected);
await Download.save(content, fileName, overwriteExisting, backgroundDownload);
}
} finally {
pages.forEach(page => page.isIncludeable = originalIsIncludeable.get(page.sourceUrl));
}
}

function makeSplitFileName(novelBaseName, startChapter, endChapter, totalSelected) {
let padSize = Math.max(2, String(totalSelected).length);
let paddedStart = String(startChapter).padStart(padSize, "0");
let paddedEnd = String(endChapter).padStart(padSize, "0");
return EpubPacker.addExtensionIfMissing(`${novelBaseName} ${paddedStart} - ${paddedEnd}`);
}

async function fetchContentAndPackEpub() {
let libclick = this;
if (document.getElementById("noAdditionalMetadataCheckbox").checked == true) {
Expand All @@ -153,19 +203,27 @@ var main = (function() {
replaceLibAddToLibrary();
parser.onStartCollecting();
await parser.fetchContent();
let content = await packEpub(metaInfo);
// Enable button here. If user cancels save dialog
// the promise never returns
window.workInProgress = false;
main.getPackEpubButton().disabled = false;
replaceLibAddToLibrary();
let overwriteExisting = userPreferences.overwriteExistingEpub.value;
let backgroundDownload = userPreferences.noDownloadPopup.value;
let fileName = Download.CustomFilename();
let novelBaseName = makeNovelBaseName();
if ("yes" == libclick.dataset.libclick || util.sleepController.signal.aborted) {
let content = await packEpub(metaInfo);
let fileName = Download.CustomFilename();
await library.LibAddToLibrary(content, fileName, document.getElementById("startingUrlInput").value, overwriteExisting, backgroundDownload);
} else {
await Download.save(content, fileName, overwriteExisting, backgroundDownload);
let splitSize = getSplitChapterCount();
if (splitSize > 0) {
await saveSplitEpubs(metaInfo, splitSize, overwriteExisting, backgroundDownload, novelBaseName);
} else {
let content = await packEpub(metaInfo);
let fileName = Download.CustomFilename();
await Download.save(content, fileName, overwriteExisting, backgroundDownload);
}
}
try {
parser.updateReadingList();
Expand Down
6 changes: 6 additions & 0 deletions plugin/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ <h3>Instructions</h3>
</td>
<td>__MSG_label_Max_chapters_per_epub__</td>
</tr>
<tr id="splitChaptersPerEpub">
<td>
<input id="splitChaptersPerEpubTag" type="number" name="splitChaptersPerEpubTag" min="0" step="1" style="width: 61px;" value="0">
</td>
<td>__MSG_label_Split_chapters_per_epub__</td>
</tr>
<tr id="manualDelayPerChapter">
<td>
<input id="manualDelayPerChapterTag" type="number" name="manualDelayPerChapterTag" style="width: 61px;">
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ Don't forget to give the project a star! Thanks again!
<li>Justin Mott</li>
<li>mobedoor</li>
<li>nitramkh</li>
<li>Adoldev40</li>
</ul>
</details>

Expand Down