Skip to content

Commit

Permalink
Merge pull request #100 from terwer/dev
Browse files Browse the repository at this point in the history
feat:#85 Google插件扩展-修复Form表单参数传递
  • Loading branch information
terwer committed Sep 26, 2022
2 parents bb122eb + 0cb8be2 commit c192ebf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
17 changes: 16 additions & 1 deletion public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,22 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
(async () => {
let resJson
try {
const response = await fetch(request.apiUrl, request.fetchCORSOptions);
const fetchCORSOptions = request.fetchCORSOptions
const formJsonText = request.formJson
// console.log("formJsonText=>", formJsonText)
if (formJsonText) {
const formJson = JSON.parse(formJsonText)
// 将formJson转换为formData
const form = new URLSearchParams();
formJson.forEach(function (item) {
form.append(item.key, item.value)
})
fetchCORSOptions.body = form
// console.log("fetchCORSOptions.body=>", form)
}
// console.log("chrome.runtime fetchChromeJson apiUrl", request.apiUrl)
// console.log("chrome.runtime fetchChromeJson reqOps", fetchCORSOptions)
const response = await fetch(request.apiUrl, fetchCORSOptions);
resJson = await response.json()
// console.log("chrome.runtime.onMessage.addListener fetchChromeJson response:", resJson)
} catch (e) {
Expand Down
47 changes: 27 additions & 20 deletions src/lib/platform/commonblog/commonblogApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,28 @@ export class CommonblogApi {
* @param fetchOptions 请求参数
* @param formJson 可选,发送form请求才需要
*/
private async fetchChromeCORS(apiUrl: string, fetchOptions: RequestInit): Promise<string> {
let result
logUtil.logInfo("fetchChrome apiUrl=>")
logUtil.logInfo(apiUrl)

const fetchCORSOptions = fetchOptions
logUtil.logWarn("fetchChrome fetchCORSOptions=>", fetchCORSOptions)

const resJson = await sendChromeMessage({
// 里面的值应该可以自定义,用于判断哪个请求之类的
type: 'fetchChromeJson',
apiUrl: apiUrl, // 需要请求的url
fetchCORSOptions: fetchCORSOptions
});
logUtil.logInfo("fetchChromeJson resJson=>", resJson)

// @ts-ignore
return resJson;
private async fetchChromeCORS(apiUrl: string, fetchOptions: RequestInit, formJson?: any[]): Promise<string> {
try {
const reqOps = {
// 里面的值应该可以自定义,用于判断哪个请求之类的
type: 'fetchChromeJson',
apiUrl: apiUrl, // 需要请求的url
fetchCORSOptions: fetchOptions,
}
if (formJson) {
Object.assign(reqOps, {
formJson: JSON.stringify(formJson)
})
}
logUtil.logInfo("fetchChrome reqOps=>", reqOps)
const resJson = await sendChromeMessage(reqOps);
logUtil.logInfo("fetchChromeJson resJson=>", resJson)

// @ts-ignore
return resJson;
} catch (e: any) {
throw new Error("请求异常", e)
}
}

/**
Expand All @@ -94,7 +98,7 @@ export class CommonblogApi {
result = await fetch(apiUrl, fetchOptions)
} else if (isInChromeExtension()) {
logUtil.logInfo("当前处于Chrome插件中,需要模拟fetch解决CORS跨域问题")
result = await this.fetchChromeCORS(apiUrl, fetchOptions)
result = await this.fetchChromeCORS(apiUrl, fetchOptions, formJson)
} else {
logUtil.logInfo("当前处于非挂件模式,已开启请求代理解决CORS跨域问题")
logUtil.logInfo("formJson=>", formJson)
Expand Down Expand Up @@ -187,14 +191,17 @@ export class CommonblogApi {
*/
protected async doFormFetch(apiUrl: string, fetchOptions: RequestInit, formJson: any[]): Promise<any> {
const widgetResult = await getWidgetId()
if (widgetResult.isInSiyuan || isInChromeExtension()) {
if (widgetResult.isInSiyuan) {
// 将formJson转换为formData
const form = new URLSearchParams();
formJson.forEach((item) => {
form.append(item.key, item.value)
})
fetchOptions.body = form
return await this.doFetch(apiUrl, fetchOptions)
}
if (isInChromeExtension()) {
return await this.doFetch(apiUrl, fetchOptions, formJson)
} else {
return await this.doFetch(apiUrl, fetchOptions, formJson)
}
Expand Down

1 comment on commit c192ebf

@vercel
Copy link

@vercel vercel bot commented on c192ebf Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.