forked from interplanaria/bottle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
130 lines (129 loc) · 4.93 KB
/
events.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
var ondragstart = function(ev) {
let url = ev.target.dataset.url;
Bookmarklet.del(url);
}
var ondrop = function(ev) {
ev.preventDefault()
ev.stopPropagation()
if (ev.target.id === 'nav-body-shortcuts' || ev.target.closest("#nav-body-shortcuts")) {
let url = ev.dataTransfer.getData("text/plain");
Bookmarklet.set(url);
}
}
var onkeydown = 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 && Util.fromKeyCode(evt.keyCode) == 'E'){
Nav.openDevTools();
e.preventDefault();
return false;
}
if (shiftPressed && metaPressed && Util.fromKeyCode(evt.keyCode) == 'C'){
Nav.openDevTools();
e.preventDefault();
return false;
}
// New tab => Ctrl+T
if (ctrlPressed && Util.fromKeyCode(evt.keyCode) == 'T' || metaPressed && Util.fromKeyCode(evt.keyCode) == 'T') {
document.querySelector("#nav-ctrls-url").blur();
let tab = Nav.newTab("about:blank", {
icon: "bottle://assets/cap.png",
webviewAttributes: {
plugins: "", preload: dirname + "/preload.js"
},
})
tab.setAttribute('plugins', '');
e.preventDefault();
setTimeout(function() {
document.querySelector("#nav-ctrls-url").focus();
}, 100)
return false;
}
if (ctrlPressed && Util.fromKeyCode(evt.keyCode) == 'R' || metaPressed && Util.fromKeyCode(evt.keyCode) == 'R') {
Nav.reload();
e.preventDefault();
return false;
}
// Close tab => Ctrl+W
if (ctrlPressed && Util.fromKeyCode(evt.keyCode) == 'W' || metaPressed && Util.fromKeyCode(evt.keyCode) == 'W') {
Nav.closeTab();
e.preventDefault();
return false;
}
// Print tab => Ctrl+P
if (ctrlPressed && Util.fromKeyCode(evt.keyCode) == 'P' || metaPressed && Util.fromKeyCode(evt.keyCode) == 'P') {
Nav.printTab();
e.preventDefault();
return false;
}
// Prev tab => Ctrl+Shift+Tab
if (ctrlPressed && shiftPressed && Util.fromKeyCode(evt.keyCode) == 'Tab' || metaPressed && shiftPressed && Util.fromKeyCode(evt.keyCode) == 'Tab') {
Nav.prevTab();
e.preventDefault();
return false;
}
// Next tab => Ctrl+Tab
if (ctrlPressed && Util.fromKeyCode(evt.keyCode) == 'Tab' || metaPressed && Util.fromKeyCode(evt.keyCode) == 'Tab') {
Nav.nextTab();
e.preventDefault();
return false;
}
return true;
}
module.exports = {
init: function() {
document.addEventListener("DOMContentLoaded", function(e) {
ipcRenderer.on('route-updated', (event, url) => {
console.log("ROUTE UPDATED", url);
document.querySelectorAll("webview[src='" + url + "']").forEach(function(el) {
el.reload();
})
});
ipcRenderer.on('open-tab', (event, url) => {
if (!/^bottle:.+/.test(url)) {
let tab = Nav.newTab(url, { icon: "bottle://assets/cap.png" })
}
});
ipcRenderer.on('resolve-error', (event, address) => {
let url = document.querySelector("#nav-ctrls-url").value;
console.log("url = ", url);
if (/^bit:.+/.test(url)) {
let html = "<div><i class='fas fa-exclamation-circle'></i> Connection required</div><div class='flexible'></div><a href='bottle://bitcom?redirect=" + url +"&address=" + address + "' target='_blank' class='btn'>Connect to " + address + "</a>";;
document.querySelector("#nav-footer .sub").innerHTML = html;
}
});
document.querySelector("#nav-body-shortcuts").addEventListener("dragleave", function(ev) {
ev.preventDefault();
document.querySelector("#nav-body-shortcuts").style.backgroundColor = "whitesmoke";
}, false);
document.querySelector("#nav-body-shortcuts").addEventListener("dragover", function(ev) {
ev.preventDefault();
document.querySelector("#nav-body-shortcuts").style.backgroundColor = "silver";
}, false);
document.querySelector("#nav-body-shortcuts").addEventListener("click", function(e) {
if (e.target.className === 'shortcut') {
let url = e.target.dataset.url;
if (/^(https?:|b:|c:|bit:|bottle:).*/i.test(url)) {
document.querySelector("webview.nav-views-view.active").executeJavaScript("location.href='" + url + "'")
} else if (/^javascript:/.test(url)) {
let js = url.replace(/javascript:/, "");
document.querySelector("webview.nav-views-view.active").executeJavaScript(js);
} else {
document.querySelector("webview.nav-views-view.active").executeJavaScript(url);
}
}
})
document.addEventListener("dragstart", ondragstart);
document.addEventListener("drop", ondrop);
document.addEventListener("keydown", onkeydown);
});
}
}