-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpage.js
131 lines (119 loc) · 3.4 KB
/
page.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict'
let running = false
require('debug').save('zeronet*,libp2p*')
process.env.INTENSE_DEBUG = '1'
process.env.DEBUG_PACKETS = '1'
window.debug = require('debug')
const ZeroNet = require('./zeronet')
window.onerror = function (messageOrEvent, source, lineno, colno, error) {
console.error('%c' + (messageOrEvent == 'Script Error.' ? messageOrEvent : error.stack), 'color: red')
}
const $ = window.$ = require('jquery')
const moment = require('moment')
const Common = require('./common')
const COLSTART = 'ȵ'
const COLEND = 'ȶ'
const COLRESET = 'ȷ'
const MAX_KEEP = 500
let c_hist = []
function consoleParse (t) {
let rr = t.slice(1)
const n = t[0].replace(/%[a-z]/g, function (str) {
const r = rr.shift()
if (typeof r == null) {
return r
}
if (str.toLowerCase() == '%c') {
return COLSTART + r + COLEND
}
return r
})
return [n].concat(rr)
}
function cssProcess (rules) {
return rules.split(';').map(r => r.split(':').map(r => r.trim())).filter(v => v.length == 2)
}
$(document).ready(() => (function () {
function addToLog () {
const d = moment(new Date()).format('HH:mm:ss.SSS[Z]')
let t = [...arguments]
if (typeof t[0] === 'string') {
t.unshift('[' + d + '] ' + t.shift())
t = consoleParse(t)
} else {
t.unshift('[' + d + ']')
}
let css = ''
let iscol = false
let lc = ''
let cc = ''
t = t.join(COLRESET + ' ').split('\n')
t.forEach(t => {
const d = $('<p></p>')
let p = false
t.split('').forEach(c => {
lc = cc
cc = c
if (c == COLSTART) {
return (iscol = true)
} else if (c == COLEND) {
return (iscol = false)
} else if (c == COLRESET && lc != COLEND) {
return (css = '')
}
if (iscol) return css += c
const e = $('<span></span>')
if (c != ' ') p = true
if (!p) e.css('margin-left', '9px')
cssProcess(css).forEach(r => e.css(r[0], r[1]))
if (c == ' ') p = false
e.text(c)
d.append(e)
})
c_hist.push(d)
$('#logfield').append(d)
while (c_hist.length >= MAX_KEEP) [c_hist.shift()].forEach(r => $(r).remove())
})
}
['log', 'error', 'warn', 'info'].forEach(d => {
const o = console[d].bind(console)
console[d] = function console () {
addToLog.apply(window, arguments)
o.apply(console, arguments)
}
})
console.info('%c[node]%c Preparing to launch...', 'font-weight: bold', 'color: inherit')
$('#node-state').text('Preparing...')
ZeroNet({
debug: true,
common: new Common({
debug: true
}),
swarm: {
libp2p: {
wstar: require('zeronet/src/peers').wstar
}
}
}, (err, node) => {
if (err) throw err
console.info('%c[node]%c Ready to launch', 'font-weight: bold', 'color: inherit')
window.node = node
$('#node-state').text('Node: Offline (Click to launch)')
$('#node-state').click(() => {
if (running) return
running = true
$('#node-state').text('Node: Starting...')
node.start(err => {
if (err) {
$('#node-state').text('Node: Error')
throw err
} else {
$('#node-state').text('Node: Online')
require('./controls')($, node)
$('#controls').fadeIn('fast')
console.info('%c[node]%c Online', 'font-weight: bold', 'color: inherit')
}
})
})
})
}()))