Skip to content

Commit 844d424

Browse files
committed
♻️ 重构导出云脚本
1 parent 02d1a45 commit 844d424

7 files changed

Lines changed: 376 additions & 125 deletions

File tree

‎pkg/cloudscript/cloudscript.ts‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import IoC from "@App/app/ioc";
2+
import { Script } from "@App/app/repo/scripts";
3+
import { Value, ValueDAO } from "@App/app/repo/value";
4+
import ValueManager from "@App/app/service/value/manager";
5+
6+
export type ExportCookies = {
7+
[key: string]: any;
8+
domain?: string;
9+
url?: string;
10+
cookie: chrome.cookies.Cookie[];
11+
};
12+
13+
export type ExportParams = {
14+
[key: string]: any;
15+
exportValue: string;
16+
exportCookie: string;
17+
overwriteValue: boolean;
18+
overwriteCookie: boolean;
19+
};
20+
21+
export default interface CloudScript {
22+
exportCloud(
23+
script: Script,
24+
values: Value[],
25+
cookies: ExportCookies[]
26+
): Promise<void>;
27+
}
28+
29+
function getCookies(
30+
detail: chrome.cookies.GetAllDetails
31+
): Promise<chrome.cookies.Cookie[]> {
32+
return new Promise((resolve) => {
33+
chrome.cookies.getAll(detail, (cookies) => {
34+
resolve(cookies);
35+
});
36+
});
37+
}
38+
39+
// 解析导出cookie表达式生成导出的cookie
40+
export function parseExportCookie(
41+
exportCookie: string
42+
): Promise<ExportCookies[]> {
43+
const lines = exportCookie.split("\n");
44+
const result = [];
45+
for (let i = 0; i < lines.length; i += 1) {
46+
const line = lines[i];
47+
const detail: ExportCookies = {
48+
cookie: [],
49+
};
50+
if (line.trim()) {
51+
line.split(";").forEach((param) => {
52+
const s = param.split("=");
53+
if (s.length !== 2) {
54+
return;
55+
}
56+
detail[s[0].trim()] = s[1].trim();
57+
});
58+
if (!detail.url && !detail.domain) {
59+
result.push(
60+
new Promise<ExportCookies>((resolve) => {
61+
detail.cookies = getCookies(detail);
62+
resolve(detail);
63+
})
64+
);
65+
}
66+
}
67+
}
68+
return Promise.all(result);
69+
}
70+
71+
// 解析value表达式生成导出的value
72+
export async function parseExportValue(
73+
script: Script,
74+
exportValue: string
75+
): Promise<Value[]> {
76+
const lines = exportValue.split("\n");
77+
const result = [];
78+
const valueManager = IoC.instance(ValueManager) as ValueManager;
79+
const values = await valueManager.getValues(script);
80+
for (let i = 0; i < lines.length; i += 1) {
81+
const line = lines[i];
82+
if (line.trim()) {
83+
const s = line.split(",");
84+
for (let n = 0; n < s.length; n += 1) {
85+
const key = s[n].trim();
86+
if (key && values[key]) {
87+
result.push(values[key]);
88+
}
89+
}
90+
}
91+
}
92+
return result;
93+
}

‎pkg/cloudscript/factory.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { ExportTarget } from "@App/app/repo/export";
2+
import { ExportParams } from "./cloudscript";
3+
import LocalCloudScript from "./local";
4+
15
export interface CloudScriptParams {
26
[key: string]: {
37
title: string;
@@ -7,7 +11,14 @@ export interface CloudScriptParams {
711
}
812

913
export default class CloudScriptFactory {
10-
static create() {}
14+
static create(type: ExportTarget, params: ExportParams) {
15+
switch (type) {
16+
case "local":
17+
return new LocalCloudScript(params);
18+
default:
19+
throw new Error(`unknown type ${type}`);
20+
}
21+
}
1122

1223
static params(): { [key: string]: CloudScriptParams } {
1324
return {

‎pkg/cloudscript/local.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { ExtVersion } from "@App/app/const";
2+
import { Script } from "@App/app/repo/scripts";
3+
import { Value } from "@App/app/repo/value";
4+
import JSZip from "jszip";
5+
import packageTpl from "@App/template/cloudcat-package/package.tpl";
6+
import utilsTpl from "@App/template/cloudcat-package/utils.tpl";
7+
import indexTpl from "@App/template/cloudcat-package/index.tpl";
8+
import CloudScript, { ExportCookies, ExportParams } from "./cloudscript";
9+
10+
// 导出到本地,一个可执行到npm包
11+
export default class LocalCloudScript implements CloudScript {
12+
zip: JSZip;
13+
14+
params: ExportParams;
15+
16+
constructor(params: ExportParams) {
17+
this.zip = params.zip! as JSZip;
18+
this.params = params;
19+
}
20+
21+
exportCloud(
22+
script: Script,
23+
values: Value[],
24+
cookies: ExportCookies[]
25+
): Promise<void> {
26+
this.zip.file("userScript.js", script.code);
27+
this.zip.file("cookies.js", `exports.cookies = ${JSON.stringify(cookies)}`);
28+
this.zip.file("values.js", `exports.values = ${JSON.stringify(values)}`);
29+
this.zip.file(
30+
"config.js",
31+
`export default ${JSON.stringify({
32+
version: ExtVersion,
33+
uuid: script.uuid,
34+
overwrite: {
35+
value: this.params.overwriteValue,
36+
cookie: this.params.overwriteCookie,
37+
},
38+
})}`
39+
);
40+
this.zip.file("package.json", <string>packageTpl);
41+
this.zip.file("utils.js", <string>utilsTpl);
42+
this.zip.file("index.js", <string>indexTpl);
43+
return Promise.resolve();
44+
}
45+
}

‎src/app/repo/export.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
import { ExportParams } from "@Pkg/cloudscript/cloudscript";
12
import { DAO, db } from "./dao";
23

3-
export type ExportTarget = "local" | "tencentCloud" | "";
4+
export type ExportTarget = "local" | "tencentCloud";
45

56
// 导出与本地脚本关联记录
67
export interface Export {
78
id: number;
89
scriptId: number;
9-
params?: {
10-
[key: string]: {
11-
[key: string]: any;
12-
};
10+
params: {
11+
[key: string]: ExportParams;
1312
};
1413
// 导出目标
1514
target: ExportTarget;
@@ -22,4 +21,8 @@ export class ExportDAO extends DAO<Export> {
2221
super();
2322
this.table = db.table(this.tableName);
2423
}
24+
25+
findByScriptID(scriptID: number) {
26+
return this.table.where({ scriptId: scriptID }).first();
27+
}
2528
}

‎src/pages/components/CloudScript/index.tsx‎

Lines changed: 0 additions & 117 deletions
This file was deleted.

0 commit comments

Comments
 (0)