Skip to content

Commit

Permalink
♻️ 重构导出云脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Nov 6, 2022
1 parent 02d1a45 commit 844d424
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 125 deletions.
93 changes: 93 additions & 0 deletions pkg/cloudscript/cloudscript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import IoC from "@App/app/ioc";
import { Script } from "@App/app/repo/scripts";
import { Value, ValueDAO } from "@App/app/repo/value";
import ValueManager from "@App/app/service/value/manager";

export type ExportCookies = {
[key: string]: any;
domain?: string;
url?: string;
cookie: chrome.cookies.Cookie[];
};

export type ExportParams = {
[key: string]: any;
exportValue: string;
exportCookie: string;
overwriteValue: boolean;
overwriteCookie: boolean;
};

export default interface CloudScript {
exportCloud(
script: Script,
values: Value[],
cookies: ExportCookies[]
): Promise<void>;
}

function getCookies(
detail: chrome.cookies.GetAllDetails
): Promise<chrome.cookies.Cookie[]> {
return new Promise((resolve) => {
chrome.cookies.getAll(detail, (cookies) => {
resolve(cookies);
});
});
}

// 解析导出cookie表达式生成导出的cookie
export function parseExportCookie(
exportCookie: string
): Promise<ExportCookies[]> {
const lines = exportCookie.split("\n");
const result = [];
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
const detail: ExportCookies = {
cookie: [],
};
if (line.trim()) {
line.split(";").forEach((param) => {
const s = param.split("=");
if (s.length !== 2) {
return;
}
detail[s[0].trim()] = s[1].trim();
});
if (!detail.url && !detail.domain) {
result.push(
new Promise<ExportCookies>((resolve) => {
detail.cookies = getCookies(detail);
resolve(detail);
})
);
}
}
}
return Promise.all(result);
}

// 解析value表达式生成导出的value
export async function parseExportValue(
script: Script,
exportValue: string
): Promise<Value[]> {
const lines = exportValue.split("\n");
const result = [];
const valueManager = IoC.instance(ValueManager) as ValueManager;
const values = await valueManager.getValues(script);
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i];
if (line.trim()) {
const s = line.split(",");
for (let n = 0; n < s.length; n += 1) {
const key = s[n].trim();
if (key && values[key]) {
result.push(values[key]);
}
}
}
}
return result;
}
13 changes: 12 additions & 1 deletion pkg/cloudscript/factory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { ExportTarget } from "@App/app/repo/export";
import { ExportParams } from "./cloudscript";
import LocalCloudScript from "./local";

export interface CloudScriptParams {
[key: string]: {
title: string;
Expand All @@ -7,7 +11,14 @@ export interface CloudScriptParams {
}

export default class CloudScriptFactory {
static create() {}
static create(type: ExportTarget, params: ExportParams) {
switch (type) {
case "local":
return new LocalCloudScript(params);
default:
throw new Error(`unknown type ${type}`);
}
}

static params(): { [key: string]: CloudScriptParams } {
return {
Expand Down
45 changes: 45 additions & 0 deletions pkg/cloudscript/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ExtVersion } from "@App/app/const";
import { Script } from "@App/app/repo/scripts";
import { Value } from "@App/app/repo/value";
import JSZip from "jszip";
import packageTpl from "@App/template/cloudcat-package/package.tpl";
import utilsTpl from "@App/template/cloudcat-package/utils.tpl";
import indexTpl from "@App/template/cloudcat-package/index.tpl";
import CloudScript, { ExportCookies, ExportParams } from "./cloudscript";

// 导出到本地,一个可执行到npm包
export default class LocalCloudScript implements CloudScript {
zip: JSZip;

params: ExportParams;

constructor(params: ExportParams) {
this.zip = params.zip! as JSZip;
this.params = params;
}

exportCloud(
script: Script,
values: Value[],
cookies: ExportCookies[]
): Promise<void> {
this.zip.file("userScript.js", script.code);
this.zip.file("cookies.js", `exports.cookies = ${JSON.stringify(cookies)}`);
this.zip.file("values.js", `exports.values = ${JSON.stringify(values)}`);
this.zip.file(
"config.js",
`export default ${JSON.stringify({
version: ExtVersion,
uuid: script.uuid,
overwrite: {
value: this.params.overwriteValue,
cookie: this.params.overwriteCookie,
},
})}`
);
this.zip.file("package.json", <string>packageTpl);
this.zip.file("utils.js", <string>utilsTpl);
this.zip.file("index.js", <string>indexTpl);
return Promise.resolve();
}
}
13 changes: 8 additions & 5 deletions src/app/repo/export.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ExportParams } from "@Pkg/cloudscript/cloudscript";
import { DAO, db } from "./dao";

export type ExportTarget = "local" | "tencentCloud" | "";
export type ExportTarget = "local" | "tencentCloud";

// 导出与本地脚本关联记录
export interface Export {
id: number;
scriptId: number;
params?: {
[key: string]: {
[key: string]: any;
};
params: {
[key: string]: ExportParams;
};
// 导出目标
target: ExportTarget;
Expand All @@ -22,4 +21,8 @@ export class ExportDAO extends DAO<Export> {
super();
this.table = db.table(this.tableName);
}

findByScriptID(scriptID: number) {
return this.table.where({ scriptId: scriptID }).first();
}
}
117 changes: 0 additions & 117 deletions src/pages/components/CloudScript/index.tsx

This file was deleted.

Loading

0 comments on commit 844d424

Please sign in to comment.