-
Notifications
You must be signed in to change notification settings - Fork 356
/
background.js
51 lines (43 loc) · 1.52 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
; (function () {
'use strict';
const SETUP_STRING = 'live-reload-extension-new-setup-v2';
function sendMsgToAllContainPage(req, data) {
chrome.tabs.query({}, tabs => {
tabs.forEach(tab => {
chrome.tabs.sendMessage(tab.id, {
req: req,
data: data
});
});
});
}
function storeConfigToLocalStorage(data) {
// return promise
return chrome.storage.local.set({ [SETUP_STRING]: data || {} })
}
function getConfigFromLocalStorage() {
return new Promise((resolve) => {
chrome.storage.local.get([SETUP_STRING], (result) => {
resolve(result[SETUP_STRING] || {});
});
});
}
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (typeof msg !== 'object') return;
if (msg.req === 'set-live-server-config') {
storeConfigToLocalStorage(msg.data);
sendMsgToAllContainPage('live-server-config-updated', msg.data);
}
else if (msg.req === 'get-live-server-config') {
getConfigFromLocalStorage()
.then(function (value) {
sendResponse(value)
})
.catch(function (error) {
console.error("Error in get-live-server-config:",error);
sendResponse({})
});
}
return true; //Keep the callback(sendResponse) active
});
})();