-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExtensionRelay.js
35 lines (29 loc) · 931 Bytes
/
ExtensionRelay.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
export default class ExtensionRelay {
constructor () {
this.tabs = new Set()
this.tabsIssuesMapping = {}
}
init () {
chrome.runtime.onMessage.addListener(this.handleMessage.bind(this))
}
handleMessage (action, sender) {
console.log('Received action:', action, this.tabs, this.tabsIssuesMapping)
if (action.type === 'register') {
this.tabs.add(sender.tab.id)
this.tabsIssuesMapping[sender.tab.id] = action.issueID
}
if (action.type === 'steps-receive') {
this.sendToIssueID(action.issueID, { type: 'steps-reply', ...action.payload })
}
if (action.type === 'cursor-receive') {
this.sendToIssueID(action.issueID, { type: 'cursor-reply', ...action.payload })
}
}
sendToIssueID (issueID, message) {
this.tabs.forEach((tid) => {
if (this.tabsIssuesMapping[tid] === issueID) {
chrome.tabs.sendMessage(tid, message)
}
})
}
}