-
Notifications
You must be signed in to change notification settings - Fork 115
/
content.ts
150 lines (131 loc) · 3.67 KB
/
content.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { Channel } from "./channel";
import {
ChannelManager,
MessageHander,
MessageManager,
WarpChannelManager,
} from "./message";
// content与页面通讯,使用CustomEvent
export default class MessageContent
extends MessageHander
implements MessageManager
{
static instance: MessageContent;
static getInstance() {
return this.instance;
}
eventId: string;
isContent: boolean;
channelManager: ChannelManager;
relatedTarget: Map<number, Element>;
constructor(eventId: string, isContent: boolean) {
super();
this.eventId = eventId;
this.isContent = isContent;
this.channelManager = new WarpChannelManager((data) => {
this.nativeSend(data);
});
this.relatedTarget = new Map<number, Element>();
document.addEventListener(
(isContent ? "ct" : "fd") + eventId,
(event: unknown) => {
if (event instanceof MutationEvent) {
this.relatedTarget.set(
parseInt(event.prevValue, 10),
<Element>event.relatedNode
);
return;
}
const message = (<
{
detail: {
data: any;
action: string;
stream: string;
error: any;
connect: boolean;
};
}
>event).detail;
this.handler(message, this.channelManager, {
targetTag: "content",
});
}
);
if (!MessageContent.instance) {
MessageContent.instance = this;
}
}
// 组合ChannelManager
getChannel(flag: string): Channel | undefined {
return this.channelManager.getChannel(flag);
}
channel(flag?: string): Channel {
return this.channelManager.channel(flag);
}
disChannel(channel: Channel): void {
return this.channelManager.disChannel(channel);
}
free(): void {
return this.channelManager.free();
}
syncSend(action: string, data: any): Promise<any> {
const channel = this.channelManager.channel();
return channel.syncSend(action, data);
}
// content与inject通讯为阻塞可以实现真同步,使用回调的方式返回参数
sendCallback(action: string, data: any, callback: (resp: any) => void) {
const channel = this.channelManager.channel();
channel.handler = callback;
this.nativeSend({
action,
data,
stream: channel.flag,
channel: false,
});
}
getAndDelRelatedTarget(id: number) {
const target = this.relatedTarget.get(id);
this.relatedTarget.delete(id);
return target;
}
nativeSend(data: any): void {
let detail = data;
if (typeof cloneInto !== "undefined") {
try {
// eslint-disable-next-line no-undef
detail = cloneInto(detail, document.defaultView);
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
}
}
// 特殊处理relatedTarget
if (detail.data && typeof detail.data.relatedTarget === "object") {
// 先将relatedTarget转换成id发送过去
const target = detail.data.relatedTarget;
delete detail.data.relatedTarget;
detail.data.relatedTarget = Math.ceil(Math.random() * 1000000);
// 可以使用此种方式交互element
const ev = document.createEvent("MutationEvent");
ev.initMutationEvent(
(this.isContent ? "fd" : "ct") + this.eventId,
false,
false,
target,
detail.data.relatedTarget.toString()
);
document.dispatchEvent(ev);
}
const ev = new CustomEvent((this.isContent ? "fd" : "ct") + this.eventId, {
detail,
});
document.dispatchEvent(ev);
}
public send(action: string, data: any) {
this.nativeSend({
action,
data,
});
}
}