const state = { all_plugins: {}, plugin_arr: [], current_array: [], current_plugin: undefined, filters: [], } const language_selection = $('#language'); const plugin_search = $('#plugin'); const search_results = $('#search-results'); fetch('/all_plugins').then(res => res.json()).then(all => { state.all_plugins = all; language_selection.html(``); for(let lang in all){ language_selection.append(``); state.plugin_arr = state.plugin_arr.concat(all[lang]); } }); const display_search = (arr) => { search_results.hide(); search_results.html(''); arr.slice(0, 15).forEach(plugin => { search_results.append(`
`) }); search_results.show(); } plugin_search.focus(e => { if(!state.all_plugins) return; if(language_selection.val()){ state.current_array = state.all_plugins[language_selection.val()]; }else{ state.current_array = state.plugin_arr; } display_search(state.current_array); }); plugin_search.keyup(e => { display_search(state.current_array.filter(plg => plg.name.toLowerCase().includes(plugin_search.val().toLowerCase()))); }); $('#clear-search').click(e => { search_results.hide(); plugin_search.val(''); plugin_search.attr('data-require', ''); }); const test_plugin = (btn) => { const ele = $(btn); plugin_requirePath = ele.attr('data-require'); state.current_plugin = new Plugin(plugin_requirePath); plugin_search.attr('data-require', plugin_requirePath); plugin_search.val(ele.text().split('/')[1]); search_results.hide(); state.current_plugin.getfilters(); } class Plugin { constructor(requirePath){ this.requirePath = requirePath; } getfilters = () => { const makeSelect = (filter) => { const container = document.createElement('div'); const select = document.createElement('select'); select.classList.add('select'); if(filter.inputType === 2 || filter.inputType === "Checkbox"){ select.setAttribute("multiple", ""); }else{ select.innerHTML = ``; } select.setAttribute("key", filter.key); filter.values.forEach((v) => { const option = document.createElement('option'); option.setAttribute("value", v.value) option.innerText = v.label; select.append(option); }); container.append(select); const label = document.createElement('label'); label.classList.add("form-label"); label.classList.add("select-label"); label.innerText = filter.label; container.append(label); const hr = document.createElement('hr'); hr.classList.add("hr"); container.append(hr); return container; } fetch("/filters", { method: 'POST', headers: { "Content-Type": "application/json" }, body: JSON.stringify({"pluginRequirePath": this.requirePath}), }).then(res => res.json()) .then(filters => { const filtersModal = $("#filtersModal .modal-body"); filtersModal.html(""); filters.forEach(filter => { filtersModal.append(makeSelect(filter)) }); state.filters.length = 0; document.querySelectorAll('select.select').forEach(e => { const key = e.getAttribute("key"); const select = new mdb.Select(e); select.key = key; state.filters.push(select); }) }) } popularNovels = () => { const filters = {}; state.filters.forEach(_f => { filters[_f.key] = _f.value; }) $('#popularNovels .spinner-border').show(); fetch(`/popularNovels/`,{ method: 'POST', headers: { "Content-Type": "application/json" }, body: JSON.stringify( { "pluginRequirePath": this.requirePath, "filters": filters } ), }) .then(res => res.json()) .then(novels => { const novel_list = $('#popularNovels .novel-list'); novel_list.html(''); if(novels.error){ novel_list.html(novels.error); }else{ novels.forEach(novel => { novel_list.append(`
No cover found
name
${novel.name?.slice(0, 50)}
Copy
url
${novel.url?.slice(0, 50)}
Copy
cover
${novel.cover?.slice(0, 50)}
Copy
`); }) } $('#popularNovels .spinner-border').hide(); }); } searchNovels = () => { const searchTerm = $('#searchNovels input').val(); $('#searchNovels .spinner-border').show(); fetch(`/searchNovels/`,{ method: 'POST', headers: { "Content-Type": "application/json" }, body: JSON.stringify({"pluginRequirePath": this.requirePath, "searchTerm": searchTerm}), }) .then(res => res.json()) .then(novels => { const novel_list = $('#searchNovels .novel-list'); novel_list.html(''); if(novels.error){ novel_list.html(novels.error); }else{ novels.forEach(novel => { novel_list.append(`
No cover found
name
${novel.name?.slice(0, 50)}
Copy
url
${novel.url?.slice(0, 50)}
Copy
cover
${novel.cover?.slice(0, 50)}
Copy
`); }) } $('#searchNovels .spinner-border').hide(); }); } parseNovelAndChapters = () => { const novelUrl = $('#parseNovelAndChapters input').val(); $('#parseNovelAndChapters .spinner-border').show(); fetch(`/parseNovelAndChapters/`,{ method: 'POST', headers: { "Content-Type": "application/json" }, body: JSON.stringify({"pluginRequirePath": this.requirePath, "novelUrl": novelUrl}), }) .then(res => res.json()) .then(sourceNovel => { const novel_item = $('#parseNovelAndChapters .novel-list .novel-item'); novel_item.html(` No cover found
name
${sourceNovel.name?.slice(0, 50)}
Copy
url
${sourceNovel.url?.slice(0, 50)}
Copy
cover
${sourceNovel.cover?.slice(0, 50)}
Copy
summary
${sourceNovel.summary?.slice(0, 50)}
Copy
author
${sourceNovel.author}
Copy
artist
${sourceNovel.artist}
Copy
status
${sourceNovel.status}
Copy
genres
${sourceNovel.genres?.slice(0, 50)}
Copy
`); if(sourceNovel.error){ novel_item.html(sourceNovel.error); }else{ const chapter_list = $('#parseNovelAndChapters .chapter-list'); chapter_list.html(''); sourceNovel.chapters.forEach(chapter => { chapter_list.append(`
${chapter.name?.slice(0, 26)}
${chapter.releaseTime}
Copy url
`); }) } $('#parseNovelAndChapters .spinner-border').hide(); }); } parseChapter = () => { const chapterUrl = $('#parseChapter input').val(); $('#parseChapter .spinner-border').show(); fetch(`/parseChapter/`,{ method: 'POST', headers: { "Content-Type": "application/json" }, body: JSON.stringify({"pluginRequirePath": this.requirePath, "chapterUrl": chapterUrl}), }) .then(res => res.json()) .then(chapterText => { $('#parseChapter textarea').text(chapterText); $('#parseChapter .spinner-border').hide(); }) } fetchImage = () => { const url = $('#fetchImage input').val(); $('#fetchImage .spinner-border').show(); fetch(`/fetchImage/`,{ method: 'POST', headers: { "Content-Type": "application/json" }, body: JSON.stringify({"pluginRequirePath": this.requirePath, "url": url}), }) .then(res => res.json()) .then(base64 => { $('#fetchImage img').attr("src", `data:image/jpg;base64,${base64}`); $('#fetchImage .spinner-border').hide(); }) } } $(".popularNovels-btn").click(e => { if(state.current_plugin){ state.current_plugin.popularNovels(); } }); $(".searchNovels-btn").click(e => { if(state.current_plugin){ state.current_plugin.searchNovels(); } }); $(".parseNovelAndChapters-btn").click(e => { if(state.current_plugin){ state.current_plugin.parseNovelAndChapters(); } }); $(".parseChapter-btn").click(e => { if(state.current_plugin){ state.current_plugin.parseChapter(); } }); $(".fetchImage-btn").click(e => { if(state.current_plugin){ state.current_plugin.fetchImage(); } });