Skip to content

Commit 8ce9ae9

Browse files
committed
feat: 外部api用于脚本市场获取脚本状态
1 parent 2c1fd8a commit 8ce9ae9

6 files changed

Lines changed: 72 additions & 6 deletions

File tree

src/apps/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export const ExtVersion = "0.7.0";
22

33
export const Server = process.env.NODE_ENV == "production" ? "https://sc.icodef.com/" : "http://localhost:8080/";
4+
5+
export const ExternalWhitelist = [
6+
'greasyfork.org',
7+
'scriptcat.org',
8+
'openuserjs.org',
9+
];

src/apps/grant/frontend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class FrontendGrant implements ScriptContext {
4040
this.script = script;
4141
this.browserMsg = browserMsg!;
4242
if (browserMsg) {
43-
this.licenseMsg();
43+
this.listenMsg();
4444
}
4545
// 处理GM_cookie.list等操作
4646
let action = (action: string) => {
@@ -114,7 +114,7 @@ export class FrontendGrant implements ScriptContext {
114114
this.browserMsg.send("grant", grant);
115115
}
116116

117-
public licenseMsg = () => {
117+
public listenMsg = () => {
118118
this.browserMsg.listen(this.script.flag!, (grant: Grant) => {
119119
let callback = this.request.get(grant.request);
120120
if (callback) {

src/apps/msg-center/event.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export const RequestImportFile: string = "request-import-file";
4545
export const ToolsConnectVSCode = "tools-connect-vscode";
4646
export const ToolsDisconnecttVSCode = "tools-disconnect-vscode";
4747

48+
export const ExternalMessage = 'external.message';
49+
4850
// 单页面内的消息
4951
export class AppEvent {
5052
public static eventMap = new Map<string, Map<any, any>>();

src/apps/tools/manager.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Manager } from "@App/pkg/apps/manager";
2-
import { ToolsConnectVSCode, ToolsDisconnecttVSCode } from "../msg-center/event";
2+
import { ExternalWhitelist } from "../config";
3+
import { ExternalMessage, ToolsConnectVSCode, ToolsDisconnecttVSCode } from "../msg-center/event";
34
import { ScriptController } from "../script/controller";
45
import { ScriptManager } from "../script/manager";
56

@@ -18,6 +19,29 @@ export class ToolsManager extends Manager {
1819
public listenEvent() {
1920
this.listenerMessage(ToolsConnectVSCode, this.connectVSCode);
2021
this.listenerMessage(ToolsDisconnecttVSCode, this.connectVSCode);
22+
23+
this.listenerMessage(ExternalMessage, this.externalMessage);
24+
}
25+
26+
public externalMessage(body: any, sendResponse: (response?: any) => void, sender?: chrome.runtime.MessageSender) {
27+
return new Promise(async resolve => {
28+
// 对外接口白名单
29+
let u = new URL(sender?.url!);
30+
for (let i = 0; i < ExternalWhitelist.length; i++) {
31+
if (u.host.endsWith(ExternalWhitelist[i])) {
32+
switch (body.action) {
33+
case "isInstalled":
34+
let script = await this.scriptController.scriptModel.findByNameAndNamespace(body.params.name, body.params.namespace);
35+
if (script) {
36+
resolve({ action: 'isInstalled', data: { installed: true, version: script.metadata['version'] && script.metadata['version'][0] } });
37+
} else {
38+
resolve({ action: 'isInstalled', data: { installed: false } });
39+
}
40+
}
41+
return;
42+
}
43+
}
44+
});
2145
}
2246

2347
public connectVSCode(url: string) {

src/content.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { ExternalWhitelist } from "./apps/config";
12
import { Grant } from "./apps/grant/interface";
23
import { BrowserMsg } from "./apps/msg-center/browser";
3-
import { ScriptExec, ScriptGrant, ScriptValueChange } from "./apps/msg-center/event";
4+
import { ExternalMessage, ScriptExec, ScriptGrant, ScriptValueChange } from "./apps/msg-center/event";
45
import { MsgCenter } from "./apps/msg-center/msg-center";
56
import { ScriptCache } from "./model/do/script";
67

@@ -18,6 +19,7 @@ chrome.runtime.sendMessage("runScript", (event: any) => {
1819
browserMsg.send(msg.flag!, msg);
1920
break;
2021
default:
22+
// NOTE: 好像没处理释放问题
2123
MsgCenter.connect(ScriptGrant, msg).addListener((msg: Grant, port: chrome.runtime.Port) => {
2224
browserMsg.send(msg.flag!, msg);
2325
});
@@ -26,6 +28,11 @@ chrome.runtime.sendMessage("runScript", (event: any) => {
2628
MsgCenter.connect(ScriptValueChange, 'init').addListener((msg: any) => {
2729
browserMsg.send(ScriptValueChange, msg);
2830
});
31+
browserMsg.listen(ExternalMessage, msg => {
32+
MsgCenter.connect(ExternalMessage, msg).addListener((msg, port) => {
33+
browserMsg.send(ExternalMessage, msg);
34+
});
35+
});
2936
chrome.runtime.onMessage.addListener((event) => {
3037
switch (event.action) {
3138
case ScriptExec:

src/injected.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// splitChunks对injected可能会有问题
22

3+
import { ExternalWhitelist } from "./apps/config";
34
import { FrontendGrant, ScriptContext } from "./apps/grant/frontend";
45
import { BrowserMsg } from "./apps/msg-center/browser";
5-
import { ScriptExec, ScriptValueChange } from "./apps/msg-center/event";
6+
import { ExternalMessage, ScriptExec, ScriptValueChange } from "./apps/msg-center/event";
67
import { ScriptCache } from "./model/do/script";
78
import { Value } from "./model/do/value";
89
import { addStyle } from "./pkg/frontend";
@@ -98,4 +99,30 @@ browserMsg.listen("scripts", (msg) => {
9899
});
99100
});
100101

101-
});
102+
});
103+
104+
105+
// 对外接口白名单
106+
for (let i = 0; i < ExternalWhitelist.length; i++) {
107+
if (window.location.host.endsWith(ExternalWhitelist[i])) {
108+
// 注入
109+
let isInstalledCallback: any;
110+
(<any>window).external = window.external || {};
111+
browserMsg.listen(ExternalMessage, (msg) => {
112+
switch (msg.action) {
113+
case "isInstalled":
114+
isInstalledCallback(msg.data);
115+
break;
116+
}
117+
});
118+
(<any>window.external).Scriptcat = {
119+
isInstalled(name: string, namespace: string, callback: any) {
120+
isInstalledCallback = callback;
121+
browserMsg.send(ExternalMessage, { 'action': 'isInstalled', 'params': { name, namespace } });
122+
}
123+
};
124+
(<any>window.external).Tampermonkey = (<any>window.external).Scriptcat;
125+
break;
126+
}
127+
128+
}

0 commit comments

Comments
 (0)