source/comix/comix.js
2025-12-09 22:18:29 +00:00

121 lines
No EOL
3.9 KiB
JavaScript

async function searchContent(keyword, page = 0) {
try {
const response = await fetch("https://comix.to/api/v2/manga?order[relevance]=desc&keyword=" + encodeURIComponent(keyword) + "&limit=100");
const data = await response.json();
const results = data.result.items.map(manga => ({
title: manga.title,
imageURL: manga.poster.large,
id: manga.hash_id + "-" + manga.slug
}));
return results;
} catch (err) {
return [];
}
}
async function getContentData(slug) {
try {
const response = await fetch("https://comix.to/title/" + slug);
const html = await response.text();
const descriptionMatch = html.match(/<meta name="description" content="([^"]*)"/);
const description = descriptionMatch ? descriptionMatch[1] : "";
function cleanDescription(obj) {
if (!obj.description) return '';
let text = obj.description;
const htmlEntities = {
'&#x27;': "'",
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
'&quot;': '"'
};
text = text.replace(/&#x27;|&amp;|&lt;|&gt;|&quot;/g, match => htmlEntities[match] || match);
text = text.replace(/\[([^\]]+)\]\([^\)]+\)/g, '$1');
text = text.replace(/[*_~`]/g, '');
text = text.replace(/___.*$/g, '');
text = text.replace(/[\n\r\t]+/g, ' ').replace(/\s+/g, ' ');
return text.trim();
}
return {
description: cleanDescription({ description }),
tags: []
};
} catch (err) {
return {
description: "Error",
tags: []
};
}
}
async function getChapters(url) {
const hash = url.split("-")[0]
const results = [];
try {
const firstResponse = await fetch("https://comix.to/api/v2/manga/" + hash + "/chapters?limit=100&page=1&order[number]=desc")
const firstData = await firstResponse.json();
const lastPage = firstData.result.pagination.last_page;
let allItems = [];
for (let page = 1; page <= lastPage; page++) {
const response = await fetch("https://comix.to/api/v2/manga/" + hash + "/chapters?limit=100&page=" + page + "&order[number]=desc")
const data = await response.json();
allItems = allItems.concat(data.result.items);
}
allItems.reverse();
const chaptersMap = new Map();
allItems.forEach(item => {
const chapterNum = String(item.number);
if (!chaptersMap.has(chapterNum)) {
chaptersMap.set(chapterNum, [
chapterNum,
[{
id: url + "/" + item.chapter_id + "-chapter-" + item.number,
title: item.name || `Chapter ${item.number}`,
chapter: item.number,
scanlation_group: item.scanlation_group?.name || "Comix"
}]
]);
}
});
return {
en: Array.from(chaptersMap.values())
};
} catch (err) {
return {
en: []
};
}
}
async function getChapterImages(url) {
const results = [];
try {
const response = await fetch("https://comix.to/title/" + url);
const html = await response.text();
const match = html.match(/\\"images\\":\[([^\]]+)\]/);
if (match) {
const imagesJson = '[' + match[1].replace(/\\"/g, '"') + ']';
const images = JSON.parse(imagesJson);
const imageUrls = images.map(img => img.url);
results.push(...imageUrls);
return results;
} else {
console.error("Invalid response from server");
return [];
}
} catch (error) {
console.error("Error fetching chapters: " + error);
return [];
}
}