-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (153 loc) · 5.75 KB
/
index.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 生成随机 hash 串
const hash = (len = 16) => {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const maxPos = chars.length
let res = ''
for (let i = 0; i < len; i++) {
res += chars[Math.floor(Math.random() * maxPos)]
}
return res
}
// background 连接
export class Connect {
constructor (name) {
// 连接名称
this.name = name
// 所有连接
this.ports = []
// 中间件
this.middleWares = new Map()
// 处理器
this.handlers = new Map()
// debug
this.debug = msg => {}
// 连接事件
chrome.runtime.onConnect.addListener(port => {
if (port.name === this.name) {
// 保存连接
this.ports.push(port)
// 监听消息
port.onMessage.addListener(msg => {
// 获取处理指令的处理器
const handler = this.handlers.get(msg.command)
if (!handler) throw new Error(`未找到指令 ${msg.command} 的处理器`)
// 处理消息 (前两个是常用参数,后面两个完整参数放着备用)
handler(msg.payload, res => {
const isError = res instanceof Error
port.postMessage({
token: msg.token,
payload: isError
? {
status: false,
message: res.message
}
: {
status: true,
data: res
}
})
}, msg, port)
})
// 监听断开连接
port.onDisconnect.addListener(() => {
// 移除连接
this.ports = this.ports.filter(p => p !== port)
})
}
})
}
// debugger
debugger (fn = msg => {}) {
this.debug = fn
}
// 注册中间件
use (name, middleware) {
this.middleWares.set(name, middleware)
}
// 注册处理器
on (command = '', handler = (payload, callback, msg, port) => {}) {
if (!command) throw new Error('指令不能为空')
// this.handlers.set(command, handler)
this.handlers.set(command, async (payload, callback, msg, port) => {
for (const [name, middleware] of this.middleWares) {
const ret = await middleware(command, payload, msg, port)
if (ret instanceof Error) {
this.debug(`中间件 ${name} 执行失败:${ret.message}`)
return callback(ret) // 如果中间件返回错误,则中断执行,返回错误
}
}
// 执行处理器
handler(payload, ret => {
if (ret instanceof Error) this.debug(`指令 ${command} 执行失败:${ret.message}`)
callback(ret)
}, msg, port)
})
}
// 主动触发处理器
emit (command = '', payload) {
return new Promise((resolve, reject) => {
if (!command) return reject(new Error('指令不能为空'))
// 获取处理指令的处理器
const handler = this.handlers.get(command)
if (!handler) return reject(new Error(`未找到指令 ${command} 的处理器`))
// 处理消息
handler(payload, resolve)
})
}
// 主动发送消息 (向所有页面发送,主要用于广播消息、同步数据等)
broadcast (command = '', payload = {}) {
if (!command) throw new Error('指令不能为空')
this.ports.forEach(port => {
port.postMessage({ command, payload })
})
}
}
// content 连接
export class Port {
constructor (name) {
// 连接名称
this.name = name
// 异步响应
this.promises = new Map()
// 广播处理器
this.handlers = new Map()
// 连接事件
this.port = chrome.runtime.connect({
name: this.name
})
// 监听消息
this.port.onMessage.addListener(msg => {
if (msg.command) {
// 主动通信
const handler = this.handlers.get(msg.command)
if (!handler) throw new Error(`未找到指令 ${msg.command} 的处理器`)
handler(msg.payload)
} else {
// 被动通信
const { token, payload } = msg
const { resolve, reject } = this.promises.get(token)
if (payload.status) {
resolve(payload.data)
} else {
reject(new Error(payload.message))
}
// 移除
this.promises.delete(token)
}
})
}
// 监听广播消息
on (command = '', handler = (payload) => {}) {
if (!command) throw new Error('指令不能为空')
this.handlers.set(command, handler)
}
// 发送消息
send (command = '', payload) {
return new Promise((resolve, reject) => {
if (!command) return reject(new Error('指令不能为空'))
const token = hash()
this.promises.set(token, { resolve, reject })
this.port.postMessage({ command, payload, token })
})
}
}