Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
terkelg
GitHub Repository: terkelg/ramme
Path: blob/master/app/src/main/index.js
106 views
1
const path = require('path')
2
const fs = require('fs')
3
const {app, Menu, shell, ipcMain} = require('electron')
4
const tray = require('./tray')
5
const appMenu = require('./menus')
6
7
const updater = require('./updater')
8
const analytics = require('./analytics')
9
const isPlatform = require('./../common/is-platform')
10
const window = require('./window.js')
11
12
const renderer = {
13
styles: '../../dist/renderer/styles',
14
js: '../../dist/renderer/js'
15
}
16
17
/**
18
* Singleton
19
*/
20
let shouldQuit = app.makeSingleInstance(() => {
21
// Someone tried to run a second instance, we should focus our window.
22
window.each(win => {
23
if (win) {
24
if (!win.isVisible()) {
25
win.show()
26
} else {
27
if (win.isMinimized()) win.restore()
28
win.focus()
29
}
30
}
31
})
32
})
33
34
if (shouldQuit) {
35
app.quit()
36
}
37
38
/**
39
* Register Windows
40
*/
41
42
window.register('main', {
43
url: 'https://www.instagram.com/?utm_source=ig_lite',
44
useLastState: true,
45
fakeUserAgent: true,
46
defaultWindowEvents: false,
47
show: false,
48
minHeight: 480,
49
minWidth: 380,
50
maxWidth: 550,
51
maximizable: false,
52
fullscreenable: false,
53
titleBarStyle: 'hidden-inset',
54
autoHideMenuBar: true,
55
webPreferences: {
56
preload: path.join(__dirname, renderer.js, 'index.js'),
57
nodeIntegration: false
58
}
59
})
60
61
window.register('preload', {
62
url: path.join('file://', __dirname, '../renderer/html/preload.html'),
63
useLastState: true,
64
width: 200,
65
height: 400,
66
resizable: false,
67
fullscreenable: false,
68
maximizable: false,
69
frame: false
70
})
71
72
/**
73
* Kick off!
74
*/
75
app.on('ready', () => {
76
// Open preload window
77
window.open('preload')
78
79
// Open main window
80
let mainWindow = window.open('main')
81
setupWindowEvents(mainWindow)
82
83
// Create menus
84
Menu.setApplicationMenu(appMenu)
85
tray.createTray(mainWindow)
86
87
// Update and analytics
88
updater.init(mainWindow)
89
analytics.init()
90
91
// Setup events
92
setupWebContentsEvents(mainWindow.webContents)
93
})
94
95
app.on('activate', () => {
96
window.get('main').show()
97
})
98
99
app.on('before-quit', () => {
100
shouldQuit = true
101
})
102
103
/**
104
* Communicate with renderer process (web page)
105
*/
106
ipcMain.on('back', (e, arg) => {
107
let page = e.sender.webContents
108
if (page.canGoBack()) {
109
page.goBack()
110
}
111
})
112
113
ipcMain.on('home', (e, arg) => {
114
let page = e.sender.webContents
115
page.loadURL('https://www.instagram.com/?utm_source=ig_lite', {
116
userAgent: 'Mozilla/5.0 (Linux; Android 8.0.0; Android SDK built for x86 Build/OSR1.170901.043; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.158 Mobile Safari/537.36 InstagramLite 1.0.0.0.145 Android (26/8.0.0; 420dpi; 1080x1794; Google/google; Android SDK built for x86; generic_x86; ranchu; en_US; 115357035)'
117
})
118
page.clearHistory()
119
})
120
121
/**
122
* setupWindowEvents
123
*/
124
function setupWindowEvents (win) {
125
win.on('close', e => {
126
if (!shouldQuit) {
127
e.preventDefault()
128
129
if (isPlatform('macOS')) {
130
app.hide()
131
} else {
132
win.hide()
133
}
134
}
135
})
136
137
win.on('page-title-updated', e => {
138
e.preventDefault()
139
})
140
}
141
142
/**
143
* mainWindowEvents
144
*/
145
function setupWebContentsEvents (page) {
146
page.on('did-navigate-in-page', (event, arg) => {
147
// Get back menu item and disable/enable it
148
const menuBackBtn = appMenu.items[1].submenu.items[0]
149
menuBackBtn.enabled = page.canGoBack()
150
// Notify back-button in sidebar about the state change
151
page.send('set-button-state', menuBackBtn.enabled)
152
})
153
154
// Inject styles when DOM is ready
155
page.on('dom-ready', () => {
156
page.insertCSS(fs.readFileSync(path.join(__dirname, renderer.styles, 'app.css'), 'utf8'))
157
page.insertCSS(fs.readFileSync(path.join(__dirname, renderer.styles, 'theme-dark/main.css'), 'utf8'))
158
159
window.close('preload') // Close preload window
160
window.get('main').show() // Show main window
161
})
162
163
// Open links in external applications
164
page.on('new-window', (e, url) => {
165
e.preventDefault()
166
shell.openExternal(url)
167
})
168
}
169
170