Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:#85 Google插件扩展-修复Form表单参数传递 #100

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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