-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
61 lines (55 loc) · 1.48 KB
/
background.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
const DEFAULT_DELAY = 100;
const MAX_DELAY = 5000;
const URI = 'ws://localhost:35729/livereload';
let delay = DEFAULT_DELAY;
let timer = null;
let socket = null;
const connect = () => setTimeout(establishСonnection, delay);
const establishСonnection = () => {
socket = new WebSocket(URI);
socket.addEventListener('message', onMessage, false);
socket.addEventListener('open', onOpen, false);
socket.addEventListener('close', onClose, false);
socket.addEventListener('error', onError, false);
};
const onMessage = ({ data }) => {
try {
const { command } = JSON.parse(data);
if ('reload' === command) {
realod();
}
} catch (err) {
onError(err);
}
};
const onOpen = () => {
clearTimeout(timer);
delay = DEFAULT_DELAY;
};
const onClose = () => {
socket = null;
connect();
};
const onError = () => {
delay = Math.min(MAX_DELAY, delay * 2);
socket.close();
};
const realod = () => {
chrome.management.getAll(extensions =>
extensions
.filter(ext => ext.installType === chrome.management.ExtensionInstallType.DEVELOPMENT)
.filter(ext => ext.enabled === true)
.filter(ext => ext.id !== chrome.runtime.id)
.map(ext =>
chrome.management.setEnabled(ext.id, false, () =>
chrome.management.setEnabled(
ext.id,
true,
() =>
ext.type === chrome.management.ExtensionType.PACKAGED_APP && chrome.management.launchApp(ext.id)
)
)
)
);
};
timer = connect();