Skip to content

Commit

Permalink
feat:#85 Google插件扩展-公共请求及错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Sep 26, 2022
1 parent 83a3693 commit 933ed0a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
22 changes: 16 additions & 6 deletions public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/components/tab/main/MetaweblogMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
// 组装分类
Expand Down
25 changes: 7 additions & 18 deletions src/lib/platform/commonblog/commonblogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,13 @@ export class CommonblogApi {
* @param fetchOptions 请求参数
* @param formJson 可选,发送form请求才需要
*/
private async fetchChromeCORS(apiUrl: string, fetchOptions: RequestInit, formJson?: any[]): Promise<string> {
private async fetchChromeCORS(apiUrl: string, fetchOptions: RequestInit): Promise<string> {
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({
// 里面的值应该可以自定义,用于判断哪个请求之类的
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -183,9 +171,10 @@ export class CommonblogApi {
const corsJson = await response.json()
resJson = this.parseCORSBody(corsJson)
}
} else {
resJson = response
}


return resJson
}

Expand All @@ -198,7 +187,7 @@ export class CommonblogApi {
*/
protected async doFormFetch(apiUrl: string, fetchOptions: RequestInit, formJson: any[]): Promise<any> {
const widgetResult = await getWidgetId()
if (widgetResult.isInSiyuan) {
if (widgetResult.isInSiyuan || isInChromeExtension()) {
// 将formJson转换为formData
const form = new URLSearchParams();
formJson.forEach((item) => {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/platform/metaweblog/CustomMetaweblogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<UserBlog>> {
Expand Down
13 changes: 9 additions & 4 deletions src/lib/platform/metaweblog/customXmlrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 933ed0a

Please sign in to comment.