-
Notifications
You must be signed in to change notification settings - Fork 355
/
reload.js
70 lines (57 loc) · 2.09 KB
/
reload.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
62
63
64
65
66
67
68
69
70
; (function () {
'use strict';
const protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://';
let address = protocol + window.location.host + window.location.pathname + '/ws';
let socket;
let isActive = false;
function init(data) {
if (!data.proxySetup) {
//Correction
if (data.liveServerUrl.indexOf('http') !== 0)
data.liveServerUrl = 'http' + data.liveServerUrl;
if (data.actualUrl.indexOf('http') !== 0)
data.actualUrl = 'http' + data.actualUrl;
if (!data.actualUrl.endsWith('/'))
data.actualUrl = data.actualUrl + '/';
address = data.liveServerUrl.replace('http', 'ws') + '/ws';
}
socket = new WebSocket(address);
socket.onmessage = (msg) => {
reloadWindow(msg, data)
};
}
function reloadWindow(msg, data) {
if (!isActive) return;
const currentUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
if (msg.data == 'reload' || msg.data == 'refreshcss') {
if (data.proxySetup === true || (data.proxySetup === false && currentUrl.startsWith(data.actualUrl))) {
window.location.reload();
}
}
logMsgForASingleTime();
};
function logMsgForASingleTime() {
const key = 'oneTimeLog-live-server-web-extesion';
if (!sessionStorage.getItem(key)) {
console.log("Live reload Actived - Live Server Web Extension");
sessionStorage.setItem(key, 1);
}
}
chrome.runtime.onMessage.addListener((msg) => {
if (typeof msg !== 'object') return;
if (msg.req === 'live-server-config-updated') {
isActive = msg.data.isEnable;
if (isActive && !socket) {
init(msg.data);
}
}
});
chrome.runtime.sendMessage({
req: 'get-live-server-config'
}, (data) => {
isActive = data.isEnable;
if (isActive && !socket) {
init(data);
}
});
})();