From 7dd3afddff543f84acdd99300bff786bf385817f Mon Sep 17 00:00:00 2001 From: mrbunker Date: Sat, 6 Jan 2024 03:10:50 +0800 Subject: [PATCH] feat: post method --- src/utils/index.ts | 22 +++++++++++++++++++++- src/utils/siteList.ts | 35 ++++++++++++++++++++++++++++------- src/utils/xhr.ts | 14 +++++++++++--- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 3a448bd..89fdd27 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -41,7 +41,7 @@ interface TResponse { readonly finalUrl: string; } -export const gmFetch = ({ url }: { url: string }): Promise => { +export const gmGet = ({ url }: { url: string }): Promise => { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: "GET", @@ -51,3 +51,23 @@ export const gmFetch = ({ url }: { url: string }): Promise => { }); }); }; + +export const gmPost = ({ + url, + data, +}: { + url: string; + data?: Record; +}): Promise => { + return new Promise((resolve, reject) => { + GM_xmlhttpRequest({ + method: "POST", + data: new URLSearchParams(data).toString(), + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + url, + onload: (response) => resolve(response), + onerror: (error) => reject(error), + }); + }); +}; + diff --git a/src/utils/siteList.ts b/src/utils/siteList.ts index 9849f7c..b10aeb0 100644 --- a/src/utils/siteList.ts +++ b/src/utils/siteList.ts @@ -13,11 +13,6 @@ export interface DomQuery_parser { linkQuery: string; /** 在 title 里检测是否包含「字幕」等文本 */ titleQuery: string; - - /** 检测有无字幕的方法 - * 点名:Jable - */ - // checkMethod?: (pageDoc: Document) => { hasSubtitle?: boolean; hasLeakage?: boolean }; } export interface DomQuery_get { @@ -57,8 +52,13 @@ export interface SiteItem_parser extends SiteItemBase { fetchType: "parser"; domQuery: DomQuery_parser; } +export interface SiteItem_post extends SiteItemBase { + fetchType: "post"; + postParams: Record; + domQuery: DomQuery_parser; +} -export type SiteItem = SiteItem_get | SiteItem_parser; +export type SiteItem = SiteItem_get | SiteItem_parser | SiteItem_post; /** 在线网站列表 */ export const siteList: SiteItem[] = [ @@ -254,6 +254,27 @@ export const siteList: SiteItem[] = [ fetchType: "parser", domQuery: { linkQuery: ".well>a[href]", titleQuery: ".well>a[href]>span.video-title" }, }, + { + name: "evojav", + hostname: "evojav.pro", + url: "https://evojav.pro/video/{{code}}/", + fetchType: "get", + domQuery: {}, + }, + { + name: "7mm002", + hostname: "7mm002.com", + url: "https://7mm002.com/zh/searchform_search/all/index.html", + fetchType: "post", + postParams: { + search_type: "searchall", + op: "search", + }, + domQuery: { + linkQuery: ".content .video-title>a[href]", + titleQuery: ".content .video-title>a[href]", + }, + }, { name: "18av", hostname: "18av.mm-cg.com", @@ -292,7 +313,7 @@ export const siteList: SiteItem[] = [ titleQuery: ".videothumblist .video[id]:first-child>a>div.id", }, }, -] as const; +]; /** bus 里有些以 '300MIUM' 开头,要处理掉这个 300 */ export const SP_PREFIX = "300" as const; diff --git a/src/utils/xhr.ts b/src/utils/xhr.ts index a9ec39f..0a0e950 100644 --- a/src/utils/xhr.ts +++ b/src/utils/xhr.ts @@ -1,4 +1,4 @@ -import { gmFetch, isCaseInsensitiveEqual, isErrorCode, regEnum } from "./"; +import { gmGet, gmPost, isCaseInsensitiveEqual, isErrorCode, regEnum } from "./"; import type { DomQuery_get, DomQuery_parser, SiteItem } from "./siteList"; export type FetchResult = { @@ -68,7 +68,16 @@ export const handleFetch = async ( CODE: string, ): Promise => { try { - const response = await gmFetch({ url: targetLink }); + if (siteItem.fetchType === "post") { + const response = await gmPost({ + url: targetLink, + data: { search_keyword: CODE, ...siteItem.postParams }, + }); + return { + ...serachPageParser(response.responseText, siteItem.domQuery, siteItem.hostname, CODE), + }; + } + const response = await gmGet({ url: targetLink }); if (isErrorCode(response.status)) { // 请求 404,大概是对应网站没有资源 throw Error(String(response.status)); @@ -81,7 +90,6 @@ export const handleFetch = async ( targetLink, }; } else { - // 需要解析 searchPage siteItem.fetcher === "parser" return { ...serachPageParser(response.responseText, siteItem.domQuery, siteItem.hostname, CODE), };