Skip to content

Commit

Permalink
update: added default language setting
Browse files Browse the repository at this point in the history
  • Loading branch information
OshekharO authored Oct 17, 2023
2 parents 6894e4d + 9ca5bff commit 967682b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Miru extensions repository | [Miru App Download](https://github.com/miru-project
| Kisskh | kisskh.co | v0.0.2 | OshekharO | all | bangumi | [Source Code](https://github.com/miru-project/repo/blob/main/repo/kisskh.co.js) |
| 拷贝漫画 | mangacopy.comic | v0.0.1 | Monster | zh-cn | manga | [Source Code](https://github.com/miru-project/repo/blob/main/repo/mangacopy.comic.js) |
| マンガクロス | mangacross.jp | v0.0.1 | OshekharO | jp | manga | [Source Code](https://github.com/miru-project/repo/blob/main/repo/mangacross.jp.js) |
| MangaDex | mangadex.org | v0.0.1 | bethro | all | manga | [Source Code](https://github.com/miru-project/repo/blob/main/repo/mangadex.org.js) |
| MangaDex | mangadex.org | v0.0.2 | bethro | all | manga | [Source Code](https://github.com/miru-project/repo/blob/main/repo/mangadex.org.js) |
| 漫画柜 | manhuagui.com | v0.0.1 | appdevelpo | zh-cn | manga | [Source Code](https://github.com/miru-project/repo/blob/main/repo/manhuagui.com.js) |
| Mikanani | me.mikanani | v0.0.3 | MiaoMint | zh-cn | bangumi | [Source Code](https://github.com/miru-project/repo/blob/main/repo/me.mikanani.js) |
| Enime | moe.enime | v0.0.5 | MiaoMint | all | bangumi | [Source Code](https://github.com/miru-project/repo/blob/main/repo/moe.enime.js) |
Expand Down
2 changes: 1 addition & 1 deletion index.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
"package": "mangadex.org",
"type": "manga",
"url": "mangadex.org.js",
"version": "v0.0.1",
"version": "v0.0.2",
"webSite": "https://api.mangadex.org"
},
{
Expand Down
107 changes: 60 additions & 47 deletions repo/mangadex.org.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==MiruExtension==
// @name MangaDex
// @version v0.0.1
// @version v0.0.2
// @author bethro
// @lang all
// @license MIT
Expand Down Expand Up @@ -30,6 +30,14 @@ export default class extends Extension {
defaultValue: "https://api.mangadex.org",
});

this.registerSetting({
title: "Language",
key: "lang",
type: "input",
description: "Language",
defaultValue: "en",
})

this.registerSetting({
title: "Reverse Order of Chapters",
key: "reverseChaptersOrder",
Expand Down Expand Up @@ -103,62 +111,67 @@ export default class extends Extension {
return mangaList;
}

async detail(mangaId) {
const mangaRes = await this.req(`/manga/${mangaId}?includes[]=cover_art`);
const manga = mangaRes.data;
async detail(mangaId) {
const mangaRes = await this.req(`/manga/${mangaId}?includes[]=cover_art`);
const manga = mangaRes.data;

const coverArtObject = manga.relationships.find(
(relationship) => relationship.type === "cover_art"
);
const coverFilename = coverArtObject.attributes.fileName;
const coverImageURL = `https://uploads.mangadex.org/covers/${mangaId}/${coverFilename}`;
const coverArtObject = manga.relationships.find(
(relationship) => relationship.type === "cover_art"
);
const coverFilename = coverArtObject.attributes.fileName;
const coverImageURL = `https://uploads.mangadex.org/covers/${mangaId}/${coverFilename}`;

const metadata = manga.attributes.tags
.filter((tag) => tag.group === "genre")
.reduce((metadata, tag) => {
metadata[tag.name] = tag.description;
return metadata;
}, {});
const metadata = manga.attributes.tags
.filter((tag) => tag.group === "genre")
.reduce((metadata, tag) => {
metadata[tag.name] = tag.description;
return metadata;
}, {});

const chapRes = await this.req(`/manga/${mangaId}/feed?&order[volume]=asc&order[chapter]=asc&limit=500`);
const chapters = chapRes.data;
const chapRes = await this.req(`/manga/${mangaId}/feed?&order[volume]=asc&order[chapter]=asc&limit=500`);
const chapters = chapRes.data;

if (await this.getSetting("reverseChaptersOrder") === "true") {
chapters.reverse();
}
if (await this.getSetting("reverseChaptersOrder") === "true") {
chapters.reverse();
}

const chapMap = new Map();
const chapMap = new Map();
const defaultLang = await this.getSetting("lang");

for (const item of chapters) {
const lang = item.attributes.translatedLanguage;
const chapter = {
name: `Chapter ${item.attributes.chapter}`,
url: item.id,
};
for (const item of chapters) {
const lang = item.attributes.translatedLanguage;
const chapter = {
name: `Chapter ${item.attributes.chapter}`,
url: item.id,
};

if (!chapMap.has(lang)) {
const langChapters = [];
langChapters.push(chapter);
chapMap.set(lang, langChapters);
} else {
chapMap.get(lang).push(chapter);
}
if (!chapMap.has(lang)) {
chapMap.set(lang, [chapter]);
} else {
chapMap.get(lang).push(chapter);
}

const episodes = Array.from(chapMap.entries()).map(([lang, list]) => ({
title: lang,
urls: list,
}));

return {
title: manga.attributes.title.en,
cover: coverImageURL,
desc: manga.attributes.description.en,
metadata,
episodes,
};
}

const sortedChapMap = new Map([...chapMap.entries()].sort((a, b) => {
if (a[0] === defaultLang) return -1;
if (b[0] === defaultLang) return 1;
return a[0].localeCompare(b[0]); // order alphabetically
}));

const episodes = Array.from(sortedChapMap.entries()).map(([lang, list]) => ({
title: lang,
urls: list,
}));

return {
title: manga.attributes.title.en,
cover: coverImageURL,
desc: manga.attributes.description.en,
metadata,
episodes,
};
}

async watch(chapterId) {
const response = await this.req(`/at-home/server/${chapterId}`);
let { baseUrl: host, chapter: { hash: chapterHash, data } } = response;
Expand Down

0 comments on commit 967682b

Please sign in to comment.