Skip to content

Commit

Permalink
feat: 外部api用于脚本市场获取脚本状态
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Oct 22, 2021
1 parent 2c1fd8a commit 8ce9ae9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/apps/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const ExtVersion = "0.7.0";

export const Server = process.env.NODE_ENV == "production" ? "https://sc.icodef.com/" : "http://localhost:8080/";

export const ExternalWhitelist = [
'greasyfork.org',
'scriptcat.org',
'openuserjs.org',
];
4 changes: 2 additions & 2 deletions src/apps/grant/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class FrontendGrant implements ScriptContext {
this.script = script;
this.browserMsg = browserMsg!;
if (browserMsg) {
this.licenseMsg();
this.listenMsg();
}
// 处理GM_cookie.list等操作
let action = (action: string) => {
Expand Down Expand Up @@ -114,7 +114,7 @@ export class FrontendGrant implements ScriptContext {
this.browserMsg.send("grant", grant);
}

public licenseMsg = () => {
public listenMsg = () => {
this.browserMsg.listen(this.script.flag!, (grant: Grant) => {
let callback = this.request.get(grant.request);
if (callback) {
Expand Down
2 changes: 2 additions & 0 deletions src/apps/msg-center/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const RequestImportFile: string = "request-import-file";
export const ToolsConnectVSCode = "tools-connect-vscode";
export const ToolsDisconnecttVSCode = "tools-disconnect-vscode";

export const ExternalMessage = 'external.message';

// 单页面内的消息
export class AppEvent {
public static eventMap = new Map<string, Map<any, any>>();
Expand Down
26 changes: 25 additions & 1 deletion src/apps/tools/manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Manager } from "@App/pkg/apps/manager";
import { ToolsConnectVSCode, ToolsDisconnecttVSCode } from "../msg-center/event";
import { ExternalWhitelist } from "../config";
import { ExternalMessage, ToolsConnectVSCode, ToolsDisconnecttVSCode } from "../msg-center/event";
import { ScriptController } from "../script/controller";
import { ScriptManager } from "../script/manager";

Expand All @@ -18,6 +19,29 @@ export class ToolsManager extends Manager {
public listenEvent() {
this.listenerMessage(ToolsConnectVSCode, this.connectVSCode);
this.listenerMessage(ToolsDisconnecttVSCode, this.connectVSCode);

this.listenerMessage(ExternalMessage, this.externalMessage);
}

public externalMessage(body: any, sendResponse: (response?: any) => void, sender?: chrome.runtime.MessageSender) {
return new Promise(async resolve => {
// 对外接口白名单
let u = new URL(sender?.url!);
for (let i = 0; i < ExternalWhitelist.length; i++) {
if (u.host.endsWith(ExternalWhitelist[i])) {
switch (body.action) {
case "isInstalled":
let script = await this.scriptController.scriptModel.findByNameAndNamespace(body.params.name, body.params.namespace);
if (script) {
resolve({ action: 'isInstalled', data: { installed: true, version: script.metadata['version'] && script.metadata['version'][0] } });
} else {
resolve({ action: 'isInstalled', data: { installed: false } });
}
}
return;
}
}
});
}

public connectVSCode(url: string) {
Expand Down
9 changes: 8 additions & 1 deletion src/content.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ExternalWhitelist } from "./apps/config";
import { Grant } from "./apps/grant/interface";
import { BrowserMsg } from "./apps/msg-center/browser";
import { ScriptExec, ScriptGrant, ScriptValueChange } from "./apps/msg-center/event";
import { ExternalMessage, ScriptExec, ScriptGrant, ScriptValueChange } from "./apps/msg-center/event";
import { MsgCenter } from "./apps/msg-center/msg-center";
import { ScriptCache } from "./model/do/script";

Expand All @@ -18,6 +19,7 @@ chrome.runtime.sendMessage("runScript", (event: any) => {
browserMsg.send(msg.flag!, msg);
break;
default:
// NOTE: 好像没处理释放问题
MsgCenter.connect(ScriptGrant, msg).addListener((msg: Grant, port: chrome.runtime.Port) => {
browserMsg.send(msg.flag!, msg);
});
Expand All @@ -26,6 +28,11 @@ chrome.runtime.sendMessage("runScript", (event: any) => {
MsgCenter.connect(ScriptValueChange, 'init').addListener((msg: any) => {
browserMsg.send(ScriptValueChange, msg);
});
browserMsg.listen(ExternalMessage, msg => {
MsgCenter.connect(ExternalMessage, msg).addListener((msg, port) => {
browserMsg.send(ExternalMessage, msg);
});
});
chrome.runtime.onMessage.addListener((event) => {
switch (event.action) {
case ScriptExec:
Expand Down
31 changes: 29 additions & 2 deletions src/injected.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// splitChunks对injected可能会有问题

import { ExternalWhitelist } from "./apps/config";
import { FrontendGrant, ScriptContext } from "./apps/grant/frontend";
import { BrowserMsg } from "./apps/msg-center/browser";
import { ScriptExec, ScriptValueChange } from "./apps/msg-center/event";
import { ExternalMessage, ScriptExec, ScriptValueChange } from "./apps/msg-center/event";
import { ScriptCache } from "./model/do/script";
import { Value } from "./model/do/value";
import { addStyle } from "./pkg/frontend";
Expand Down Expand Up @@ -98,4 +99,30 @@ browserMsg.listen("scripts", (msg) => {
});
});

});
});


// 对外接口白名单
for (let i = 0; i < ExternalWhitelist.length; i++) {
if (window.location.host.endsWith(ExternalWhitelist[i])) {
// 注入
let isInstalledCallback: any;
(<any>window).external = window.external || {};
browserMsg.listen(ExternalMessage, (msg) => {
switch (msg.action) {
case "isInstalled":
isInstalledCallback(msg.data);
break;
}
});
(<any>window.external).Scriptcat = {
isInstalled(name: string, namespace: string, callback: any) {
isInstalledCallback = callback;
browserMsg.send(ExternalMessage, { 'action': 'isInstalled', 'params': { name, namespace } });
}
};
(<any>window.external).Tampermonkey = (<any>window.external).Scriptcat;
break;
}

}

0 comments on commit 8ce9ae9

Please sign in to comment.