-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathchromereload.js
63 lines (54 loc) · 1.7 KB
/
chromereload.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
'use strict';
// Reload client for Chrome Apps & Extensions.
// The reload client has a compatibility with livereload.
// WARNING: only supports reload command.
<% if (babel) { %>
const LIVERELOAD_HOST = 'localhost:';
const LIVERELOAD_PORT = 35729;
const connection = new WebSocket('ws://' + LIVERELOAD_HOST + LIVERELOAD_PORT + '/livereload');
var lastReload = false;
chrome.runtime.onInstalled.addListener(function(details) {
lastReload = Date.now();
});
connection.onerror = error => {
console.log('reload connection got error:', error);
};
connection.onmessage = e => {
if (e.data) {
const data = JSON.parse(e.data);
if (data && data.command === 'reload') {
var currentTime = Date.now();
if (lastReload && currentTime - lastReload > 60000) {
// don't reload more than once a minute
chrome.runtime.reload();
chrome.developerPrivate.reload(chrome.runtime.id,
{failQuietly: true});
}
}
}
};
<% } else { %>
var LIVERELOAD_HOST = 'localhost:';
var LIVERELOAD_PORT = 35729;
var connection = new WebSocket('ws://' + LIVERELOAD_HOST + LIVERELOAD_PORT + '/livereload');
var lastReload = false;
chrome.runtime.onInstalled.addListener(function(details) {
lastReload = Date.now();
});
connection.onerror = function (error) {
console.log('reload connection got error:', error);
};
connection.onmessage = function (e) {
if (e.data) {
var data = JSON.parse(e.data);
if (data && data.command === 'reload') {
if (lastReload && currentTime - lastReload > 60000) {
// don't reload more than once a minute
chrome.runtime.reload();
chrome.developerPrivate.reload(chrome.runtime.id,
{failQuietly: true});
}
}
}
};
<% } %>