Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/examples/plugin-echo-classes.ts
1028 views
1
// eslint-disable-next-line max-classes-per-file
2
import { ISendToCoreFn } from '@secret-agent/interfaces/IClientPlugin';
3
import { ClientPlugin, CorePlugin } from '@secret-agent/plugin-utils';
4
import { IOnClientCommandMeta } from '@secret-agent/interfaces/ICorePlugin';
5
import type { Agent, Tab } from 'secret-agent';
6
7
export class EchoClientPlugin extends ClientPlugin {
8
static readonly id = 'echo-plugin';
9
static coreDependencyIds = [EchoClientPlugin.id];
10
11
public onAgent(agent: Agent, sendToCore: ISendToCoreFn): void {
12
agent.echo = (echo1: string, echo2: number, ...echoAny: any[]) => {
13
return this.echo(sendToCore, echo1, echo2, ...echoAny);
14
};
15
}
16
17
public onTab(agent: Agent, tab: Tab, sendToCore: ISendToCoreFn): void {
18
tab.echo = (echo1: string, echo2: number, ...echoAny: any[]) => {
19
return this.echo(sendToCore, echo1, echo2, ...echoAny);
20
};
21
}
22
23
private async echo(
24
sendToCore: ISendToCoreFn,
25
echo1: string,
26
echo2: number,
27
...echoAny: any[]
28
): Promise<[string, number, ...any[]]> {
29
return await sendToCore(EchoClientPlugin.id, echo1, echo2, ...echoAny);
30
}
31
}
32
33
export class EchoCorePlugin extends CorePlugin {
34
static readonly id = 'echo-plugin';
35
36
public onClientCommand(
37
{ puppetPage }: IOnClientCommandMeta,
38
echo1: string,
39
echo2: number,
40
...echoAny: any[]
41
): Promise<any> {
42
return Promise.resolve([echo1, echo2, ...echoAny]);
43
}
44
}
45
46
type ExecuteJsPluginAdditions = {
47
echo(echo1: string, echo2: number, ...echoAny: any[]): Promise<[string, number, ...any[]]>;
48
};
49
50
declare module '@secret-agent/client/lib/extendables' {
51
interface Agent extends ExecuteJsPluginAdditions {}
52
interface Tab extends ExecuteJsPluginAdditions {}
53
}
54
55