Skip to content

Commit

Permalink
madara update (#1405)
Browse files Browse the repository at this point in the history
* madara customJS

* update for boxnovel
  • Loading branch information
K1ngfish3r authored Jan 5, 2025
1 parent f78d887 commit 2c7420e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 41 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions scripts/multisrc/madara/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ To add a new source you need to add it to sources.json:
exists in the languages (check folder names in "plugins/"))
- useNewChapterEndpoint: if the source uses the new chapter endpoint
- versionIncrements: needs to be updated everytime the site url is updated
- customJS: custom javascript that will be excuted when getting the text (if
the site has a custom copyright that need to be removed)

### icon

Expand Down
5 changes: 4 additions & 1 deletion scripts/multisrc/madara/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const generator = function generator(source) {
});

const pluginScript = `
${madaraTemplate}
${madaraTemplate.replace(
'// CustomJS HERE',
source.options?.customJs || '',
)}
const plugin = new MadaraPlugin(${JSON.stringify(source)});
export default plugin;
`.trim();
Expand Down
21 changes: 17 additions & 4 deletions scripts/multisrc/madara/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"sourceSite": "https://boxnovel.com/",
"sourceName": "BoxNovel",
"options": {
"useNewChapterEndpoint": true
"useNewChapterEndpoint": true,
"customJs": "chapterText.find(.cha-tit > div).remove()",
"versionIncrements": 1
}
},
{
Expand Down Expand Up @@ -141,7 +143,8 @@
"options": {
"useNewChapterEndpoint": true,
"lang": "Arabic",
"versionIncrements": 4
"versionIncrements": 5,
"customJs": "chapterText.find('span[style*=\"opacity: 0; position: fixed;\"]').remove();"
}
},
{
Expand Down Expand Up @@ -301,11 +304,12 @@
},
{
"id": "azora",
"sourceSite": "https://azorago.com/",
"sourceSite": "https://azoramoon.com/",
"sourceName": "Azora",
"options": {
"useNewChapterEndpoint": true,
"lang": "Arabic"
"lang": "Arabic",
"versionIncrements": 1
}
},
{
Expand Down Expand Up @@ -498,5 +502,14 @@
"lang": "English",
"useNewChapterEndpoint": true
}
},
{
"id": "fortuneeternal",
"sourceSite": "https://fortuneeternal.com",
"sourceName": "Fortune Eternal",
"options": {
"lang": "Korean",
"useNewChapterEndpoint": true
}
}
]
66 changes: 30 additions & 36 deletions scripts/multisrc/madara/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type MadaraOptions = {
lang?: string;
orderBy?: string;
versionIncrements?: number;
customJs?: string;
};

export type MadaraMetadata = {
Expand Down Expand Up @@ -45,40 +46,27 @@ class MadaraPlugin implements Plugin.PluginBase {
}

translateDragontea(text: Cheerio<AnyNode>): Cheerio<AnyNode> {
if (this.id === 'dragontea') {
const $ = parseHTML(text.html() || '');
let sanitizedText = $.html() || '';
sanitizedText = sanitizedText.replace('\n', '');
sanitizedText = sanitizedText.replace(/<br\s*\/?>/g, '\n');
text.html(sanitizedText);
text.find(':not(:has(*))').each((i, el) => {
// Select only the deepest elements to avoid reversing the text twice
const $el = $(el);
const alphabet =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const reversedAlphabet =
'zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA'.split('');
const text = $el.text().normalize('NFD'); // Normalize the string to separate the accents
const reversedText = text.split('');
const reversedLetters = [...reversedText]
.map(letter => {
const baseLetter = letter.normalize('NFC');
const index = alphabet.indexOf(baseLetter);
return index !== -1
? reversedAlphabet[index] + letter.slice(baseLetter.length)
: letter;
})
.join('');
$el.html(
$el
.html()
?.replace($el.text(), reversedLetters)
.replace('\n', '<br>') || '',
);
});
}
if (this.id !== 'dragontea') return text;

const $ = parseHTML(text.html()?.replace('\n', '').replace(/<br\s*\/?>/g, '\n') || '');
const reverseAlpha = 'zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA';
const forwardAlpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

text.html($.html());
text.find('*').addBack().contents().filter((_, el) => el.nodeType === 3).each((_, el) => {
const $el = $(el);
const translated = $el.text().normalize('NFD').split('')
.map(char => {
const base = char.normalize('NFC');
const idx = forwardAlpha.indexOf(base);
return idx >= 0 ? reverseAlpha[idx] + char.slice(base.length) : char;
})
.join('');
$el.replaceWith(translated.replace('\n', '<br>'));
});

return text;
}
}

getHostname(url: string): string {
url = url.split('/')[2];
Expand Down Expand Up @@ -258,6 +246,7 @@ class MadaraPlugin implements Plugin.PluginBase {
if (this.options?.useNewChapterEndpoint) {
html = await fetchApi(this.site + novelPath + 'ajax/chapters/', {
method: 'POST',
referrer: this.site + novelPath
}).then(res => res.text());
} else {
const novelId =
Expand Down Expand Up @@ -318,10 +307,15 @@ class MadaraPlugin implements Plugin.PluginBase {
loadedCheerio('.entry-content') ||
loadedCheerio('.c-blog-post > div > div:nth-child(2)');

if (this.id === 'riwyat') {
chapterText.find('span[style*="opacity: 0; position: fixed;"]').remove();
if (this.options?.customJs) {
try {
// CustomJS HERE
} catch (error) {
console.error('Error executing customJs:', error);
throw error;
}
}
chapterText.find('div.text-right').attr('style', 'text-align: right;');

return this.translateDragontea(chapterText).html() || '';
}

Expand Down

0 comments on commit 2c7420e

Please sign in to comment.