forked from ritwickdey/live-server-web-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
41 lines (32 loc) · 1.11 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
; (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) {
localStorage.setItem(SETUP_STRING, JSON.stringify(data || {}));
}
function getConfigFromLocalStorage(data) {
const val = localStorage.getItem(SETUP_STRING);
return JSON.parse(val) || {};
}
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') {
const data = getConfigFromLocalStorage();
sendResponse(data);
}
});
})();