forked from interplanaria/bottle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
379 lines (366 loc) · 12 KB
/
index.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!DOCTYPE html>
<html>
<head>
<link href="./style.css" rel='stylesheet'>
</head>
<body>
<div id="nav-body-ctrls"></div>
<div id="nav-body-tabs"></div>
<div id="nav-body-views"></div>
<script>
var current_url
const fs = require('fs')
const path = require('path')
const {ipcRenderer, webFrame, remote} = require('electron')
const dirname = remote.app.getAppPath()
const userPath = remote.app.getPath("userData")
const navDefaultURL = "about:blank";
const menu = require('./menu')
const utoa = function (str) { return window.btoa(unescape(encodeURIComponent(str))) }
const atou = function (str) { return decodeURIComponent(escape(window.atob(str))) }
const fromKeyCode = function(n) {
if (47 <= n && n <= 90) return unescape('%' + (n).toString(16))
if (96 <= n && n <= 105) return 'NUM ' + (n - 96)
if (112 <= n && n <= 135) return 'F' + (n - 111)
if (n == 3) return 'Cancel' //DOM_VK_CANCEL
if (n == 6) return 'Help' //DOM_VK_HELP
if (n == 8) return 'Backspace'
if (n == 9) return 'Tab'
if (n == 12) return 'NUM 5' //DOM_VK_CLEAR
if (n == 13) return 'Enter'
if (n == 16) return 'Shift'
if (n == 17) return 'Ctrl'
if (n == 18) return 'Alt'
if (n == 19) return 'Pause|Break'
if (n == 20) return 'CapsLock'
if (n == 27) return 'Esc'
if (n == 32) return 'Space'
if (n == 33) return 'PageUp'
if (n == 34) return 'PageDown'
if (n == 35) return 'End'
if (n == 36) return 'Home'
if (n == 37) return 'Left Arrow'
if (n == 38) return 'Up Arrow'
if (n == 39) return 'Right Arrow'
if (n == 40) return 'Down Arrow'
if (n == 42) return '*' //Opera
if (n == 43) return '+' //Opera
if (n == 44) return 'PrntScrn'
if (n == 45) return 'Insert'
if (n == 46) return 'Delete'
if (n == 91) return 'WIN Start'
if (n == 92) return 'WIN Start Right'
if (n == 93) return 'WIN Menu'
if (n == 106) return '*'
if (n == 107) return '+'
if (n == 108) return 'Separator' //DOM_VK_SEPARATOR
if (n == 109) return '-'
if (n == 110) return '.'
if (n == 111) return '/'
if (n == 144) return 'NumLock'
if (n == 145) return 'ScrollLock'
// Firefox 15+ (bug 787504)
// https://bugzilla.mozilla.org/show_bug.cgi?id=787504
// https://github.com/openlayers/openlayers/issues/605
if (-1 != navigator.userAgent.indexOf('Firefox')) {
if (n == 173 && KeyEvent && n == KeyEvent.DOM_VK_HYPHEN_MINUS) return '- _'
if (n == 181 && KeyEvent && n == KeyEvent.DOM_VK_VOLUME_MUTE) return 'Mute On|Off'
if (n == 182 && KeyEvent && n == KeyEvent.DOM_VK_VOLUME_DOWN) return 'Volume Down'
if (n == 183 && KeyEvent && n == KeyEvent.DOM_VK_VOLUME_UP) return 'Volume Up'
}
//Media buttons (Inspiron laptops)
if (n == 173) return 'Mute On|Off'
if (n == 174) return 'Volume Down'
if (n == 175) return 'Volume Up'
if (n == 176) return 'Media >>'
if (n == 177) return 'Media <<'
if (n == 178) return 'Media Stop'
if (n == 179) return 'Media Play|Pause'
if (n == 182) return 'WIN My Computer'
if (n == 183) return 'WIN Calculator'
if (n == 186) return '; :'
if (n == 187) return '= +'
if (n == 188) return ', <'
if (n == 189) return '- _'
if (n == 190) return '. >'
if (n == 191) return '/ ?'
if (n == 192) return '\` ~'
if (n == 219) return '[ {'
if (n == 220) return '\\ |'
if (n == 221) return '] }'
if (n == 222) return '\' "'
if (n == 224) return 'META|Command'
if (n == 229) return 'WIN IME'
if (n == 255) return 'Device-specific' //Dell Home button (Inspiron laptops)
return null
}
// Give more power to the webframes
webFrame.registerURLSchemeAsPrivileged("b")
webFrame.registerURLSchemeAsPrivileged("c")
webFrame.registerURLSchemeAsPrivileged("file")
webFrame.registerURLSchemeAsBypassingCSP("b")
webFrame.registerURLSchemeAsBypassingCSP("c")
webFrame.registerURLSchemeAsBypassingCSP("file")
// Create Menu
menu()
// Create Nav
const enav = new (require('electron-navigation'))({
newTabCallback: function(url, options) {
let newOptions = options
let newUrl = url
newOptions.icon = "file:///" + dirname + "/cap.png"
if (/(^c:\/\/|^b:\/\/).+/.test(url)) {
newOptions.title = url
} else if (/file:\/\/\/[CB]:\/\/.*/i.test(url)) {
newUrl = url.replace(/file:\/\/\//, "").toLowerCase();
newOptions.title = newUrl;
} else if (/file:\/\/.*drive\.html$/.test(url)) {
document.querySelector("#nav-ctrls-url").value = ""
document.querySelector("#nav-ctrls-url").focus()
newOptions.title = "Bottle"
} else {
newOptions.title = "Bottle"
}
newOptions.webviewAttributes.plugins = ""
newOptions.webviewAttributes.preload = dirname + "/preload.js"
let ret = {
url: newUrl, options: newOptions
}
return ret;
},
changeTabCallback: function(el) {
current_url = el.getAttribute('src')
if (/^view-source:.*/.test(current_url)) {
let original_url = current_url.slice(12)
fetch(original_url).then(function(res) {
return res.text()
}).then(function(res) {
el.loadURL("data:text/plain;base64," + utoa(res))
})
} else if (/about:blank/.test(current_url)) {
document.querySelector("#nav-ctrls-url").focus()
} else if (/file:\/\/.*drive\.html$/.test(current_url)) {
document.querySelector("#nav-ctrls-url").value = ""
document.querySelector("#nav-ctrls-url").focus()
}
},
newTabParams: ["file://" + dirname + "/drive.html", {
defaultFavicons: true,
node: true,
icon: "file:///" + dirname + "/cap.png"
}],
});
/***********************************************************************
*
* Override Nav Methods
*
***********************************************************************/
// 1. _purifyUrl Override
enav._purifyUrl = function(str) {
let url = str.trim()
if (/^[13][a-km-zA-HJ-NP-Z0-9]{26,33}$/.test(url)) {
url = "bitcoin:" + url
} else if (/^b?:\/\/.*/i.test(url)) {
} else if (/^c?:\/\/.*/i.test(url)) {
} else if (/file:\/\/\/[CB]:\/\/.*/i.test(url)) {
url = url.replace(/file:\/\/\//i, "")
} else if (/file:.*/i.test(url)) {
} else if (/^\/.*/i.test(url)) {
url = "file:///" + dirname + url
} else if (/^https?:\/\/.*/i.test(url)) {
url = (url.match(/^https?:\/\/.*/i)) ? url : 'http://' + url;
} else if (/^view-source:.*/.test(url)) {
} else if (/^chrome-devtools:.*/.test(url)) {
} else {
url = "about:blank"
}
return url;
}
// 2. _updateUrl Override
var oldUpdateUrl = enav._updateUrl;
enav._updateUrl = function(url) {
if (/file:\/\/\/[bc]:\/\//i.test(url)) {
let newUrl = url.replace(/file:\/\/\//i, "");
oldUpdateUrl(newUrl);
} else if (url) {
oldUpdateUrl(url);
}
}
/***********************************************************************
*
* Create Home Tab
*
***********************************************************************/
var home = enav.newTab("file://" + dirname + "/drive.html", {
id: "homePage",
icon: "file:///" + dirname + "/cap.png",
close: false,
node: true,
webviewAttributes: {
plugins: "",
preload: dirname + "/preload.js"
},
})
home.setAttribute('plugins', '');
/***********************************************************************
*
* Check Settings and Display by default if Not Set
*
***********************************************************************/
var p = path.join(userPath, '.', 'settings.json');
fs.readFile(p, (err, data) => {
let needToSet = false
if(data){
try {
content = JSON.parse(data)
if (!content.b || content.b.length === 0) {
needToSet = true
}
if (!content.c || content.c.length === 0) {
needToSet = true
}
} catch (e) {
needToSet = true
}
} else {
needToSet = true
}
if (needToSet) {
let url = "file:///" + dirname + "/settings.html"
let source = enav.newTab(url, {
node: true,
webviewAttributes: {
plugins: "",
preload: dirname + "/preload.js"
},
})
source.setAttribute('plugins', '');
}
})
/***********************************************************************
*
* Create Extended Navbar Buttons
*
***********************************************************************/
var button = function(o) {
let d = document.createElement("div")
d.innerHTML = '<i id="' + o.id + '" class="nav-tab-item nav-icons" title="' + o.title + '"><img src="' + o.icon + '"></i>'
document.querySelector("#nav-body-ctrls").appendChild(d)
d.addEventListener("click", o.onclick)
}
button({
id: "nav-bookmark", title: "bookmark", icon: "file:///" + dirname + "/star.png", onclick: function(e) {
let new_url = "file:///" + dirname + "/bookmark.html?url=" + current_url
let source = enav.newTab(new_url, {
node: true,
icon: "file:///" + dirname + "/star.png",
webviewAttributes: {
plugins: "",
preload: dirname + "/preload.js"
},
})
source.setAttribute('plugins', '');
}
})
button({
id: "nav-source", title: "view source", icon: "file:///" + dirname + "/code.png", onclick: function(e) {
let new_url = "view-source:" + current_url
let source = enav.newTab(new_url, {
icon: "file:///" + dirname + "/code.png",
node: true,
webviewAttributes: {
plugins: "",
preload: dirname + "/preload.js"
},
})
source.setAttribute('plugins', '');
}
})
button({
id: "nav-settings", title: "settings", icon: "file:///" + dirname + "/wrench.png", onclick: function(e) {
let new_url = "file:///" + dirname + "/settings.html"
let source = enav.newTab(new_url, {
node: true,
icon: "file:///" + dirname + "/wrench.png",
webviewAttributes: {
plugins: "",
preload: dirname + "/preload.js"
},
})
source.setAttribute('plugins', '');
}
})
document.addEventListener("DOMContentLoaded", function(e) {
ipcRenderer.on('open-url', (event, url) => {
webview.loadURL(url)
});
document.addEventListener("keydown", function (e) {
var ctrlPressed = 0;
var altPressed = 0;
var shiftPressed = 0;
var metaPressed = 0;
var evt = (e == null ? event : e);
shiftPressed = evt.shiftKey;
altPressed = evt.altKey;
ctrlPressed = evt.ctrlKey;
metaPressed = evt.metaKey;
// DevTools for each tab => Ctrl+Shift+E
if (shiftPressed && ctrlPressed && fromKeyCode(evt.keyCode) == 'E'){
enav.openDevTools();
e.preventDefault();
return false;
}
if (shiftPressed && metaPressed && fromKeyCode(evt.keyCode) == 'C'){
enav.openDevTools();
e.preventDefault();
return false;
}
// New tab => Ctrl+T
if (ctrlPressed && fromKeyCode(evt.keyCode) == 'T' || metaPressed && fromKeyCode(evt.keyCode) == 'T') {
//let tab = enav.newTab(navDefaultURL, {
let tab = enav.newTab("file://" + dirname + "/drive.html", {
icon: "file:///" + dirname + "/cap.png",
node: true,
webviewAttributes: {
plugins: "", preload: dirname + "/preload.js"
},
})
tab.setAttribute('plugins', '');
e.preventDefault();
return false;
}
if (ctrlPressed && fromKeyCode(evt.keyCode) == 'R' || metaPressed && fromKeyCode(evt.keyCode) == 'R') {
enav.reload()
e.preventDefault();
return false;
}
// Close tab => Ctrl+W
if (ctrlPressed && fromKeyCode(evt.keyCode) == 'W' || metaPressed && fromKeyCode(evt.keyCode) == 'W') {
enav.closeTab();
e.preventDefault();
return false;
}
// Print tab => Ctrl+P
if (ctrlPressed && fromKeyCode(evt.keyCode) == 'P' || metaPressed && fromKeyCode(evt.keyCode) == 'P') {
enav.printTab();
e.preventDefault();
return false;
}
// Prev tab => Ctrl+Shift+Tab
if (ctrlPressed && shiftPressed && fromKeyCode(evt.keyCode) == 'Tab' || metaPressed && shiftPressed && fromKeyCode(evt.keyCode) == 'Tab') {
enav.prevTab();
e.preventDefault();
return false;
}
// Next tab => Ctrl+Tab
if (ctrlPressed && fromKeyCode(evt.keyCode) == 'Tab' || metaPressed && fromKeyCode(evt.keyCode) == 'Tab') {
enav.nextTab();
e.preventDefault();
return false;
}
return true;
});
});
</script>
</body>
</html>