Skip to content

Commit

Permalink
✨ 支持zip格式导入导出
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 8, 2022
1 parent 6cd369e commit 48d197c
Show file tree
Hide file tree
Showing 15 changed files with 612 additions and 255 deletions.
14 changes: 7 additions & 7 deletions i18n/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { zh_CN } from './cn';
import { en } from './en';
import VueI18n from 'vue-i18n';
import Vuetify from "vuetify";
import Vue from "vue";
import Vuetify from 'vuetify';
import Vue from 'vue';
// import '@mdi/font/css/materialdesignicons.css'

Vue.use(VueI18n);
Vue.use(Vuetify);

export const messages = {
"zh_CN": zh_CN,
"en": en,
export const messages: { [key: string]: any } = {
'zh_CN': zh_CN,
'en': en,
};

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

if (!(<any>messages)[defaultLocale]) {
defaultLocale = "en";
if (!messages[defaultLocale]) {
defaultLocale = 'en';
}

export const i18n = new VueI18n({
Expand Down
1 change: 0 additions & 1 deletion src/apps/msg-center/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const SyncTaskEvent = 'sync-task-event';
export const TriggerSync = 'trigger-sync';

export const OpenImportFileWindow = 'open-import-file-window';
export const ImportFile = 'import-file';
export const RequestImportFile = 'request-import-file';

export const ToolsConnectVSCode = 'tools-connect-vscode';
Expand Down
11 changes: 9 additions & 2 deletions src/apps/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export class ResourceManager {
});
}

// 方便识别text文本储存
static textContentTypeMap = new Map<string, boolean>().
set('application/javascript', true).
set('application/json', true);

public loadByUrl(url: string): Promise<Resource | undefined> {
return new Promise(resolve => {
const u = this.parseUrl(url);
Expand All @@ -93,11 +98,13 @@ export class ResourceManager {
const resource: Resource = {
id: 0,
url: u.url, content: '',
contentType: (<string>((<AnyMap>response.headers)['content-type']) || '').split(';')[0],
contentType: (<string>((<AnyMap>response.headers)['content-type']) || 'application/octet-stream').split(';')[0],
hash: await this.calculateHash(<Blob>response.data),
base64: '',
};
resource.content = await (<Blob>response.data).text();
if (resource.contentType.startsWith('text/') || ResourceManager.textContentTypeMap.has(resource.contentType)) {
resource.content = await (<Blob>response.data).text();
}
resource.base64 = await blobToBase64(<Blob>response.data) || '';
App.Log.Info('resource', u.url, 'load');
return resolve(resource);
Expand Down
21 changes: 4 additions & 17 deletions src/apps/script/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
SubscribeUpdate,
Unsubscribe,
SubscribeCheckUpdate,
ImportFile,
OpenImportFileWindow,
RequestImportFile,
ScriptValueChange
Expand All @@ -44,7 +43,6 @@ import { ResourceManager } from '../resource';
import { compileScriptCode } from '@App/pkg/sandbox/compile';
import { SubscribeModel } from '@App/model/subscribe';
import { Subscribe, SUBSCRIBE_STATUS_DISABLE, SUBSCRIBE_STATUS_ENABLE } from '@App/model/do/subscribe';
import { ExportFile } from '@App/model/do/backup';

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

public getImportFile(uuid: string): Promise<ExportFile> {
public getImportFile(uuid: string): Promise<{ name: string, url: string }> {
return new Promise(resolve => {
MsgCenter.sendMessage(RequestImportFile, uuid, resp => {
resolve(<ExportFile>resp);
resolve(<{ name: string, url: string }>resp);
});
});
}
Expand Down Expand Up @@ -565,20 +563,9 @@ export class ScriptController {
})
}

public parseBackFile(str: string): { data?: ExportFile, err?: string } {
const data = <ExportFile>JSON.parse(str);
if (!data.created_by) {
return { err: '错误的格式' };
}
if (!data.scripts) {
return { err: '脚本为空' }
}
return { data: data };
}

public openImportFileWindow(file: ExportFile): Promise<any> {
public openImportFileWindow(name: string, url: string): Promise<any> {
return new Promise(resolve => {
MsgCenter.sendMessage(OpenImportFileWindow, file, (resp) => {
MsgCenter.sendMessage(OpenImportFileWindow, { name, url }, (resp) => {
resolve(resp);
});
});
Expand Down
18 changes: 4 additions & 14 deletions src/apps/script/manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { MsgCenter } from '@App/apps/msg-center/msg-center';
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';
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';
import { dealScript, get, randomString } from '@App/pkg/utils/utils';
import { App } from '../app';
import { UrlMatch } from '@App/pkg/match';
Expand All @@ -19,7 +19,6 @@ import { Subscribe } from '@App/model/do/subscribe';
import { SubscribeModel } from '@App/model/subscribe';
import { SyncModel } from '@App/model/sync';
import { SyncAction, SyncData } from '@App/model/do/sync';
import { ExportFile } from '@App/model/do/backup';
import { v4 as uuidv4 } from 'uuid';
import { Manager } from '@App/pkg/apps/manager';

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

this.listenerMessage(OpenImportFileWindow, this.openImportFileWindow)
this.listenerMessage(ImportFile, this.importFile)
this.listenerMessage(RequestImportFile, this.requestImportFile)

// 监听事件,并转发
Expand Down Expand Up @@ -196,11 +194,11 @@ export class ScriptManager extends Manager {
});
}

public openImportFileWindow(file: ExportFile): Promise<any> {
public openImportFileWindow(file: { name: string, url: string }): Promise<any> {
return new Promise(resolve => {
// 打开导入窗口
const uuid = uuidv4()
App.Cache.set('import:info:' + uuid, file);
void App.Cache.set('import:info:' + uuid, file);
chrome.tabs.create({
url: 'import.html?uuid=' + uuid,
active: true,
Expand All @@ -209,15 +207,7 @@ export class ScriptManager extends Manager {
});
}

public importFile(file: ExportFile): Promise<any> {
return new Promise(resolve => {


resolve(true);
});
}

public requestImportFile(uuid: string): Promise<ExportFile> {
public requestImportFile(uuid: string): Promise<any> {
return new Promise(resolve => {
const file = App.Cache.get('import:info:' + uuid);
resolve(file);
Expand Down
11 changes: 3 additions & 8 deletions src/import.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'reflect-metadata';
import 'vuetify/dist/vuetify.min.css';
import Vue from 'vue';
import Vuetify from 'vuetify';

import { i18n, vuetify } from '../i18n/i18n';
import App from '@App/views/pages/Import/index.vue';
import { migrate } from './model/migrate';

Expand All @@ -18,12 +17,8 @@ InitApp({
Environment: ENV_FRONTEND,
});

Vue.use(Vuetify);

const opts = {};
const vuetifyInstance = new Vuetify(opts);

new Vue({
vuetify: vuetifyInstance,
i18n,
vuetify: vuetify,
render: (h) => h(App),
}).$mount('#app');
21 changes: 19 additions & 2 deletions src/model/do/backup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 使用tampermonkey的备份模式
import { Metadata, Script as InstallScript } from './script';
import { Script as InstallScript } from './script';
import { SubscribeScript, Subscribe as InstallSubscribe } from './subscribe';

export interface ExportScript {
Expand All @@ -17,18 +17,29 @@ export interface ImportScript {
source: string
enabled: boolean
position: number
options?: ImportScriptOptions
storage?: Storage
requires?: ImportResource[]
requires_css?: ImportResource[]
resources?: ImportResource[]
}

export interface ImportScriptOptions {
name: string
download_url: string
subscribe_url?: string
}

export interface ImportResource {
meta: { name: string, url: string, ts: number, mimetype: string }
meta: ResourceMeta
source: string
base64: string
}

export interface ResourceMeta {
name: string, url: string, ts: number, mimetype: string
}


export interface ExportSubscribe {
name: string
Expand All @@ -40,6 +51,12 @@ export interface ImportSubscribe {
source: string
enabled: boolean
scripts: { [key: string]: SubscribeScript };
options: ImportSubscribeOptions
}

export interface ImportSubscribeOptions {
name: string
url: string
}

export interface ExportResource {
Expand Down
2 changes: 1 addition & 1 deletion src/model/do/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface Resource {
base64: string
hash: ResourceHash
type?: ResourceType
contentType?: string
contentType: string
createtime?: number
updatetime?: number
}
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/apps/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MessageCallback, MsgCenter } from '@App/apps/msg-center/msg-center';
export class Manager {

public listenerMessage(topic: string, callback: MessageCallback) {
MsgCenter.listenerMessage(topic, async (body, send, sender) => {
MsgCenter.listenerMessage(topic, (body, send, sender) => {
const ret = callback.call(this, body, send, sender)
if (ret instanceof Promise) {
ret.then((ret) => {
Expand Down
Loading

0 comments on commit 48d197c

Please sign in to comment.