Skip to content

Commit

Permalink
feat: gm.xhr支持发送FormData数据
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Nov 9, 2021
1 parent aff0bb4 commit 693c410
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/apps/grant/background.ts
@@ -1,5 +1,5 @@
import { PermissionModel } from "@App/model/permission";
import { isFirefox } from "@App/pkg/utils/utils";
import { base64ToBlob, isFirefox } from "@App/pkg/utils/utils";
import { App } from "../app";
import { AppEvent, ListenGmLog, PermissionConfirm, ScriptGrant, ScriptValueChange, TabMenuClick, TabRemove } from "../msg-center/event";
import { MsgCenter } from "../msg-center/msg-center";
Expand Down Expand Up @@ -521,6 +521,18 @@ export class BackgroundGrant {
if (config.overrideMimeType) {
xhr.overrideMimeType(config.overrideMimeType);
}
if ((<any>config).dataType && (<any>config).dataType == "FormData") {
let data = new FormData();
for (const key in config.data) {
let val = config.data[key];
if (val.type == "file") {
data.append(val.key, base64ToBlob(val.val), val.filename);
} else {
data.append(val.key, val.val);
}
}
config.data = data;
}
xhr.send(config.data);
return resolve(undefined);
});
Expand Down
33 changes: 31 additions & 2 deletions src/apps/grant/frontend.ts
@@ -1,7 +1,7 @@
import { ScriptCache } from "@App/model/do/script";
import { Value } from "@App/model/do/value";
import { addStyle } from "@App/pkg/frontend";
import { randomInt, randomString } from "@App/pkg/utils/utils";
import { blobToBase64, randomInt, randomString } from "@App/pkg/utils/utils";
import { BrowserMsg } from "../msg-center/browser";
import { AppEvent, ScriptValueChange } from "../msg-center/event";
import { Grant } from "./interface";
Expand Down Expand Up @@ -133,7 +133,7 @@ export class FrontendGrant implements ScriptContext {
}

@FrontendGrant.GMFunction({ depend: ['CAT_fetchBlob'] })
public GM_xmlhttpRequest(details: GM_Types.XHRDetails) {
public async GM_xmlhttpRequest(details: GM_Types.XHRDetails) {
let param: GM_Types.XHRDetails = {
method: details.method,
timeout: details.timeout,
Expand All @@ -151,6 +151,35 @@ export class FrontendGrant implements ScriptContext {
if (details.nocache) {
param.headers!["Cache-Control"] = 'no-cache';
}
if (param.data instanceof FormData) {
(<any>param).dataType = "FormData";
let data = [];
let keys: { [key: string]: boolean } = {};
param.data.forEach((val, key) => {
keys[key] = true;
});
for (const key in keys) {
let values = param.data.getAll(key);
for (let i = 0; i < values.length; i++) {
let val = values[i];
if (val instanceof File) {
data.push({
key: key,
type: "file",
val: await blobToBase64(val),
filename: val.name
});
} else {
data.push({
key: key,
type: "text",
val: val
});
}
}
}
param.data = data;
}

if (details.onload && (details.responseType == "arraybuffer" || details.responseType == "blob")) {
let old = details.onload;
Expand Down
12 changes: 12 additions & 0 deletions src/pkg/utils/utils.ts
Expand Up @@ -262,6 +262,18 @@ export function blobToBase64(blob: Blob): Promise<string | null> {
});
}

export function base64ToBlob(dataURI: string) {
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
var byteString = atob(dataURI.split(',')[1]);
var arrayBuffer = new ArrayBuffer(byteString.length);
var intArray = new Uint8Array(arrayBuffer);

for (var i = 0; i < byteString.length; i++) {
intArray[i] = byteString.charCodeAt(i);
}
return new Blob([intArray], { type: mimeString });
}

export function base64ToStr(base64: string): string {
return decodeURIComponent(atob(base64).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
Expand Down

0 comments on commit 693c410

Please sign in to comment.