Skip to content

Commit 66fe49a

Browse files
authored
Manifest update from V2 to V3 in manifest.json (#129)
* chrome.runtime null check * manifest update from V2 to V3 * undefined check * remove unused function parameter in getConfigFromLocalStorage() * undefined window check for localStroage getter/setter. * change localStorage to chrome.storage.local * Change of host_permissions * async handling * keep callback active in chrome.runtime.onMessage * revert host_permissions * Remove chrome and chrome.runtime null check.
1 parent 40b3e30 commit 66fe49a

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

background.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
}
1717

1818
function storeConfigToLocalStorage(data) {
19-
localStorage.setItem(SETUP_STRING, JSON.stringify(data || {}));
19+
chrome.storage.local.set({ [SETUP_STRING]: data || {} });
2020
}
21-
22-
function getConfigFromLocalStorage(data) {
23-
const val = localStorage.getItem(SETUP_STRING);
24-
return JSON.parse(val) || {};
21+
22+
function getConfigFromLocalStorage() {
23+
return new Promise((resolve) => {
24+
chrome.storage.local.get([SETUP_STRING], (result) => {
25+
resolve(result[SETUP_STRING] || {});
26+
});
27+
});
2528
}
2629

2730
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
@@ -32,10 +35,12 @@
3235
sendMsgToAllContainPage('live-server-config-updated', msg.data);
3336
}
3437
else if (msg.req === 'get-live-server-config') {
35-
const data = getConfigFromLocalStorage();
36-
sendResponse(data);
38+
getConfigFromLocalStorage().then(
39+
function (value) { sendResponse(value) },
40+
function (error) { console.error(`Error: ${error}`) }
41+
);
3742
}
38-
43+
return true; //Keep the callback(sendResponse) active
3944
});
4045

4146
})();

manifest.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"manifest_version": 2,
2+
"manifest_version": 3,
33
"name": "Live Server Web Extension",
44
"version": "1.4.0",
55
"description": " Makes your existing server live. This is a browser extension that helps you to live reload feature for dynamic pages",
@@ -22,16 +22,18 @@
2222
}
2323
],
2424
"background": {
25-
"scripts": [
26-
"background.js"
27-
]
25+
"service_worker": "background.js",
26+
"type": "module"
2827
},
29-
"browser_action": {
28+
"action": {
3029
"default_popup": "./popup/popup.html",
3130
"default_icon": "./img/icon.png",
3231
"default_title": "Live Server"
3332
},
3433
"permissions": [
34+
"storage"
35+
],
36+
"host_permissions": [
3537
"http://*/*",
3638
"https://*/*"
3739
]

popup/popup.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; (function () {
2-
2+
33
'use strict';
44

55
const liveReloadCheck = document.getElementById('liveReloadCheck');
@@ -18,10 +18,12 @@
1818
liveServerUrl: liveServerAddress.value || ''
1919
}
2020

21-
chrome.runtime.sendMessage({
22-
req: 'set-live-server-config',
23-
data: formData
24-
});
21+
if (chrome && chrome.runtime) {
22+
chrome.runtime.sendMessage({
23+
req: 'set-live-server-config',
24+
data: formData
25+
});
26+
}
2527
}
2628

2729
liveReloadCheck.onclick = () => {
@@ -39,12 +41,13 @@
3941
chrome.runtime.sendMessage({
4042
req: 'get-live-server-config'
4143
}, (data) => {
42-
console.log('popupwidnow')
43-
liveReloadCheck.checked = data.isEnable || false;
44-
noProxyCheckBox.checked = !data.proxySetup;
45-
actualServerAddress.value = data.actualUrl || '';
46-
liveServerAddress.value = data.liveServerUrl || '';
47-
serverSetupDiv.className = noProxyCheckBox.checked ? 'show' : 'hide';
44+
if (data) {
45+
liveReloadCheck.checked = data.isEnable || false;
46+
noProxyCheckBox.checked = !data.proxySetup;
47+
actualServerAddress.value = data.actualUrl || '';
48+
liveServerAddress.value = data.liveServerUrl || '';
49+
serverSetupDiv.className = noProxyCheckBox.checked ? 'show' : 'hide';
50+
}
4851
});
4952
});
5053

0 commit comments

Comments
 (0)