Skip to content

Commit

Permalink
feat: 通过链接安装
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Oct 25, 2021
1 parent 2c1fd8a commit 00e4622
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/apps/msg-center/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const PermissionConfirm: string = "permission-confirm";

export const ScriptExec: string = "script-exec";
export const ScriptStop: string = "script-stop";
export const ScriptInstallByURL: string = "script-install-by-url";
export const ScriptInstall: string = "script-install";
export const ScriptReinstall: string = "script-reinstall";
export const ScriptUninstall: string = "script-uninstall";
Expand Down
51 changes: 24 additions & 27 deletions src/apps/script/manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { MessageCallback, 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 } from "@App/apps/msg-center/event";
import { dealScript, get, Page, randomString } from "@App/pkg/utils";
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 { dealScript, get, randomString } from "@App/pkg/utils";
import { App } from "../app";
import { UrlMatch } from "@App/pkg/match";
import { ValueModel } from "@App/model/value";
Expand All @@ -21,9 +21,10 @@ import { SyncModel } from "@App/model/sync";
import { SyncAction, SyncData } from "@App/model/do/sync";
import { File } from "@App/model/do/back";
import { v4 as uuidv4 } from "uuid";
import { Manager } from "@App/pkg/apps/manager";

// 脚本管理器,收到控制器消息进行实际的操作
export class ScriptManager {
export class ScriptManager extends Manager {

protected scriptModel = new ScriptModel();
protected subscribeModel = new SubscribeModel();
Expand Down Expand Up @@ -86,6 +87,7 @@ export class ScriptManager {
public listen() {

// 消息监听处理
this.listenerMessage(ScriptInstallByURL, this.installScriptByURL);
this.listenerMessage(ScriptInstall, this.scriptInstall)
this.listenerMessage(ScriptReinstall, this.scriptReinstall)
this.listenerMessage(ScriptUninstall, (body) => { return this.scriptUninstall(body, false) })
Expand Down Expand Up @@ -124,7 +126,11 @@ export class ScriptManager {
if (hash.indexOf("bypass=true") != -1) {
return;
}
this.installScript(req.tabId, req.url);
this.installScriptByURL(req.url).catch(() => {
chrome.tabs.update(req.tabId, {
url: req.url + "#bypass=true",
});
});
return { redirectUrl: "javascript:void 0" };
},
{
Expand All @@ -138,18 +144,19 @@ export class ScriptManager {
);
}

public async installScript(tabid: number, url: string) {
let info = await loadScriptByUrl(url);
if (info) {
App.Cache.set("install:info:" + info.uuid, info);
chrome.tabs.create({
url: "install.html?uuid=" + info.uuid,
});
} else {
chrome.tabs.update(tabid, {
url: url + "#bypass=true",
});
}
public async installScriptByURL(url: string) {
return new Promise(async (resolve, reject) => {
let info = await loadScriptByUrl(url);
if (info) {
App.Cache.set("install:info:" + info.uuid, info);
chrome.tabs.create({
url: "install.html?uuid=" + info.uuid,
});
resolve(true);
} else {
reject(false);
}
});
}

// 监听来自AppEvent的事件和连接来自其它地方的长链接,转发AppEvent的事件
Expand All @@ -176,16 +183,6 @@ export class ScriptManager {
})
}

public listenerMessage(topic: string, callback: MessageCallback) {
MsgCenter.listenerMessage(topic, async (body, send, sender) => {
let ret = <any>callback.call(this, body, send, sender)
if (ret instanceof Promise) {
ret = await ret;
}
send(ret);
});
}

public requestConfirmInfo(uuid: string): Promise<ConfirmParam> {
return new Promise(resolve => {
let info = App.Cache.get("confirm:info:" + uuid);
Expand Down
9 changes: 7 additions & 2 deletions src/pkg/apps/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ export class Manager {
MsgCenter.listenerMessage(topic, async (body, send, sender) => {
let ret = <any>callback.call(this, body, send, sender)
if (ret instanceof Promise) {
ret = await ret;
ret.then((ret) => {
send(ret);
}).catch(ret => {
send(ret);
})
} else {
send(ret);
}
send(ret);
});
}

Expand Down
17 changes: 13 additions & 4 deletions src/views/pages/Option/tabs/ScriptList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@
<span>定时脚本</span>
</v-tooltip>

<!-- <v-tooltip left>
<v-tooltip left>
<template v-slot:activator="{ on, attrs }">
<v-btn
v-bind="attrs"
Expand All @@ -571,13 +571,13 @@
dark
small
color="#1296db"
@click="newScript('background')"
@click="linkInstall()"
>
<v-icon>mdi-open-in-new</v-icon>
<v-icon>mdi-link</v-icon>
</v-btn>
</template>
<span>链接导入</span>
</v-tooltip> -->
</v-tooltip>
</v-speed-dial>
</div>
</template>
Expand All @@ -595,6 +595,7 @@ import {
import { MsgCenter } from "@App/apps/msg-center/msg-center";
import {
ListenGmLog,
ScriptInstallByURL,
ScriptRunStatusChange,
SyncTaskEvent,
} from "@App/apps/msg-center/event";
Expand Down Expand Up @@ -974,6 +975,14 @@ export default class ScriptList extends Vue {
document.execCommand("copy", false, <any>null);
scriptModule.showSnackbar("复制成功");
}
linkInstall() {
let url = prompt("请填写脚本url", "");
if (!url) {
return;
}
MsgCenter.sendMessage(ScriptInstallByURL, url);
}
}
</script>

Expand Down

0 comments on commit 00e4622

Please sign in to comment.