fix plugins (#1524)
* fix: free web novel search interval * fix: allnovelfull oversearch
This commit is contained in:
@@ -9,13 +9,13 @@ export default function SearchNovels() {
|
|||||||
const plugin = useAppStore(state => state.plugin);
|
const plugin = useAppStore(state => state.plugin);
|
||||||
|
|
||||||
const [searchTerm, setSearchTerm] = useState('');
|
const [searchTerm, setSearchTerm] = useState('');
|
||||||
const [page] = useState(1);
|
const [currentPage, setCurrentPage] = useState(1);
|
||||||
const [novels, setNovels] = useState<Plugin.NovelItem[]>([]);
|
const [novels, setNovels] = useState<Plugin.NovelItem[] | undefined>([]);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const [fetchError, setFetchError] = useState('');
|
const [fetchError, setFetchError] = useState('');
|
||||||
|
|
||||||
const fetchNovels = () => {
|
const fetchNovels = (page: number) => {
|
||||||
if (plugin) {
|
if (plugin) {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
plugin
|
plugin
|
||||||
@@ -28,6 +28,10 @@ export default function SearchNovels() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setCurrentPage(1);
|
||||||
|
}, [plugin]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AccordionContainer title="Search Novels" loading={loading}>
|
<AccordionContainer title="Search Novels" loading={loading}>
|
||||||
{fetchError && <span style={{ color: 'red' }}>{fetchError}</span>}
|
{fetchError && <span style={{ color: 'red' }}>{fetchError}</span>}
|
||||||
@@ -40,12 +44,19 @@ export default function SearchNovels() {
|
|||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
disabled={plugin === undefined}
|
disabled={plugin === undefined}
|
||||||
onClick={fetchNovels}
|
onClick={() => fetchNovels(currentPage)}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
>
|
>
|
||||||
Fetch
|
Fetch
|
||||||
</Button>
|
</Button>
|
||||||
<Button disabled={plugin === undefined} variant="outlined">
|
<Button
|
||||||
|
disabled={plugin === undefined}
|
||||||
|
variant="outlined"
|
||||||
|
onClick={() => {
|
||||||
|
fetchNovels(currentPage + 1);
|
||||||
|
setCurrentPage(pre => pre + 1);
|
||||||
|
}}
|
||||||
|
>
|
||||||
Next Page
|
Next Page
|
||||||
</Button>
|
</Button>
|
||||||
</Stack>
|
</Stack>
|
||||||
@@ -56,7 +67,7 @@ export default function SearchNovels() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Stack sx={{ width: 'max-content' }} direction={'row'} spacing={2}>
|
<Stack sx={{ width: 'max-content' }} direction={'row'} spacing={2}>
|
||||||
{novels.map((novel, index) => (
|
{novels?.map((novel, index) => (
|
||||||
<NovelItemCard key={'searchedNovelItem_' + index} item={novel} />
|
<NovelItemCard key={'searchedNovelItem_' + index} item={novel} />
|
||||||
))}
|
))}
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class AllNovelFullPlugin implements Plugin.PluginBase {
|
|||||||
name = 'AllNovelFull';
|
name = 'AllNovelFull';
|
||||||
icon = 'src/en/allnovelfull/icon.png';
|
icon = 'src/en/allnovelfull/icon.png';
|
||||||
site = 'https://allnovelfull.net';
|
site = 'https://allnovelfull.net';
|
||||||
version = '1.0.0';
|
version = '1.0.1';
|
||||||
|
|
||||||
parseNovels(loadedCheerio: CheerioAPI) {
|
parseNovels(loadedCheerio: CheerioAPI) {
|
||||||
const novels: Plugin.NovelItem[] = [];
|
const novels: Plugin.NovelItem[] = [];
|
||||||
@@ -145,6 +145,15 @@ class AllNovelFullPlugin implements Plugin.PluginBase {
|
|||||||
const body = await result.text();
|
const body = await result.text();
|
||||||
|
|
||||||
const loadedCheerio = parseHTML(body);
|
const loadedCheerio = parseHTML(body);
|
||||||
|
const lastPage = loadedCheerio('.pagination-container ul li a')
|
||||||
|
.last()
|
||||||
|
.attr('data-page');
|
||||||
|
if (!lastPage) {
|
||||||
|
// no pagination
|
||||||
|
if (page > 1) return [];
|
||||||
|
} else {
|
||||||
|
if (parseInt(lastPage) && page > parseInt(lastPage)) return [];
|
||||||
|
}
|
||||||
return this.parseNovels(loadedCheerio);
|
return this.parseNovels(loadedCheerio);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,16 @@ class FreeWebNovel implements Plugin.PluginBase {
|
|||||||
id = 'FWN.com';
|
id = 'FWN.com';
|
||||||
name = 'Free Web Novel';
|
name = 'Free Web Novel';
|
||||||
site = 'https://freewebnovel.com/';
|
site = 'https://freewebnovel.com/';
|
||||||
version = '1.1.1';
|
version = '1.1.2';
|
||||||
icon = 'src/en/freewebnovel/icon.png';
|
icon = 'src/en/freewebnovel/icon.png';
|
||||||
|
|
||||||
|
lastSearch: number | null = null;
|
||||||
|
searchInterval = 3200;
|
||||||
|
|
||||||
|
async sleep(ms: number) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
async getCheerio(url: string): Promise<CheerioAPI> {
|
async getCheerio(url: string): Promise<CheerioAPI> {
|
||||||
const r = await fetchApi(url);
|
const r = await fetchApi(url);
|
||||||
if (!r.ok)
|
if (!r.ok)
|
||||||
@@ -124,6 +131,10 @@ class FreeWebNovel implements Plugin.PluginBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {
|
async searchNovels(searchTerm: string): Promise<Plugin.NovelItem[]> {
|
||||||
|
const now = Date.now();
|
||||||
|
if (this.lastSearch && now - this.lastSearch <= this.searchInterval) {
|
||||||
|
await this.sleep(this.searchInterval);
|
||||||
|
}
|
||||||
const r = await fetchApi(this.site + 'search', {
|
const r = await fetchApi(this.site + 'search', {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -131,6 +142,7 @@ class FreeWebNovel implements Plugin.PluginBase {
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: new URLSearchParams({ searchkey: searchTerm }).toString(),
|
body: new URLSearchParams({ searchkey: searchTerm }).toString(),
|
||||||
});
|
});
|
||||||
|
this.lastSearch = Date.now();
|
||||||
if (!r.ok)
|
if (!r.ok)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Could not reach site (' + r.status + ') try to open in webview.',
|
'Could not reach site (' + r.status + ') try to open in webview.',
|
||||||
|
|||||||
Reference in New Issue
Block a user