-
Notifications
You must be signed in to change notification settings - Fork 7
/
browser.jsx
270 lines (257 loc) · 9.39 KB
/
browser.jsx
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
'use strict'
var remote = require('remote')
var Menu = remote.require('menu')
var MenuItem = remote.require('menu-item')
var clipboard = require('clipboard')
var urllib = require('url')
function createPageObject (location) {
return {
location: location||'http://3g2upl4pq6kufc4m.onion/',
statusText: false,
title: 'new tab',
isLoading: false,
isSearching: false,
canGoBack: false,
canGoForward: false,
canRefresh: false
}
}
var BrowserChrome = React.createClass({
getInitialState: function () {
return {
pages: [createPageObject()],
currentPageIndex: 0
}
},
componentWillMount: function () {
// bind handlers to this object
for (var k in this.tabHandlers) this.tabHandlers[k] = this.tabHandlers[k].bind(this)
for (var k in this.navHandlers) this.navHandlers[k] = this.navHandlers[k].bind(this)
for (var k in this.pageHandlers) this.pageHandlers[k] = this.pageHandlers[k].bind(this)
},
componentDidMount: function () {
// attach webview events
for (var k in this.webviewHandlers)
this.getWebView().addEventListener(k, this.webviewHandlers[k].bind(this))
// attach keyboard shortcuts
// :TODO: replace this with menu hotkeys
var self = this
document.body.addEventListener('keydown', function (e) {
if (e.metaKey && e.keyCode == 70) { // cmd+f
// start search
self.getPageObject().isSearching = true
self.setState(self.state)
// make sure the search input has focus
self.getPage().getDOMNode().querySelector('#browser-page-search input').focus()
} else if (e.keyCode == 27) { // esc
// stop search
self.getPageObject().isSearching = false
self.setState(self.state)
}
})
},
getWebView: function (i) {
i = (typeof i == 'undefined') ? this.state.currentPageIndex : i
return this.refs['page-'+i].refs.webview.getDOMNode()
},
getPage: function (i) {
i = (typeof i == 'undefined') ? this.state.currentPageIndex : i
return this.refs['page-'+i]
},
getPageObject: function (i) {
i = (typeof i == 'undefined') ? this.state.currentPageIndex : i
return this.state.pages[i]
},
createTab: function (location) {
this.state.pages.push(createPageObject(location))
this.setState({ pages: this.state.pages, currentPageIndex: this.state.pages.length - 1 })
},
closeTab: function (pageIndex) {
// last tab, full reset
if (this.state.pages.filter(Boolean).length == 1)
return this.setState({ pages: [createPageObject()], currentPageIndex: 0 })
this.state.pages[pageIndex] = null
this.setState({ pages: this.state.pages })
// find the nearest adjacent page to make active
if (this.state.currentPageIndex == pageIndex) {
for (var i = pageIndex; i >= 0; i--) {
if (this.state.pages[i])
return this.setState({ currentPageIndex: i })
}
for (var i = pageIndex; i < this.state.pages.length; i++) {
if (this.state.pages[i])
return this.setState({ currentPageIndex: i })
}
}
},
tabContextMenu: function (pageIndex) {
var self = this
var menu = new Menu()
menu.append(new MenuItem({ label: 'New Tab', click: function () { self.createTab() } }))
menu.append(new MenuItem({ label: 'Duplicate', click: function () { self.createTab(self.getPageObject(pageIndex).location) } }))
menu.append(new MenuItem({ type: 'separator' }))
menu.append(new MenuItem({ label: 'Close Tab', click: function() { self.closeTab(pageIndex) } }))
menu.popup(remote.getCurrentWindow())
},
locationContextMenu: function (el) {
var self = this
var menu = new Menu()
menu.append(new MenuItem({ label: 'Copy', click: function () {
clipboard.writeText(el.value)
}}))
menu.append(new MenuItem({ label: 'Cut', click: function () {
clipboard.writeText(el.value.slice(el.selectionStart, el.selectionEnd))
self.getPageObject().location = el.value.slice(0, el.selectionStart) + el.value.slice(el.selectionEnd)
}}))
menu.append(new MenuItem({ label: 'Paste', click: function() {
var l = el.value.slice(0, el.selectionStart) + clipboard.readText() + el.value.slice(el.selectionEnd)
self.getPageObject().location = l
}}))
menu.append(new MenuItem({ label: 'Paste and Go', click: function() {
var l = el.value.slice(0, el.selectionStart) + clipboard.readText() + el.value.slice(el.selectionEnd)
self.getPageObject().location = l
self.getPage().navigateTo(l)
}}))
menu.popup(remote.getCurrentWindow())
},
webviewContextMenu: function (e) {
var self = this
var menu = new Menu()
if (e.href) {
menu.append(new MenuItem({ label: 'Open Link in New Tab', click: function () { self.createTab(e.href) } }))
menu.append(new MenuItem({ label: 'Copy Link Address', click: function () { clipboard.writeText(e.href) } }))
}
if (e.img) {
menu.append(new MenuItem({ label: 'Save Image As...', click: function () { alert('todo') } }))
menu.append(new MenuItem({ label: 'Copy Image URL', click: function () { clipboard.writeText(e.img) } }))
menu.append(new MenuItem({ label: 'Open Image in New Tab', click: function () { self.createTab(e.img) } }))
}
if (e.hasSelection)
menu.append(new MenuItem({ label: 'Copy', click: function () { self.getWebView().copy() } }))
menu.append(new MenuItem({ label: 'Select All', click: function () { self.getWebView().selectAll() } }))
menu.append(new MenuItem({ type: 'separator' }))
menu.append(new MenuItem({ label: 'Inspect Element', click: function() { self.getWebView().inspectElement(e.x, e.y) } }))
menu.popup(remote.getCurrentWindow())
},
tabHandlers: {
onNewTab: function () {
this.createTab()
},
onTabClick: function (e, page, pageIndex) {
this.setState({ currentPageIndex: pageIndex })
},
onTabContextMenu: function (e, page, pageIndex) {
this.tabContextMenu(pageIndex)
},
onTabClose: function (e, page, pageIndex) {
this.closeTab(pageIndex)
},
onMaximize: function () {
if (remote.getCurrentWindow())
remote.getCurrentWindow().maximize()
else
remote.unmaximize()
},
onMinimize: function () {
remote.getCurrentWindow().minimize()
},
onClose: function () {
remote.getCurrentWindow().close()
}
},
navHandlers: {
onClickHome: function () {
this.getWebView().goToIndex(0)
},
onClickBack: function () {
this.getWebView().goBack()
},
onClickForward: function () {
this.getWebView().goForward()
},
onClickRefresh: function () {
this.getWebView().reload()
},
onClickBundles: function () {
var location = urllib.parse(this.getWebView().getUrl()).path
this.getPage().navigateTo('/bundles/view.html#'+location)
},
onClickVersions: function () {
var location = urllib.parse(this.getWebView().getUrl()).path
this.getPage().navigateTo('/bundles/versions.html#'+location)
},
onClickSync: console.log.bind(console, 'sync'),
onEnterLocation: function (location) {
this.getPage().navigateTo(location)
},
onChangeLocation: function (location) {
var page = this.getPageObject()
page.location = location
this.setState(this.state)
},
onLocationContextMenu: function (e) {
this.locationContextMenu(e.target)
}
},
pageHandlers: {
onDidStartLoading: function (e, page) {
page.isLoading = true
page.title = false
this.setState(this.state)
},
onDomReady: function (e, page, pageIndex) {
var webview = this.getWebView(pageIndex)
page.canGoBack = webview.canGoBack()
page.canGoForward = webview.canGoForward()
page.canRefresh = true
this.setState(this.state)
},
onDidStopLoading: function (e, page, pageIndex) {
// update state
var webview = this.getWebView(pageIndex)
page.statusText = false
page.location = webview.getUrl()
page.canGoBack = webview.canGoBack()
page.canGoForward = webview.canGoForward()
if (!page.title)
page.title = page.location
page.isLoading = false
this.setState(this.state)
},
onPageTitleSet: function (e) {
var page = this.getPageObject()
page.title = e.title
page.location = this.getWebView().getUrl()
this.setState(this.state)
},
onContextMenu: function (e, page, pageIndex) {
this.getWebView(pageIndex).send('get-contextmenu-data', { x: e.nativeEvent.offsetX, y: e.nativeEvent.offsetY })
},
onIpcMessage: function (e, page) {
if (e.channel == 'status') {
page.statusText = e.args[0]
this.setState(this.state)
}
else if (e.channel == 'contextmenu-data') {
this.webviewContextMenu(e.args[0])
}
}
},
render: function() {
var self = this
return <div>
<BrowserTabs ref="tabs" pages={this.state.pages} currentPageIndex={this.state.currentPageIndex} {...this.tabHandlers} />
<BrowserNavbar ref="navbar" {...this.navHandlers} page={this.state.pages[this.state.currentPageIndex]} />
{this.state.pages.map(function (page, i) {
if (!page)
return
return <BrowserPage ref={'page-'+i} key={'page-'+i} {...self.pageHandlers} page={page} pageIndex={i} isActive={i == self.state.currentPageIndex} />
})}
</div>
}
})
// render
React.render(
<BrowserChrome />,
document.getElementById('browser-chrome')
)