stage-browser 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +89 -2
- package/bin/stage.js +52 -0
- package/build/icon.png +0 -0
- package/package.json +40 -4
- package/src/main/cli-args.js +28 -0
- package/src/main/context-menu.js +64 -0
- package/src/main/extensions.js +61 -0
- package/src/main/main.js +310 -0
- package/src/main/menu.js +171 -0
- package/src/main/reveal.js +141 -0
- package/src/main/selftest.js +383 -0
- package/src/main/store.js +77 -0
- package/src/main/suggest.js +50 -0
- package/src/main/tabs.js +351 -0
- package/src/main/url.js +37 -0
- package/src/main/window.js +71 -0
- package/src/preload/extension-compat.js +34 -0
- package/src/preload/ui-preload.js +40 -0
- package/src/ui/app.js +588 -0
- package/src/ui/index.html +44 -0
- package/src/ui/mock.js +455 -0
- package/src/ui/newtab.html +69 -0
- package/src/ui/styles.css +400 -0
package/src/main/tabs.js
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { WebContentsView, shell } = require('electron')
|
|
4
|
+
const store = require('./store')
|
|
5
|
+
const { isWebUrl } = require('./url')
|
|
6
|
+
|
|
7
|
+
const NEWTAB_FILE = path.join(__dirname, '..', 'ui', 'newtab.html')
|
|
8
|
+
const WEB_SCHEMES = new Set(['http:', 'https:', 'file:', 'about:', 'data:', 'blob:', 'chrome-extension:', 'view-source:', 'devtools:', 'chrome:', 'javascript:'])
|
|
9
|
+
|
|
10
|
+
function schemeOf(url) {
|
|
11
|
+
const m = /^([a-z][a-z0-9+.-]*:)/i.exec(url || '')
|
|
12
|
+
return m ? m[1].toLowerCase() : ''
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
class Tabs {
|
|
16
|
+
constructor(ctx) {
|
|
17
|
+
this.ctx = ctx
|
|
18
|
+
this.list = []
|
|
19
|
+
this.activeId = null
|
|
20
|
+
this.closed = [] // urls, most recent last
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get(id) {
|
|
24
|
+
return this.list.find((t) => t.id === id)
|
|
25
|
+
}
|
|
26
|
+
get active() {
|
|
27
|
+
return this.get(this.activeId)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ---- creation ----------------------------------------------------------
|
|
31
|
+
// opts: { url?: string, activate?: boolean, webContents?: WebContents, index?: number }
|
|
32
|
+
// `webContents` comes from Electron's window-open path: the view must adopt it (it keeps
|
|
33
|
+
// window.opener) and Electron drives its first navigation.
|
|
34
|
+
create(opts = {}) {
|
|
35
|
+
const { ctx } = this
|
|
36
|
+
const activate = opts.activate !== false
|
|
37
|
+
const view = opts.webContents
|
|
38
|
+
? new WebContentsView({ webContents: opts.webContents })
|
|
39
|
+
: new WebContentsView({
|
|
40
|
+
webPreferences: { session: ctx.session, sandbox: true, contextIsolation: true, nodeIntegration: false },
|
|
41
|
+
})
|
|
42
|
+
view.setBackgroundColor(ctx.bgColor())
|
|
43
|
+
const wc = view.webContents
|
|
44
|
+
const tab = {
|
|
45
|
+
id: wc.id,
|
|
46
|
+
view,
|
|
47
|
+
wc,
|
|
48
|
+
title: '',
|
|
49
|
+
url: '',
|
|
50
|
+
favicon: null,
|
|
51
|
+
loading: false,
|
|
52
|
+
audible: false,
|
|
53
|
+
muted: false,
|
|
54
|
+
newTab: false,
|
|
55
|
+
}
|
|
56
|
+
const at = typeof opts.index === 'number' ? opts.index : this.list.length
|
|
57
|
+
this.list.splice(at, 0, tab)
|
|
58
|
+
ctx.win.contentView.addChildView(view, 0) // UI view stays on top
|
|
59
|
+
view.setBounds(ctx.tabBounds())
|
|
60
|
+
view.setVisible(false)
|
|
61
|
+
this.wire(tab)
|
|
62
|
+
ctx.ext && ctx.ext.addTab(wc, ctx.win)
|
|
63
|
+
if (!opts.webContents) {
|
|
64
|
+
if (opts.url) this.load(tab, opts.url)
|
|
65
|
+
else this.loadNewTab(tab)
|
|
66
|
+
}
|
|
67
|
+
if (activate) this.activate(tab.id)
|
|
68
|
+
else ctx.push()
|
|
69
|
+
return tab
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
loadNewTab(tab) {
|
|
73
|
+
tab.newTab = true
|
|
74
|
+
tab.wc.loadFile(NEWTAB_FILE).catch(() => {})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
load(tab, url) {
|
|
78
|
+
tab.newTab = false
|
|
79
|
+
tab.wc.loadURL(url).catch(() => {}) // ERR_ABORTED etc. are reported via did-fail-load
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ---- wiring ------------------------------------------------------------
|
|
83
|
+
wire(tab) {
|
|
84
|
+
const { wc } = tab
|
|
85
|
+
const push = () => this.ctx.push()
|
|
86
|
+
const syncUrl = () => {
|
|
87
|
+
const u = wc.getURL()
|
|
88
|
+
tab.newTab = u.startsWith('file://') && decodeURI(u).endsWith('/ui/newtab.html')
|
|
89
|
+
tab.url = tab.newTab ? '' : u
|
|
90
|
+
}
|
|
91
|
+
wc.on('page-title-updated', (_e, title) => {
|
|
92
|
+
tab.title = title
|
|
93
|
+
if (isWebUrl(tab.url)) store.setHistoryTitle(tab.url, title)
|
|
94
|
+
push()
|
|
95
|
+
})
|
|
96
|
+
wc.on('page-favicon-updated', (_e, favs) => {
|
|
97
|
+
tab.favicon = favs[0] || null
|
|
98
|
+
push()
|
|
99
|
+
})
|
|
100
|
+
wc.on('did-start-navigation', (e) => {
|
|
101
|
+
if (e.isMainFrame && !e.isSameDocument) tab.favicon = null
|
|
102
|
+
})
|
|
103
|
+
wc.on('did-start-loading', () => {
|
|
104
|
+
tab.loading = true
|
|
105
|
+
push()
|
|
106
|
+
})
|
|
107
|
+
wc.on('did-stop-loading', () => {
|
|
108
|
+
tab.loading = false
|
|
109
|
+
syncUrl()
|
|
110
|
+
if (!tab.title && tab.url) tab.title = tab.url
|
|
111
|
+
push()
|
|
112
|
+
})
|
|
113
|
+
wc.on('did-navigate', (_e, url) => {
|
|
114
|
+
syncUrl()
|
|
115
|
+
if (isWebUrl(url)) store.addVisit(url, wc.getTitle())
|
|
116
|
+
this.ctx.saveSession()
|
|
117
|
+
push()
|
|
118
|
+
})
|
|
119
|
+
wc.on('did-navigate-in-page', (_e, _url, isMain) => {
|
|
120
|
+
if (!isMain) return
|
|
121
|
+
syncUrl()
|
|
122
|
+
this.ctx.saveSession()
|
|
123
|
+
push()
|
|
124
|
+
})
|
|
125
|
+
wc.on('audio-state-changed', (e) => {
|
|
126
|
+
tab.audible = !!e.audible
|
|
127
|
+
push()
|
|
128
|
+
})
|
|
129
|
+
wc.on('zoom-changed', push)
|
|
130
|
+
wc.on('did-fail-load', (_e, code, _d, _u, isMain) => {
|
|
131
|
+
if (isMain && code !== -3) {
|
|
132
|
+
syncUrl()
|
|
133
|
+
push()
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
wc.on('destroyed', () => this.removeDead(tab))
|
|
137
|
+
wc.on('context-menu', (_e, params) => require('./context-menu').showPageMenu(this.ctx, tab, params))
|
|
138
|
+
|
|
139
|
+
const external = (e, url) => {
|
|
140
|
+
const s = schemeOf(url)
|
|
141
|
+
if (s && !WEB_SCHEMES.has(s)) {
|
|
142
|
+
e.preventDefault()
|
|
143
|
+
shell.openExternal(url).catch(() => {})
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
wc.on('will-navigate', external)
|
|
147
|
+
wc.on('will-redirect', external)
|
|
148
|
+
wc.setWindowOpenHandler((d) => this.openHandler(d))
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Shared by tab webContents and sign-in popup windows.
|
|
152
|
+
openHandler(d) {
|
|
153
|
+
const { url, disposition } = d
|
|
154
|
+
const s = schemeOf(url)
|
|
155
|
+
if (s && !WEB_SCHEMES.has(s)) {
|
|
156
|
+
shell.openExternal(url).catch(() => {})
|
|
157
|
+
return { action: 'deny' }
|
|
158
|
+
}
|
|
159
|
+
if (disposition === 'new-window') {
|
|
160
|
+
return {
|
|
161
|
+
action: 'allow',
|
|
162
|
+
overrideBrowserWindowOptions: { autoHideMenuBar: true, backgroundColor: this.ctx.bgColor(), webPreferences: { sandbox: true, contextIsolation: true } },
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const background = disposition === 'background-tab'
|
|
166
|
+
return {
|
|
167
|
+
action: 'allow',
|
|
168
|
+
createWindow: (options) => {
|
|
169
|
+
const tab = this.create({
|
|
170
|
+
webContents: options.webContents,
|
|
171
|
+
url: options.webContents ? undefined : url,
|
|
172
|
+
activate: !background,
|
|
173
|
+
index: this.insertIndexAfterActive(),
|
|
174
|
+
})
|
|
175
|
+
return tab.wc
|
|
176
|
+
},
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
insertIndexAfterActive() {
|
|
181
|
+
const i = this.list.findIndex((t) => t.id === this.activeId)
|
|
182
|
+
return i < 0 ? this.list.length : i + 1
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Popup windows (OAuth etc.) get the same link/scheme handling.
|
|
186
|
+
attachPopup(win) {
|
|
187
|
+
const wc = win.webContents
|
|
188
|
+
wc.setWindowOpenHandler((d) => this.openHandler(d))
|
|
189
|
+
const external = (e, url) => {
|
|
190
|
+
const s = schemeOf(url)
|
|
191
|
+
if (s && !WEB_SCHEMES.has(s)) {
|
|
192
|
+
e.preventDefault()
|
|
193
|
+
shell.openExternal(url).catch(() => {})
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
wc.on('will-navigate', external)
|
|
197
|
+
wc.on('will-redirect', external)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ---- state transitions -------------------------------------------------
|
|
201
|
+
activate(id) {
|
|
202
|
+
const tab = this.get(id)
|
|
203
|
+
if (!tab) return
|
|
204
|
+
this.activeId = id
|
|
205
|
+
for (const t of this.list) t.view.setVisible(t === tab)
|
|
206
|
+
tab.view.setBounds(this.ctx.tabBounds())
|
|
207
|
+
this.ctx.ext && this.ctx.ext.selectTab(tab.wc)
|
|
208
|
+
if (!this.ctx.overlay) tab.wc.focus()
|
|
209
|
+
this.ctx.saveSession()
|
|
210
|
+
this.ctx.push()
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
focusActive() {
|
|
214
|
+
const t = this.active
|
|
215
|
+
if (t && !t.wc.isDestroyed()) t.wc.focus()
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
close(id) {
|
|
219
|
+
const tab = this.get(id)
|
|
220
|
+
if (!tab) return
|
|
221
|
+
if (tab.url && isWebUrl(tab.url)) this.closed.push(tab.url)
|
|
222
|
+
if (this.closed.length > 25) this.closed.shift()
|
|
223
|
+
this.removeFromList(tab)
|
|
224
|
+
try {
|
|
225
|
+
this.ctx.win.contentView.removeChildView(tab.view)
|
|
226
|
+
} catch {}
|
|
227
|
+
try {
|
|
228
|
+
this.ctx.ext && this.ctx.ext.removeTab(tab.wc)
|
|
229
|
+
} catch {}
|
|
230
|
+
if (!tab.wc.isDestroyed()) tab.wc.close()
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// webContents went away on its own (window.close(), crash cleanup)
|
|
234
|
+
removeDead(tab) {
|
|
235
|
+
if (!this.get(tab.id)) return
|
|
236
|
+
this.removeFromList(tab)
|
|
237
|
+
try {
|
|
238
|
+
this.ctx.win.contentView.removeChildView(tab.view)
|
|
239
|
+
} catch {}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
removeFromList(tab) {
|
|
243
|
+
const i = this.list.indexOf(tab)
|
|
244
|
+
if (i < 0) return
|
|
245
|
+
this.list.splice(i, 1)
|
|
246
|
+
if (this.ctx.quitting) return
|
|
247
|
+
if (this.list.length === 0) {
|
|
248
|
+
this.create({})
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
if (this.activeId === tab.id) this.activate(this.list[Math.min(i, this.list.length - 1)].id)
|
|
252
|
+
else {
|
|
253
|
+
this.ctx.saveSession()
|
|
254
|
+
this.ctx.push()
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
closeOthers(id) {
|
|
259
|
+
for (const t of [...this.list]) if (t.id !== id) this.close(t.id)
|
|
260
|
+
}
|
|
261
|
+
closeRight(id) {
|
|
262
|
+
const i = this.list.findIndex((t) => t.id === id)
|
|
263
|
+
for (const t of this.list.slice(i + 1)) this.close(t.id)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
reopenClosed() {
|
|
267
|
+
const url = this.closed.pop()
|
|
268
|
+
if (url) this.create({ url })
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
duplicate(id) {
|
|
272
|
+
const t = this.get(id)
|
|
273
|
+
if (!t) return
|
|
274
|
+
const i = this.list.indexOf(t)
|
|
275
|
+
this.create(t.url ? { url: t.url, index: i + 1 } : { index: i + 1 })
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
move(id, toIndex) {
|
|
279
|
+
const from = this.list.findIndex((t) => t.id === id)
|
|
280
|
+
if (from < 0) return
|
|
281
|
+
const [t] = this.list.splice(from, 1)
|
|
282
|
+
this.list.splice(Math.max(0, Math.min(toIndex, this.list.length)), 0, t)
|
|
283
|
+
this.ctx.saveSession()
|
|
284
|
+
this.ctx.push()
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
step(delta) {
|
|
288
|
+
if (this.list.length < 2) return
|
|
289
|
+
const i = this.list.findIndex((t) => t.id === this.activeId)
|
|
290
|
+
this.activate(this.list[(i + delta + this.list.length) % this.list.length].id)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
goTo(n) {
|
|
294
|
+
// n is 1-based; 9 means last
|
|
295
|
+
const t = n === 9 ? this.list[this.list.length - 1] : this.list[n - 1]
|
|
296
|
+
if (t) this.activate(t.id)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
toggleMute(id) {
|
|
300
|
+
const t = this.get(id)
|
|
301
|
+
if (!t) return
|
|
302
|
+
t.muted = !t.muted
|
|
303
|
+
t.wc.setAudioMuted(t.muted)
|
|
304
|
+
this.ctx.push()
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// navigate the active tab (or open a new one)
|
|
308
|
+
open(url, { newTab }) {
|
|
309
|
+
const t = this.active
|
|
310
|
+
if (newTab || !t) return this.create({ url, index: this.insertIndexAfterActive() })
|
|
311
|
+
this.load(t, url)
|
|
312
|
+
this.activate(t.id)
|
|
313
|
+
return t
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
layout() {
|
|
317
|
+
const b = this.ctx.tabBounds()
|
|
318
|
+
for (const t of this.list) t.view.setBounds(b)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
zoom(delta) {
|
|
322
|
+
const t = this.active
|
|
323
|
+
if (!t) return
|
|
324
|
+
const f = delta === 0 ? 1 : Math.min(5, Math.max(0.25, Math.round((t.wc.getZoomFactor() + delta) * 100) / 100))
|
|
325
|
+
t.wc.setZoomFactor(f)
|
|
326
|
+
this.ctx.push()
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// ---- snapshot ----------------------------------------------------------
|
|
330
|
+
snapshot() {
|
|
331
|
+
const a = this.active
|
|
332
|
+
const nav = a && !a.wc.isDestroyed() ? a.wc.navigationHistory : null
|
|
333
|
+
return {
|
|
334
|
+
tabs: this.list.map((t) => ({
|
|
335
|
+
id: t.id,
|
|
336
|
+
title: t.newTab ? 'New Tab' : t.title || t.url || 'Untitled',
|
|
337
|
+
url: t.url,
|
|
338
|
+
favicon: t.favicon,
|
|
339
|
+
loading: t.loading,
|
|
340
|
+
audible: t.audible,
|
|
341
|
+
muted: t.muted,
|
|
342
|
+
})),
|
|
343
|
+
activeTabId: this.activeId,
|
|
344
|
+
canGoBack: !!nav && nav.canGoBack(),
|
|
345
|
+
canGoForward: !!nav && nav.canGoForward(),
|
|
346
|
+
zoomFactor: a && !a.wc.isDestroyed() ? a.wc.getZoomFactor() : 1,
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
module.exports = { Tabs, NEWTAB_FILE }
|
package/src/main/url.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
// Omnibox input -> URL. Pure functions, no Electron dependency.
|
|
3
|
+
|
|
4
|
+
const SCHEME_SLASHES = /^[a-z][a-z0-9+.-]*:\/\//i
|
|
5
|
+
const SCHEME_BARE = /^(about|data|mailto|view-source|blob|chrome-extension|file):/i
|
|
6
|
+
const LOCAL = /^(localhost|\[[0-9a-f:]+\]|\d{1,3}(\.\d{1,3}){3})(:\d+)?([/?#].*)?$/i
|
|
7
|
+
const DOTTED = /^[^\s/?#:]+\.[a-z][a-z0-9-]*(:\d+)?([/?#]\S*)?$/i
|
|
8
|
+
|
|
9
|
+
function searchUrl(q) {
|
|
10
|
+
return 'https://www.google.com/search?q=' + encodeURIComponent(q)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function resolveInput(input) {
|
|
14
|
+
const s = String(input == null ? '' : input).trim()
|
|
15
|
+
if (!s) return null
|
|
16
|
+
if (SCHEME_SLASHES.test(s) || SCHEME_BARE.test(s)) return s
|
|
17
|
+
if (!/\s/.test(s)) {
|
|
18
|
+
if (LOCAL.test(s)) return 'http://' + s
|
|
19
|
+
if (DOTTED.test(s)) return 'https://' + s
|
|
20
|
+
}
|
|
21
|
+
return searchUrl(s)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// What kind of suggestion does the raw input produce?
|
|
25
|
+
function classify(input) {
|
|
26
|
+
const s = String(input || '').trim()
|
|
27
|
+
if (!s) return null
|
|
28
|
+
const url = resolveInput(s)
|
|
29
|
+
const isSearch = url.startsWith('https://www.google.com/search?q=') && !s.startsWith('http')
|
|
30
|
+
return { url, kind: isSearch ? 'search' : 'url' }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isWebUrl(u) {
|
|
34
|
+
return /^https?:\/\//i.test(u || '')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = { resolveInput, classify, searchUrl, isWebUrl }
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { BaseWindow, WebContentsView, nativeTheme, screen, session } = require('electron')
|
|
4
|
+
|
|
5
|
+
const BAR_HEIGHT = 52
|
|
6
|
+
const TRAFFIC_LIGHTS = { x: 20, y: 19 } // 52px strip, 14px buttons -> centered
|
|
7
|
+
|
|
8
|
+
function bgColor() {
|
|
9
|
+
return nativeTheme.shouldUseDarkColors ? '#1c1c1e' : '#ffffff'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function createWindow(ctx, saved, size) {
|
|
13
|
+
let bounds = { width: 1280, height: 800 }
|
|
14
|
+
if (saved && saved.width && saved.height) {
|
|
15
|
+
// only restore if it still lands on a display
|
|
16
|
+
const onScreen = screen.getAllDisplays().some((d) => {
|
|
17
|
+
const a = d.workArea
|
|
18
|
+
return saved.x < a.x + a.width - 40 && saved.x + saved.width > a.x + 40 && saved.y >= a.y - 10 && saved.y < a.y + a.height - 40
|
|
19
|
+
})
|
|
20
|
+
bounds = onScreen ? saved : { width: saved.width, height: saved.height }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const win = new BaseWindow({
|
|
24
|
+
...bounds,
|
|
25
|
+
useContentSize: !!size,
|
|
26
|
+
minWidth: 480,
|
|
27
|
+
minHeight: 320,
|
|
28
|
+
show: false,
|
|
29
|
+
titleBarStyle: 'hidden',
|
|
30
|
+
trafficLightPosition: TRAFFIC_LIGHTS,
|
|
31
|
+
backgroundColor: bgColor(),
|
|
32
|
+
})
|
|
33
|
+
if (size) {
|
|
34
|
+
win.setContentSize(size.width, size.height)
|
|
35
|
+
win.center()
|
|
36
|
+
}
|
|
37
|
+
win.setWindowButtonVisibility(false)
|
|
38
|
+
|
|
39
|
+
const uiView = new WebContentsView({
|
|
40
|
+
webPreferences: {
|
|
41
|
+
// separate, non-persistent session: it only serves the local UI; extension icons load via crx://
|
|
42
|
+
session: session.fromPartition('stage-ui'),
|
|
43
|
+
preload: path.join(__dirname, '..', 'preload', 'ui-preload.js'),
|
|
44
|
+
contextIsolation: true,
|
|
45
|
+
sandbox: false,
|
|
46
|
+
nodeIntegration: false,
|
|
47
|
+
},
|
|
48
|
+
})
|
|
49
|
+
uiView.setBackgroundColor('#00000000')
|
|
50
|
+
uiView.setVisible(false)
|
|
51
|
+
win.contentView.addChildView(uiView)
|
|
52
|
+
|
|
53
|
+
ctx.win = win
|
|
54
|
+
ctx.uiView = uiView
|
|
55
|
+
|
|
56
|
+
ctx.tabBounds = () => {
|
|
57
|
+
if (win.isDestroyed()) return { x: 0, y: 0, width: 0, height: 0 }
|
|
58
|
+
const [w, h] = win.getContentSize()
|
|
59
|
+
const top = ctx.bar.pinned ? BAR_HEIGHT : 0
|
|
60
|
+
return { x: 0, y: top, width: w, height: Math.max(0, h - top) }
|
|
61
|
+
}
|
|
62
|
+
ctx.layout = () => {
|
|
63
|
+
if (win.isDestroyed() || uiView.webContents.isDestroyed()) return
|
|
64
|
+
const [w, h] = win.getContentSize()
|
|
65
|
+
uiView.setBounds(ctx.overlay ? { x: 0, y: 0, width: w, height: h } : { x: 0, y: 0, width: w, height: BAR_HEIGHT })
|
|
66
|
+
ctx.tabs && ctx.tabs.layout()
|
|
67
|
+
}
|
|
68
|
+
return win
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = { createWindow, bgColor, BAR_HEIGHT }
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
// Fills in chrome.* constants that electron-chrome-extensions leaves undefined but real
|
|
3
|
+
// Chrome provides. Runs after the library's own preload, in extension pages and service
|
|
4
|
+
// workers only.
|
|
5
|
+
//
|
|
6
|
+
// Why: the Okta Browser Plugin builds a declarativeNetRequest rule with
|
|
7
|
+
// `tabIds: [chrome.tabs.TAB_ID_NONE]`. With the constant undefined, its service worker throws
|
|
8
|
+
// during startup, never answers the popup, and the popup renders blank.
|
|
9
|
+
const { contextBridge } = require('electron')
|
|
10
|
+
|
|
11
|
+
function addMissingConstants() {
|
|
12
|
+
const c = globalThis.chrome
|
|
13
|
+
if (!c) return
|
|
14
|
+
const define = (obj, key, value) => {
|
|
15
|
+
if (!obj || obj[key] !== undefined) return
|
|
16
|
+
try {
|
|
17
|
+
Object.defineProperty(obj, key, { value, enumerable: true })
|
|
18
|
+
} catch {}
|
|
19
|
+
}
|
|
20
|
+
define(c.tabs, 'TAB_ID_NONE', -1)
|
|
21
|
+
define(c.tabs, 'TAB_INDEX_NONE', -1)
|
|
22
|
+
define(c.tabs, 'MAX_CAPTURE_VISIBLE_TAB_CALLS_PER_SECOND', 2)
|
|
23
|
+
define(c.windows, 'WINDOW_ID_NONE', -1)
|
|
24
|
+
define(c.windows, 'WINDOW_ID_CURRENT', -2)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const isExtensionContext = process.type === 'service-worker' || (typeof location !== 'undefined' && location.href.startsWith('chrome-extension://'))
|
|
28
|
+
if (isExtensionContext) {
|
|
29
|
+
try {
|
|
30
|
+
contextBridge.executeInMainWorld({ func: addMissingConstants })
|
|
31
|
+
} catch (e) {
|
|
32
|
+
console.error('[stage] extension-compat preload failed', e)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
// Preload for the overlay UI view (sandbox: false, contextIsolation: true).
|
|
3
|
+
const { contextBridge, ipcRenderer } = require('electron')
|
|
4
|
+
const { injectBrowserAction } = require('electron-chrome-extensions/browser-action')
|
|
5
|
+
|
|
6
|
+
// <browser-action-list> custom element for extension icons
|
|
7
|
+
injectBrowserAction()
|
|
8
|
+
|
|
9
|
+
const send = (channel, ...args) => ipcRenderer.send(channel, ...args)
|
|
10
|
+
|
|
11
|
+
contextBridge.exposeInMainWorld('stage', {
|
|
12
|
+
getState: () => ipcRenderer.invoke('stage:getState'),
|
|
13
|
+
onState(cb) {
|
|
14
|
+
const handler = (_e, state) => cb(state)
|
|
15
|
+
ipcRenderer.on('stage:state', handler)
|
|
16
|
+
return () => ipcRenderer.removeListener('stage:state', handler)
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
activateTab: (id) => send('stage:activateTab', id),
|
|
20
|
+
closeTab: (id) => send('stage:closeTab', id),
|
|
21
|
+
moveTab: (id, toIndex) => send('stage:moveTab', id, toIndex),
|
|
22
|
+
showTabMenu: (id) => send('stage:showTabMenu', id),
|
|
23
|
+
|
|
24
|
+
openOmnibox: (mode) => send('stage:openOmnibox', mode),
|
|
25
|
+
closeOverlay: () => send('stage:closeOverlay'),
|
|
26
|
+
suggest: (query) => ipcRenderer.invoke('stage:suggest', query),
|
|
27
|
+
submitOmnibox: (input, opts) => send('stage:submitOmnibox', input, opts),
|
|
28
|
+
|
|
29
|
+
back: () => send('stage:back'),
|
|
30
|
+
forward: () => send('stage:forward'),
|
|
31
|
+
reload: () => send('stage:reload'),
|
|
32
|
+
|
|
33
|
+
setPinned: (pinned) => send('stage:setPinned', !!pinned),
|
|
34
|
+
setBarHover: (hovering) => send('stage:setBarHover', !!hovering),
|
|
35
|
+
|
|
36
|
+
// Window dragging fallback (see docs): call on mousedown over empty bar background,
|
|
37
|
+
// endWindowDrag on mouseup / pointercancel / blur. Main moves the window by cursor delta.
|
|
38
|
+
startWindowDrag: () => send('stage:startWindowDrag'),
|
|
39
|
+
endWindowDrag: () => send('stage:endWindowDrag'),
|
|
40
|
+
})
|