forked from interplanaria/bottle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nav.js
182 lines (171 loc) · 6.73 KB
/
nav.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
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
const Route = require('./route');
const Util = require('./util');
const Bookmarklet = require('./bookmarklet');
const CONSTANTS = require('./constants');
const handleNav = function() {
let current_url = Route.get();
let src = document.querySelector("#nav-source");
let bookmark = document.querySelector("#nav-bookmark");
let bar = document.querySelector("#nav-body-shortcuts");
if (/^bottle:\/\/.*/.test(current_url)) {
if (src) src.classList.add("hidden");
if (bar) bar.classList.add("disabled");
if (bookmark) bookmark.classList.add("hidden");
} else {
if (src) src.classList.remove("hidden");
if (bar) bar.classList.remove("disabled");
if (bookmark) bookmark.classList.remove("hidden");
}
}
// Create Nav
const enav = new (require('electron-navigation'))({
newTabCallback: function(url, options) {
let newOptions = options;
let newUrl = url;
newOptions.icon = "bottle://assets/cap.png";
if (/^bottle:\/\/.*/.test(url)) {
newOptions.webviewAttributes.nodeIntegration = true;
newOptions.title = newUrl;
newOptions.webviewAttributes.readonlyUrl = true;
newOptions.readonlyUrl = true;
}
Route.set(url);
newOptions.webviewAttributes.plugins = "";
newOptions.webviewAttributes.defaultEncoding = "utf-8";
newOptions.webviewAttributes.preload = dirname + "/preload.js";
newOptions.webviewAttributes.disablewebsecurity = "";
newOptions.webviewAttributes.contextIsolation = "";
let ret = { url: newUrl, options: newOptions };
return ret;
},
changeTabCallback: function(el) {
let current_url = Route.set(el.getAttribute('src'));
handleNav()
},
newTabParams: ["about:blank", {
defaultFavicons: true,
icon: "bottle://assets/cap.png"
}],
});
var addEvents = enav._addEvents;
enav._addEvents = function (sessionID, options) {
let wv = addEvents(sessionID, options);
wv.addEventListener("did-fail-load", function(res) {
console.log("#### Err = ", res)
if (res.errorCode != -3) {
let m = /bit:\/\/([^\/]+)\/.*/i.exec(res.validatedURL)
if (m && m.length > 0) {
let style = "var style = document.body.style; style.padding='100px'; style.textAlign='center'; style.margin=0; style.background='rgba(0,0,0,0.9)'; style.color='white'; style.fontFamily='Menlo,monaco,monospace'; style.fontSize='11px';"
let s = style + " document.body.innerHTML='<div>" +
"<div>The protocol is not yet connected. Please connect with an endpoint</div>" +
"<a style=\"margin: 20px; background: burlywood; padding: 10px; border-radius: 2px; text-decoration: none; display: inline-block; padding: 10px; color: rgba(0,0,0,0.8); border: 1px solid rgba(0,0,0,0.3); \" target=\"_blank\" href=\"bottle://bitcom?redirect=" + res.validatedURL + "&address=" + m[1] + "\">Connect to " + m[1] + "</a>" +
"</div>';";
wv.executeJavaScript(s);
}
}
})
return wv;
}
// Block iframe from changing URL
window.addEventListener("did-navigate-in-page", function (event) {
if (!event.isMainFrame) {
event.stopPropagation();
}
}, true);
var updateUrl = enav._updateUrl;
enav._updateUrl = function(url) {
document.querySelector("#nav-footer .sub").innerHTML = "";
let current_view = document.querySelector("webview.active");
updateUrl(url);
Route.set(url);
let m = /bit:\/\/([^\/]+)\/.*/i.exec(url);
if (m && m.length > 0) {
const router = remote.getGlobal("router")
let connection = router.get(m[1]);
if (connection && Object.keys(connection).length > 0) {
let _path = Object.keys(connection)[0];
let _endpoint = connection[_path];
if (_endpoint) {
let _addr = Object.keys(_endpoint)[0];
let _api = Object.values(_endpoint)[0];
let html = "<div><i class='fas fa-plug'></i> " + _path + " <i class='fas fa-angle-double-right'></i> " + _api + "</div><a href='bottle://bitcom?redirect=" + url + "&address=" + m[1] + "' target='_blank' class='btn'>Switch Endpoint</a>";
document.querySelector("#nav-footer .main").innerHTML = html;
} else {
let html = "<div><i class='fas fa-plug'></i> " + _path + "</div><a href='bottle://bitcom?redirect=" + url + "&address=" + m[1] + "' target='_blank' class='btn'>Switch Endpoint</a>";
document.querySelector("#nav-footer .main").innerHTML = html;
}
} else {
let html = "<div><i class='fas fa-plug'></i> " + _path + "</div><a href='bottle://bitcom?redirect=" + url + "&address=" + m[1] + "' target='_blank' class='btn'>Switch Endpoint</a>";
document.querySelector("#nav-footer .main").innerHTML = html;
}
} else {
document.querySelector("#nav-footer .main").innerHTML = "";
}
if (Bookmarklet) {
Bookmarklet.update();
}
}
/***********************************************************************
*
* Override Nav Methods
*
***********************************************************************/
var pu = enav._purifyUrl;
enav._purifyUrl = function(str) {
let p = str.trim();
let r = /^(bit|b|c):\/\/([^\/]+)/i.exec(p);
let res;
if (r && r.length > 0) {
if (r[1].toLowerCase() === 'b') {
res = "bit://" + CONSTANTS.B + "/" + r[2];
} else if (r[1].toLowerCase() === 'c') {
res = "bit://" + CONSTANTS.C + "/" + r[2];
} else {
res = p;
}
} else if (p === 'about:blank') {
res = p;
} else {
res = pu(str);
}
return res;
}
/***********************************************************************
*
* 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' + (o.hidden ? ' hidden"' : '"') + ' title="' + o.title + '"><img src="' + o.icon + '"></i>';
document.querySelector("#nav-body-ctrls").appendChild(d);
d.addEventListener("click", o.onclick);
}
button({
id: "nav-bookmark", hidden: true, title: "bookmark", icon: "bottle://assets/star.png", onclick: function(e) {
let current_url = Route.get();
let match = /chrome:\/\/[^/]+\/index\.html\?src=(.+)$/g.exec(current_url);
if (match && match.length > 1) {
current_url = Route.set(match[1]);
}
if (Bookmarklet.items.map(function(item) { return item.url }).includes(current_url)) {
Bookmarklet.del(current_url);
} else {
Bookmarklet.set(current_url);
}
}
});
button({
id: "nav-source", hidden: true, title: "view source", icon: "bottle://assets/code.png", onclick: function(e) {
let current_url = Route.get();
let new_url = "bottle://inspect?uri=" + current_url;
let source = Nav.newTab(new_url, {
icon: "bottle://assets/code.png",
readonlyUrl: true,
})
}
})
Bookmarklet.get().then(function(items) {
Bookmarklet.render(items);
});
module.exports = enav;