Skip to content

Commit 48d197c

Browse files
committed
✨ 支持zip格式导入导出
1 parent 6cd369e commit 48d197c

File tree

15 files changed

+612
-255
lines changed

15 files changed

+612
-255
lines changed

i18n/i18n.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { zh_CN } from './cn';
22
import { en } from './en';
33
import VueI18n from 'vue-i18n';
4-
import Vuetify from "vuetify";
5-
import Vue from "vue";
4+
import Vuetify from 'vuetify';
5+
import Vue from 'vue';
66
// import '@mdi/font/css/materialdesignicons.css'
77

88
Vue.use(VueI18n);
99
Vue.use(Vuetify);
1010

11-
export const messages = {
12-
"zh_CN": zh_CN,
13-
"en": en,
11+
export const messages: { [key: string]: any } = {
12+
'zh_CN': zh_CN,
13+
'en': en,
1414
};
1515

1616
let defaultLocale = chrome.i18n.getUILanguage().replace(/-/g, '_');
1717

18-
if (!(<any>messages)[defaultLocale]) {
19-
defaultLocale = "en";
18+
if (!messages[defaultLocale]) {
19+
defaultLocale = 'en';
2020
}
2121

2222
export const i18n = new VueI18n({

src/apps/msg-center/event.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export const SyncTaskEvent = 'sync-task-event';
4545
export const TriggerSync = 'trigger-sync';
4646

4747
export const OpenImportFileWindow = 'open-import-file-window';
48-
export const ImportFile = 'import-file';
4948
export const RequestImportFile = 'request-import-file';
5049

5150
export const ToolsConnectVSCode = 'tools-connect-vscode';

src/apps/resource.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ export class ResourceManager {
8080
});
8181
}
8282

83+
// 方便识别text文本储存
84+
static textContentTypeMap = new Map<string, boolean>().
85+
set('application/javascript', true).
86+
set('application/json', true);
87+
8388
public loadByUrl(url: string): Promise<Resource | undefined> {
8489
return new Promise(resolve => {
8590
const u = this.parseUrl(url);
@@ -93,11 +98,13 @@ export class ResourceManager {
9398
const resource: Resource = {
9499
id: 0,
95100
url: u.url, content: '',
96-
contentType: (<string>((<AnyMap>response.headers)['content-type']) || '').split(';')[0],
101+
contentType: (<string>((<AnyMap>response.headers)['content-type']) || 'application/octet-stream').split(';')[0],
97102
hash: await this.calculateHash(<Blob>response.data),
98103
base64: '',
99104
};
100-
resource.content = await (<Blob>response.data).text();
105+
if (resource.contentType.startsWith('text/') || ResourceManager.textContentTypeMap.has(resource.contentType)) {
106+
resource.content = await (<Blob>response.data).text();
107+
}
101108
resource.base64 = await blobToBase64(<Blob>response.data) || '';
102109
App.Log.Info('resource', u.url, 'load');
103110
return resolve(resource);

src/apps/script/controller.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
SubscribeUpdate,
2525
Unsubscribe,
2626
SubscribeCheckUpdate,
27-
ImportFile,
2827
OpenImportFileWindow,
2928
RequestImportFile,
3029
ScriptValueChange
@@ -44,7 +43,6 @@ import { ResourceManager } from '../resource';
4443
import { compileScriptCode } from '@App/pkg/sandbox/compile';
4544
import { SubscribeModel } from '@App/model/subscribe';
4645
import { Subscribe, SUBSCRIBE_STATUS_DISABLE, SUBSCRIBE_STATUS_ENABLE } from '@App/model/do/subscribe';
47-
import { ExportFile } from '@App/model/do/backup';
4846

4947
// 脚本控制器,发送或者接收来自管理器的消息,并不对脚本数据做实际的处理
5048
export class ScriptController {
@@ -180,10 +178,10 @@ export class ScriptController {
180178
});
181179
}
182180

183-
public getImportFile(uuid: string): Promise<ExportFile> {
181+
public getImportFile(uuid: string): Promise<{ name: string, url: string }> {
184182
return new Promise(resolve => {
185183
MsgCenter.sendMessage(RequestImportFile, uuid, resp => {
186-
resolve(<ExportFile>resp);
184+
resolve(<{ name: string, url: string }>resp);
187185
});
188186
});
189187
}
@@ -565,20 +563,9 @@ export class ScriptController {
565563
})
566564
}
567565

568-
public parseBackFile(str: string): { data?: ExportFile, err?: string } {
569-
const data = <ExportFile>JSON.parse(str);
570-
if (!data.created_by) {
571-
return { err: '错误的格式' };
572-
}
573-
if (!data.scripts) {
574-
return { err: '脚本为空' }
575-
}
576-
return { data: data };
577-
}
578-
579-
public openImportFileWindow(file: ExportFile): Promise<any> {
566+
public openImportFileWindow(name: string, url: string): Promise<any> {
580567
return new Promise(resolve => {
581-
MsgCenter.sendMessage(OpenImportFileWindow, file, (resp) => {
568+
MsgCenter.sendMessage(OpenImportFileWindow, { name, url }, (resp) => {
582569
resolve(resp);
583570
});
584571
});

src/apps/script/manager.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios';
22
import { MsgCenter } from '@App/apps/msg-center/msg-center';
3-
import { AppEvent, ScriptExec, ScriptRunStatusChange, ScriptStatusChange, ScriptStop, ScriptUninstall, ScriptReinstall, ScriptValueChange, TabRemove, RequestTabRunScript, ScriptInstall, RequestInstallInfo, ScriptCheckUpdate, RequestConfirmInfo, ListenGmLog, SubscribeUpdate, Unsubscribe, SubscribeCheckUpdate, ImportFile, OpenImportFileWindow, RequestImportFile, ScriptInstallByURL } from '@App/apps/msg-center/event';
3+
import { AppEvent, ScriptExec, ScriptRunStatusChange, ScriptStatusChange, ScriptStop, ScriptUninstall, ScriptReinstall, ScriptValueChange, TabRemove, RequestTabRunScript, ScriptInstall, RequestInstallInfo, ScriptCheckUpdate, RequestConfirmInfo, ListenGmLog, SubscribeUpdate, Unsubscribe, SubscribeCheckUpdate, OpenImportFileWindow, RequestImportFile, ScriptInstallByURL } from '@App/apps/msg-center/event';
44
import { dealScript, get, randomString } from '@App/pkg/utils/utils';
55
import { App } from '../app';
66
import { UrlMatch } from '@App/pkg/match';
@@ -19,7 +19,6 @@ import { Subscribe } from '@App/model/do/subscribe';
1919
import { SubscribeModel } from '@App/model/subscribe';
2020
import { SyncModel } from '@App/model/sync';
2121
import { SyncAction, SyncData } from '@App/model/do/sync';
22-
import { ExportFile } from '@App/model/do/backup';
2322
import { v4 as uuidv4 } from 'uuid';
2423
import { Manager } from '@App/pkg/apps/manager';
2524

@@ -103,7 +102,6 @@ export class ScriptManager extends Manager {
103102
this.listenerMessage(SubscribeCheckUpdate, this.subscribeCheckUpdate);
104103

105104
this.listenerMessage(OpenImportFileWindow, this.openImportFileWindow)
106-
this.listenerMessage(ImportFile, this.importFile)
107105
this.listenerMessage(RequestImportFile, this.requestImportFile)
108106

109107
// 监听事件,并转发
@@ -196,11 +194,11 @@ export class ScriptManager extends Manager {
196194
});
197195
}
198196

199-
public openImportFileWindow(file: ExportFile): Promise<any> {
197+
public openImportFileWindow(file: { name: string, url: string }): Promise<any> {
200198
return new Promise(resolve => {
201199
// 打开导入窗口
202200
const uuid = uuidv4()
203-
App.Cache.set('import:info:' + uuid, file);
201+
void App.Cache.set('import:info:' + uuid, file);
204202
chrome.tabs.create({
205203
url: 'import.html?uuid=' + uuid,
206204
active: true,
@@ -209,15 +207,7 @@ export class ScriptManager extends Manager {
209207
});
210208
}
211209

212-
public importFile(file: ExportFile): Promise<any> {
213-
return new Promise(resolve => {
214-
215-
216-
resolve(true);
217-
});
218-
}
219-
220-
public requestImportFile(uuid: string): Promise<ExportFile> {
210+
public requestImportFile(uuid: string): Promise<any> {
221211
return new Promise(resolve => {
222212
const file = App.Cache.get('import:info:' + uuid);
223213
resolve(file);

src/import.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import 'reflect-metadata';
22
import 'vuetify/dist/vuetify.min.css';
33
import Vue from 'vue';
4-
import Vuetify from 'vuetify';
5-
4+
import { i18n, vuetify } from '../i18n/i18n';
65
import App from '@App/views/pages/Import/index.vue';
76
import { migrate } from './model/migrate';
87

@@ -18,12 +17,8 @@ InitApp({
1817
Environment: ENV_FRONTEND,
1918
});
2019

21-
Vue.use(Vuetify);
22-
23-
const opts = {};
24-
const vuetifyInstance = new Vuetify(opts);
25-
2620
new Vue({
27-
vuetify: vuetifyInstance,
21+
i18n,
22+
vuetify: vuetify,
2823
render: (h) => h(App),
2924
}).$mount('#app');

src/model/do/backup.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 使用tampermonkey的备份模式
2-
import { Metadata, Script as InstallScript } from './script';
2+
import { Script as InstallScript } from './script';
33
import { SubscribeScript, Subscribe as InstallSubscribe } from './subscribe';
44

55
export interface ExportScript {
@@ -17,18 +17,29 @@ export interface ImportScript {
1717
source: string
1818
enabled: boolean
1919
position: number
20+
options?: ImportScriptOptions
2021
storage?: Storage
2122
requires?: ImportResource[]
2223
requires_css?: ImportResource[]
2324
resources?: ImportResource[]
2425
}
2526

27+
export interface ImportScriptOptions {
28+
name: string
29+
download_url: string
30+
subscribe_url?: string
31+
}
32+
2633
export interface ImportResource {
27-
meta: { name: string, url: string, ts: number, mimetype: string }
34+
meta: ResourceMeta
2835
source: string
2936
base64: string
3037
}
3138

39+
export interface ResourceMeta {
40+
name: string, url: string, ts: number, mimetype: string
41+
}
42+
3243

3344
export interface ExportSubscribe {
3445
name: string
@@ -40,6 +51,12 @@ export interface ImportSubscribe {
4051
source: string
4152
enabled: boolean
4253
scripts: { [key: string]: SubscribeScript };
54+
options: ImportSubscribeOptions
55+
}
56+
57+
export interface ImportSubscribeOptions {
58+
name: string
59+
url: string
4360
}
4461

4562
export interface ExportResource {

src/model/do/resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface Resource {
77
base64: string
88
hash: ResourceHash
99
type?: ResourceType
10-
contentType?: string
10+
contentType: string
1111
createtime?: number
1212
updatetime?: number
1313
}

src/pkg/apps/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MessageCallback, MsgCenter } from '@App/apps/msg-center/msg-center';
33
export class Manager {
44

55
public listenerMessage(topic: string, callback: MessageCallback) {
6-
MsgCenter.listenerMessage(topic, async (body, send, sender) => {
6+
MsgCenter.listenerMessage(topic, (body, send, sender) => {
77
const ret = callback.call(this, body, send, sender)
88
if (ret instanceof Promise) {
99
ret.then((ret) => {

0 commit comments

Comments
 (0)