Skip to content

Commit 05a71f0

Browse files
authored
⚡️ Tree Shaking (#505)
* CloudScript * favicon.ts 優化 * types.ts * example空白修正 * Tree Shaking * 处理双重uuid * 修正
1 parent bbc2550 commit 05a71f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1071
-763
lines changed

example/gm_bg_menu.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// @description 在后台脚本中使用菜单
66
// @author You
77
// @background
8-
// @grant GM_registerMenuCommand
9-
// @grant GM_unregisterMenuCommand
8+
// @grant GM_registerMenuCommand
9+
// @grant GM_unregisterMenuCommand
1010
// ==/UserScript==
1111

1212
return new Promise((resolve) => {

example/gm_tab.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// @description 打开一个标签页
66
// @author You
77
// @match https://bbs.tampermonkey.net.cn/
8-
// @grant GM_openInTab
8+
// @grant GM_openInTab
99
// ==/UserScript==
1010

1111
const tab = GM_openInTab("https://scriptcat.org/search");

example/userconfig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// @description 会在页面上显示用户配置,可以可视化的进行配置
66
// @author You
77
// @background
8-
// @grant GM_getValue
9-
// @grant CAT_userConfig
8+
// @grant GM_getValue
9+
// @grant CAT_userConfig
1010
// ==/UserScript==
1111

1212
/* ==UserConfig==

packages/cloudscript/README.md

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

packages/cloudscript/factory.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ExportTarget } from "@App/app/repo/export";
2+
import { ExportParams } from "./cloudscript";
3+
import LocalCloudScript from "./local";
4+
5+
export interface CloudScriptParams {
6+
[key: string]: {
7+
title: string;
8+
type?: "select";
9+
options?: string[];
10+
};
11+
}
12+
13+
export default class CloudScriptFactory {
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+
}
22+
23+
static params(): { [key: string]: CloudScriptParams } {
24+
return {
25+
local: {},
26+
};
27+
}
28+
}

packages/cloudscript/local.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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(script: Script, code: string, values: Value[], cookies: ExportCookies[]): Promise<void> {
22+
this.zip.file("userScript.js", code);
23+
this.zip.file("cookies.js", `exports.cookies = ${JSON.stringify(cookies)}`);
24+
this.zip.file("values.js", `exports.values = ${JSON.stringify(values)}`);
25+
this.zip.file(
26+
"config.js",
27+
`export default ${JSON.stringify({
28+
version: ExtVersion,
29+
uuid: script.uuid,
30+
overwrite: {
31+
value: this.params.overwriteValue,
32+
cookie: this.params.overwriteCookie,
33+
},
34+
})}`
35+
);
36+
this.zip.file("package.json", <string>packageTpl);
37+
this.zip.file("utils.js", <string>utilsTpl);
38+
this.zip.file("index.js", <string>indexTpl);
39+
return Promise.resolve();
40+
}
41+
}

packages/filesystem/baidu/baidu.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AuthVerify } from "../auth";
2-
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
2+
import type FileSystem from "../filesystem";
3+
import type { File, FileReader, FileWriter } from "../filesystem";
34
import { joinPath } from "../utils";
45
import { BaiduFileReader, BaiduFileWriter } from "./rw";
56

packages/filesystem/baidu/rw.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { calculateMd5 } from "@App/pkg/utils/utils";
2-
import { MD5 } from "crypto-js";
3-
import { File, FileReader, FileWriter } from "../filesystem";
1+
import type { File, FileReader, FileWriter } from "../filesystem";
2+
import { calculateMd5, md5OfText } from "@App/pkg/utils/crypto";
43
import BaiduFileSystem from "./baidu";
54

65
export class BaiduFileReader implements FileReader {
@@ -55,7 +54,7 @@ export class BaiduFileWriter implements FileWriter {
5554
if (content instanceof Blob) {
5655
return calculateMd5(content);
5756
}
58-
return MD5(content).toString();
57+
return md5OfText(content);
5958
}
6059

6160
async write(content: string | Blob): Promise<void> {

packages/filesystem/factory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import GoogleDriveFileSystem from "./googledrive/googledrive";
44
import OneDriveFileSystem from "./onedrive/onedrive";
55
import WebDAVFileSystem from "./webdav/webdav";
66
import ZipFileSystem from "./zip/zip";
7-
import i18n from "@App/locales/locales";
7+
import { t } from "@App/locales/locales";
88

99
export type FileSystemType = "zip" | "webdav" | "baidu-netdsik" | "onedrive" | "googledrive";
1010

@@ -50,13 +50,13 @@ export default class FileSystemFactory {
5050
return {
5151
webdav: {
5252
authType: {
53-
title: i18n.t("auth_type"),
53+
title: t("auth_type"),
5454
type: "select",
5555
options: ["password", "digest", "none", "token"],
5656
},
57-
url: { title: i18n.t("url") },
58-
username: { title: i18n.t("username") },
59-
password: { title: i18n.t("password"), type: "password" },
57+
url: { title: t("url") },
58+
username: { title: t("username") },
59+
password: { title: t("password"), type: "password" },
6060
},
6161
"baidu-netdsik": {},
6262
onedrive: {},

0 commit comments

Comments
 (0)