From 933ed0aaa9ecf484ed5cd1aa4876e8e97fb646c2 Mon Sep 17 00:00:00 2001 From: terwer Date: Mon, 26 Sep 2022 19:34:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:#85=20Google=E6=8F=92=E4=BB=B6=E6=89=A9?= =?UTF-8?q?=E5=B1=95-=E5=85=AC=E5=85=B1=E8=AF=B7=E6=B1=82=E5=8F=8A?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/background.js | 22 +++++++++++----- src/components/tab/main/MetaweblogMain.vue | 8 +++++- src/lib/platform/commonblog/commonblogApi.ts | 25 ++++++------------- .../metaweblog/CustomMetaweblogApi.ts | 4 +++ src/lib/platform/metaweblog/customXmlrpc.ts | 13 +++++++--- 5 files changed, 43 insertions(+), 29 deletions(-) diff --git a/public/background.js b/public/background.js index 4942e8ca..11576ff6 100644 --- a/public/background.js +++ b/public/background.js @@ -15,17 +15,27 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { switch (request.type) { case 'fetchChromeXmlrpc': (async () => { - const response = await fetch(request.apiUrl, request.fetchCORSParams); - const resText = await response.text() - // console.log("chrome.runtime.onMessage.addListener fetchChromeXmlrpc response:", resText) + let resText + try { + const response = await fetch(request.apiUrl, request.fetchCORSParams); + resText = await response.text() + // console.log("chrome.runtime.onMessage.addListener fetchChromeXmlrpc response:", resText) + } catch (e) { + console.error("chrome.runtime fetchChromeXmlrpc request error", e) + } sendResponse(resText); })(); break; case 'fetchChromeJson': (async () => { - const response = await fetch(request.apiUrl, request.fetchCORSOptions); - const resJson = await response.json() - console.log("chrome.runtime.onMessage.addListener fetchChromeJson response:", resJson) + let resJson + try { + const response = await fetch(request.apiUrl, request.fetchCORSOptions); + resJson = await response.json() + // console.log("chrome.runtime.onMessage.addListener fetchChromeJson response:", resJson) + } catch (e) { + console.error("chrome.runtime fetchChromeJson request error", e) + } sendResponse(resJson); })(); break; diff --git a/src/components/tab/main/MetaweblogMain.vue b/src/components/tab/main/MetaweblogMain.vue index 520f6de9..5269d342 100644 --- a/src/components/tab/main/MetaweblogMain.vue +++ b/src/components/tab/main/MetaweblogMain.vue @@ -340,7 +340,13 @@ const initPage = async () => { } // 全部文章分类请求 - const catInfo: CategoryInfo[] = await api.getCategories() + let catInfo: CategoryInfo[] = [] + try { + catInfo = await api.getCategories() + } catch (e) { + isInitLoadding.value = false + logUtil.logError("分类获取失败", e) + } logUtil.logInfo("catInfo=>", catInfo) // 组装分类 diff --git a/src/lib/platform/commonblog/commonblogApi.ts b/src/lib/platform/commonblog/commonblogApi.ts index faed0ff9..3e626962 100644 --- a/src/lib/platform/commonblog/commonblogApi.ts +++ b/src/lib/platform/commonblog/commonblogApi.ts @@ -58,22 +58,13 @@ export class CommonblogApi { * @param fetchOptions 请求参数 * @param formJson 可选,发送form请求才需要 */ - private async fetchChromeCORS(apiUrl: string, fetchOptions: RequestInit, formJson?: any[]): Promise { + private async fetchChromeCORS(apiUrl: string, fetchOptions: RequestInit): Promise { let result logUtil.logInfo("fetchChrome apiUrl=>") logUtil.logInfo(apiUrl) const fetchCORSOptions = fetchOptions - // 如果是form请求,进行转换 - if (formJson) { - // 将formJson转换为formData - const form = new URLSearchParams(); - formJson.forEach((item: any) => { - form.append(item.key, item.value) - }) - fetchCORSOptions.body = form - } - logUtil.logInfo("fetchChrome apiUrl=>", fetchCORSOptions) + logUtil.logWarn("fetchChrome fetchCORSOptions=>", fetchCORSOptions) const resJson = await sendChromeMessage({ // 里面的值应该可以自定义,用于判断哪个请求之类的 @@ -103,7 +94,7 @@ export class CommonblogApi { result = await fetch(apiUrl, fetchOptions) } else if (isInChromeExtension()) { logUtil.logInfo("当前处于Chrome插件中,需要模拟fetch解决CORS跨域问题") - result = await this.fetchChromeCORS(apiUrl, fetchOptions, formJson) + result = await this.fetchChromeCORS(apiUrl, fetchOptions) } else { logUtil.logInfo("当前处于非挂件模式,已开启请求代理解决CORS跨域问题") logUtil.logInfo("formJson=>", formJson) @@ -157,10 +148,7 @@ export class CommonblogApi { let resJson - if (typeof response == "string") { - console.log(response) - throw new Error("未解析") - } else { + if (response instanceof Response) { // 解析响应体并返回响应结果 const statusCode = response.status @@ -183,9 +171,10 @@ export class CommonblogApi { const corsJson = await response.json() resJson = this.parseCORSBody(corsJson) } + } else { + resJson = response } - return resJson } @@ -198,7 +187,7 @@ export class CommonblogApi { */ protected async doFormFetch(apiUrl: string, fetchOptions: RequestInit, formJson: any[]): Promise { const widgetResult = await getWidgetId() - if (widgetResult.isInSiyuan) { + if (widgetResult.isInSiyuan || isInChromeExtension()) { // 将formJson转换为formData const form = new URLSearchParams(); formJson.forEach((item) => { diff --git a/src/lib/platform/metaweblog/CustomMetaweblogApi.ts b/src/lib/platform/metaweblog/CustomMetaweblogApi.ts index 387cdcb0..2fdb33ed 100644 --- a/src/lib/platform/metaweblog/CustomMetaweblogApi.ts +++ b/src/lib/platform/metaweblog/CustomMetaweblogApi.ts @@ -37,6 +37,10 @@ export class CustomMetaWeblogApi { const faultString = this.parseFieldValue(fault, "faultString") throw new Error("发生异常,错误码=>" + faultCode + ",错误信息=>" + faultString) } + + if(!ret || !ret.params){ + throw new Error("发生异常=>数据为空") + } } public async getUsersBlogs(appkey: string, username: string, password: string): Promise> { diff --git a/src/lib/platform/metaweblog/customXmlrpc.ts b/src/lib/platform/metaweblog/customXmlrpc.ts index d3c7ce86..0542e679 100644 --- a/src/lib/platform/metaweblog/customXmlrpc.ts +++ b/src/lib/platform/metaweblog/customXmlrpc.ts @@ -44,11 +44,16 @@ export async function fetchCustom(apiUrl: string, reqMethod: string, reqParams: }); logUtil.logInfo("fetchChromeXmlrpc resXml=>", resXml) - const parseResult: any = xmlParser.parse(resXml) - logUtil.logInfo("parseResult=>", parseResult) + let resJson + if (resXml) { + const parseResult: any = xmlParser.parse(resXml) + logUtil.logInfo("parseResult=>", parseResult) - const resJson = parseResult.methodResponse || {} - logUtil.logInfo("resJson=>", JSON.stringify(resJson)) + resJson = parseResult.methodResponse || {} + logUtil.logInfo("resJson=>", JSON.stringify(resJson)) + } else { + resJson = {} + } return resJson } catch (e: any) {