Skip to content

Commit 9d8c522

Browse files
committed
lightnovelwp: improvements, new source katreadingarchive.me
- generator now supports custom runtime JS and CSS, property names changed to clarify the difference - fixed some type errors - attempt to make "hide locked" option work
1 parent dd728f6 commit 9d8c522

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

plugins/multisrc/lightnovelwp/generator.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { dirname, join } from 'path';
55

66
const folder = dirname(fileURLToPath(import.meta.url));
77

8+
const LightNovelWPTemplate = readFileSync(join(folder, 'template.ts'), {
9+
encoding: 'utf-8',
10+
});
11+
812
export const generateAll = function () {
913
return list.map(source => {
1014
const exist = existsSync(join(folder, 'filters', source.id + '.json'));
@@ -15,21 +19,18 @@ export const generateAll = function () {
1519
source.filters = JSON.parse(filters).filters;
1620
}
1721
console.log(
18-
`[lightnovelwp] Generating: ${source.id}${' '.repeat(20 - source.id.length)} ${source.filters ? '🔎with filters🔍' : '🚫no filters🚫'}`,
22+
`[lightnovelwp] Generating: ${source.id} ${' '.repeat(20 - source.id.length)} ${source.filters ? '🔎with filters🔍' : '🚫no filters🚫'}`,
1923
);
2024
return generator(source);
2125
});
2226
};
2327

2428
const generator = function generator(source) {
25-
const LightNovelWPTemplate = readFileSync(join(folder, 'template.ts'), {
26-
encoding: 'utf-8',
27-
});
2829

2930
const pluginScript = `
3031
${LightNovelWPTemplate.replace(
3132
'// CustomJS HERE',
32-
source.options?.customJs || '',
33+
source.options?.customTransformJs || '',
3334
)}
3435
const plugin = new LightNovelWPPlugin(${JSON.stringify(source)});
3536
export default plugin;

plugins/multisrc/lightnovelwp/sources.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"options": {
1616
"lang": "Arabic",
1717
"reverseChapters": true,
18-
"customJs": "$('article > style').text().match(/\\.\\w+(?=\\s*[,{])/g)?.forEach(tag => $(`p${tag}`).remove());$('.epcontent .code-block').remove();",
18+
"customTransformJs": "$('article > style').text().match(/\\.\\w+(?=\\s*[,{])/g)?.forEach(tag => $(`p${tag}`).remove());$('.epcontent .code-block').remove();",
1919
"versionIncrements": 9
2020
}
2121
},
@@ -107,7 +107,7 @@
107107
"lang": "English",
108108
"reverseChapters": true,
109109
"versionIncrements": 2,
110-
"customJs": "$('.announ').remove(); return $('.epcontent').eq(-1).find('p').map(function (i, el) { return '<p>' + $(this).text() + '</p>'; }).toArray().join('\\n') || '';"
110+
"customTransformJs": "$('.announ').remove(); return $('.epcontent').eq(-1).find('p').map(function (i, el) { return '<p>' + $(this).text() + '</p>'; }).toArray().join('\\n') || '';"
111111
}
112112
},
113113
{
@@ -199,7 +199,7 @@
199199
"options": {
200200
"lang": "English",
201201
"reverseChapters": true,
202-
"customJs": "\n $('div.entry-content script').remove();\n\n const url = this.site + chapterPath.slice(0, -1);\n const offsets = [[0, 12368, 12462], [1, 6960, 7054], [2, 4176, 4270]];\n const idx = url.length * url.charCodeAt(url.length - 1) * 2 % 3;\n const [_, offsetLower, offsetCap] = offsets[idx] ?? offsets[0];\n\n const asciiA = 'A'.charCodeAt(0);\n const asciiz = 'z'.charCodeAt(0);\n\n $('div.entry-content > p').text((_, txt) =>\n txt.split('').map(char => {\n const code = char.charCodeAt(0);\n const offset = (code >= offsetLower + asciiA && code <= offsetLower + asciiz)\n ? offsetLower\n : offsetCap;\n const decoded = code - offset;\n return (decoded >= 32 && decoded <= 126) ? String.fromCharCode(decoded) : char;\n }).join('')\n );\n",
202+
"customTransformJs": "\n $('div.entry-content script').remove();\n\n const url = this.site + chapterPath.slice(0, -1);\n const offsets = [[0, 12368, 12462], [1, 6960, 7054], [2, 4176, 4270]];\n const idx = url.length * url.charCodeAt(url.length - 1) * 2 % 3;\n const [_, offsetLower, offsetCap] = offsets[idx] ?? offsets[0];\n\n const asciiA = 'A'.charCodeAt(0);\n const asciiz = 'z'.charCodeAt(0);\n\n $('div.entry-content > p').text((_, txt) =>\n txt.split('').map(char => {\n const code = char.charCodeAt(0);\n const offset = (code >= offsetLower + asciiA && code <= offsetLower + asciiz)\n ? offsetLower\n : offsetCap;\n const decoded = code - offset;\n return (decoded >= 32 && decoded <= 126) ? String.fromCharCode(decoded) : char;\n }).join('')\n );\n",
203203
"versionIncrements": 4
204204
}
205205
},
@@ -284,5 +284,19 @@
284284
"lang": "English",
285285
"reverseChapters": true
286286
}
287+
},
288+
{
289+
"id": "katreadingarchive",
290+
"sourceSite": "https://katreadingarchive.me",
291+
"sourceName": "Kat Reading Archive",
292+
"options": {
293+
"lang": "English",
294+
"versionIncrements": 1,
295+
"reverseChapters": true,
296+
"hasLocked": true,
297+
"customTransformJs": "$('div.epcontent > span:first-of-type').remove(); $('.ckb-wrap').remove();",
298+
"customRuntimeJs": "multisrc/lightnovelwp/katreadingarchive/customJs.js",
299+
"customCss": "multisrc/lightnovelwp/katreadingarchive/customCss.css"
300+
}
287301
}
288302
]

plugins/multisrc/lightnovelwp/template.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ type LightNovelWPOptions = {
1212
lang?: string;
1313
versionIncrements?: number;
1414
seriesPath?: string;
15-
customJs?: string;
15+
customTransformJs?: string;
1616
hasLocked?: boolean;
17+
customRuntimeJs?: string;
18+
customCss?: string;
1719
};
1820

1921
export type LightNovelWPMetadata = {
@@ -32,7 +34,10 @@ class LightNovelWPPlugin implements Plugin.PluginBase {
3234
version: string;
3335
options?: LightNovelWPOptions;
3436
filters?: Filters;
37+
customJS?: string;
38+
customCSS?: string;
3539

40+
// TODO: this probably needs to be done at use time, otherwise changes won't be reflected
3641
hideLocked = storage.get('hideLocked');
3742
pluginSettings?: Record<string, any>;
3843

@@ -42,9 +47,11 @@ class LightNovelWPPlugin implements Plugin.PluginBase {
4247
this.icon = `multisrc/lightnovelwp/${metadata.id.toLowerCase()}/icon.png`;
4348
this.site = metadata.sourceSite;
4449
const versionIncrements = metadata.options?.versionIncrements || 0;
45-
this.version = `1.1.${9 + versionIncrements}`;
50+
this.version = `1.1.${10 + versionIncrements}`;
4651
this.options = metadata.options ?? ({} as LightNovelWPOptions);
4752
this.filters = metadata.filters satisfies Filters;
53+
this.customJS = this.options.customRuntimeJs;
54+
this.customCSS = this.options.customCss;
4855

4956
if (this.options?.hasLocked) {
5057
this.pluginSettings = {
@@ -64,7 +71,7 @@ class LightNovelWPPlugin implements Plugin.PluginBase {
6471
return url_parts.join('.');
6572
}
6673

67-
async safeFecth(url: string, search: boolean): Promise<string> {
74+
async safeFetch(url: string, search: boolean): Promise<string> {
6875
const urlParts = url.split('://');
6976
const protocol = urlParts.shift();
7077
const sanitizedUri = urlParts[0].replace(/\/\//g, '/');
@@ -147,13 +154,13 @@ class LightNovelWPPlugin implements Plugin.PluginBase {
147154
url += `&${key}=${value}`;
148155
else if (filters[key].value) url += `&${key}=${filters[key].value}`;
149156
}
150-
const html = await this.safeFecth(url, false);
157+
const html = await this.safeFetch(url, false);
151158
return this.parseNovels(html);
152159
}
153160

154161
async parseNovel(novelPath: string): Promise<Plugin.SourceNovel> {
155162
const baseURL = this.site;
156-
const html = await this.safeFecth(baseURL + novelPath, false);
163+
const html = await this.safeFetch(baseURL + novelPath, false);
157164

158165
const novel: Plugin.SourceNovel = {
159166
path: novelPath,
@@ -180,7 +187,10 @@ class LightNovelWPPlugin implements Plugin.PluginBase {
180187
let hasLockItemOnChapterNum = false;
181188
const chapters: Plugin.ChapterItem[] = [];
182189
let tempChapter = {} as Plugin.ChapterItem;
183-
const hideLocked = this.hideLocked;
190+
191+
const hideLocked = !!(
192+
storage.get('hideLocked') ?? this.pluginSettings?.['hideLocked'].value
193+
);
184194

185195
const parser = new Parser({
186196
onopentag(name, attribs) {
@@ -421,29 +431,28 @@ class LightNovelWPPlugin implements Plugin.PluginBase {
421431
novel.chapters = chapters;
422432
}
423433

424-
novel.summary = novel.summary.trim();
434+
novel.summary = novel.summary?.trim();
425435

426436
return novel;
427437
}
428438

429439
async parseChapter(chapterPath: string): Promise<string> {
430-
let data = await this.safeFecth(this.site + chapterPath, false);
431-
if (this.options?.customJs) {
440+
let data = await this.safeFetch(this.site + chapterPath, false);
441+
const $ = load(data);
442+
if (this.options?.customTransformJs) {
432443
try {
433-
const $ = load(data);
434444
// CustomJS HERE
435-
data = $.html();
436445
} catch (error) {
437446
console.error('Error executing customJs:', error);
438447
throw error;
439448
}
440449
}
441-
return (
442-
data
443-
.match(/<div.*?class="epcontent ([^]*?)<div.*?class="?bottomnav/g)?.[0]
444-
.match(/<p[^>]*>([^]*?)<\/p>/g)
445-
?.join('\n') || ''
446-
);
450+
const content = $('div.epcontent');
451+
// content.find('h1:first-child').remove();
452+
453+
// content.find('> :not(p)').remove();
454+
455+
return content.html() ?? '';
447456
}
448457

449458
async searchNovels(
@@ -452,7 +461,7 @@ class LightNovelWPPlugin implements Plugin.PluginBase {
452461
): Promise<Plugin.NovelItem[]> {
453462
const url =
454463
this.site + 'page/' + page + '/?s=' + encodeURIComponent(searchTerm);
455-
const html = await this.safeFecth(url, true);
464+
const html = await this.safeFetch(url, true);
456465
return this.parseNovels(html);
457466
}
458467
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
blockquote, q {
2+
padding: 5px 10px;
3+
background-color: var(--theme-surfaceVariant);
4+
color: var(--theme-onSurfaceVariant);
5+
border-left: 4px solid var(--theme-outline);
6+
quotes: none;
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// WORKAROUND: empty files currently break the download function!

0 commit comments

Comments
 (0)