|
| 1 | +function searchManga(query) { |
| 2 | + var mangas = []; |
| 3 | + const res = mango.get('https://www.manhuadb.com/search?q=' + encodeURIComponent(query)); |
| 4 | + if (res.status_code !== 200) |
| 5 | + mango.raise('Failed to search for manga. Status ' + res.status_code); |
| 6 | + var html = res.body; |
| 7 | + items = mango.css(html, '.comicbook-index'); |
| 8 | + for (i = 0; i < items.length; i++) { |
| 9 | + var href = mango.attribute(mango.css(items[i], 'a')[0], 'href'); |
| 10 | + var img_link = mango.attribute(mango.css(items[i], 'img')[0], 'src'); |
| 11 | + if (img_link.substring(0, 4) != 'http') { |
| 12 | + img_link = 'https://www.manhuadb.com' + img_link; |
| 13 | + } |
| 14 | + var authors = []; |
| 15 | + var authors_name = mango.css(items[i], '.one-line.comic-author>a') |
| 16 | + for (j = 0; j < authors_name.length; j++) { |
| 17 | + authors.push(mango.attribute(authors_name[j], 'title')) |
| 18 | + } |
| 19 | + mangas.push({ |
| 20 | + id: href.substring(8, href.length), |
| 21 | + title: mango.attribute(mango.css(items[i], 'a')[0], 'title'), |
| 22 | + authors: authors, |
| 23 | + cover_url: img_link |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + return JSON.stringify(mangas); |
| 28 | +} |
| 29 | + |
| 30 | +function listChapters(id) { |
| 31 | + var url = 'https://www.manhuadb.com/manhua/' + id; |
| 32 | + const res = mango.get(url); |
| 33 | + var chapters = [] |
| 34 | + if (res.status_code !== 200) |
| 35 | + mango.raise('Failed to list chapters. Status ' + res.status_code); |
| 36 | + var html = res.body; |
| 37 | + var comic_lists = mango.css(html, '.links-of-books'); |
| 38 | + for (i = 0; i < comic_lists.length; i++) { |
| 39 | + var comics = mango.css(comic_lists[i], 'a'); |
| 40 | + for (j = 0; j < comics.length; j++) { |
| 41 | + var id_part = mango.attribute(comics[j], 'href'); |
| 42 | + id_part2 = id_part.replace('/manhua/' + id + '/', ''); |
| 43 | + chapters.push({ |
| 44 | + id: id + '__' + id_part2.substring(0, id_part2.length - 5), |
| 45 | + title: mango.attribute(comics[j], 'title'), |
| 46 | + pages: 1000, |
| 47 | + manga_title: mango.text(mango.css(html, 'h1')[0]) |
| 48 | + }) |
| 49 | + } |
| 50 | + } |
| 51 | + return JSON.stringify(chapters); |
| 52 | +} |
| 53 | + |
| 54 | +function selectChapter(id) { |
| 55 | + var ids = id.split('__'); |
| 56 | + var url = 'https://www.manhuadb.com/manhua/' + ids[0] + '/' + ids[1] + '.html'; |
| 57 | + const res = mango.get(url); |
| 58 | + if (res.status_code !== 200) |
| 59 | + mango.raise('Failed to get chapter. Status ' + res.status_code); |
| 60 | + var html = res.body; |
| 61 | + var info = mango.css(html, '.breadcrumb-item'); |
| 62 | + var title = mango.text(mango.css(info[info.length - 1], 'a')[0]); |
| 63 | + var page_num = parseInt(mango.text(info[info.length - 1]).split(' ')[5]); |
| 64 | + var manga_title = mango.text(mango.css(info[info.length - 2], 'a')[0]); |
| 65 | + mango.storage('url', 'https://www.manhuadb.com/manhua/' + ids[0] + '/' + ids[1] + '_p1.html'); |
| 66 | + mango.storage('page', '0'); |
| 67 | + |
| 68 | + return JSON.stringify({ |
| 69 | + id: id, |
| 70 | + title: title, |
| 71 | + pages: page_num, |
| 72 | + manga_title: manga_title |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +function nextPage() { |
| 77 | + const page = parseInt(mango.storage('page')); |
| 78 | + const url = mango.storage('url'); |
| 79 | + const filename = page + '.jpg'; |
| 80 | + const res = mango.get(url); |
| 81 | + if (res.status_code !== 200) |
| 82 | + mango.raise('Failed to get chapter. Status ' + res.status_code); |
| 83 | + var html = res.body; |
| 84 | + var css = mango.css(html, '.show-pic'); |
| 85 | + if (css.length == 0) return JSON.stringify({}); |
| 86 | + var img_url = mango.attribute(css[0], 'src'); |
| 87 | + mango.storage('page', (page + 1).toString()); |
| 88 | + mango.storage('url', url.replace('_p' + (page + 1).toString(), '_p' + (page + 2).toString())); |
| 89 | + return JSON.stringify({ |
| 90 | + url: img_url, |
| 91 | + filename: filename, |
| 92 | + }); |
| 93 | +} |
0 commit comments