-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchromeMessages.js
More file actions
58 lines (48 loc) · 1.18 KB
/
chromeMessages.js
File metadata and controls
58 lines (48 loc) · 1.18 KB
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
"use strict"
const message = (() => {
// Create unique ids for Message intents
let e = 0
const TypeEnum = Object.freeze({
})
const tabs = (() => {
const tabApiExists = () => chrome?.tabs
const baseTabQuery = function(which, settings, callback = C.Noop) {
if (!tabApiExists()) return
chrome.tabs.query(which, tabs => {
for (const tab of tabs) {
chrome.tabs.sendMessage(tab.id, settings, callback)
}
})
}
const active = function(settings, callback = C.Noop) {
const which = {
active : true,
currentWindow : true,
}
baseTabQuery(which, settings, callback)
}
const all = function(settings, callback = C.Noop) {
const which = {}
baseTabQuery(which, settings, callback)
}
const inactive = function(settings, callback = C.Noop) {
const which = {active : false}
baseTabQuery(which, settings, callback)
}
const publicApi = {
active,
all,
inactive,
}
return publicApi
})()
// `callback` should be of form function(req, sender, res)
const addListener = (callback = C.Noop) =>
chrome?.runtime?.onMessage?.addListener(callback)
const publicApi = {
Type : TypeEnum,
addListener,
tabs,
}
return publicApi
})()