Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
terkelg
GitHub Repository: terkelg/ramme
Path: blob/master/app/src/main/window.js
106 views
1
const path = require('path')
2
const {app, BrowserWindow, ipcMain} = require('electron')
3
const config = require('./config')
4
const isPlatform = require('./../common/is-platform')
5
6
/**
7
* API
8
*/
9
10
let registeredWindows = {}
11
let currentWindows = {}
12
let win
13
14
// [function] Register Window
15
function registerWindow (name, def) {
16
const url = def.url
17
const useLastState = def.useLastState
18
const fakeUserAgent = def.fakeUserAgent
19
const defaultWindowEvents = def.defaultWindowEvents
20
const isDarkMode = config.get('darkMode') || false
21
const userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
22
23
delete def.useLastState
24
delete def.fakeUserAgent
25
delete def.defaultWindowEvents
26
delete def.url
27
28
def.icon = isPlatform('linux') ? path.join(__dirname, '../assets/icon.png') : null
29
def.title = def.title || app.getName()
30
def.darkTheme = isDarkMode
31
def.backgroundColor = isDarkMode ? '#192633' : '#fff'
32
33
registeredWindows[name] = {
34
'def': def,
35
'options': {
36
url: url,
37
agent: fakeUserAgent ? userAgent : null,
38
defaultWindowEvents: defaultWindowEvents,
39
useLastState: useLastState
40
}
41
}
42
43
return true
44
}
45
46
function intervalFunc () {
47
if (win) {
48
let jscode = "var comp = document.getElementById('back_button');" +
49
'if (comp == null){ ' +
50
"var element = document.getElementsByClassName('_tdn3u'); " +
51
"var para = document.createElement('div'); " +
52
"para.setAttribute('class', '_k0d2z' ); " +
53
"para.setAttribute('id', 'back_button' ); " +
54
"var node = document.createTextNode(''); " +
55
'para.appendChild(node); ' +
56
'element[0].appendChild(para); ' +
57
"var element = document.getElementById('back_button'); " +
58
"var para = document.createElement('a'); " +
59
"para.setAttribute('class', '_ttgfw' ); " +
60
"para.setAttribute('id', 'back_button_a' ); " +
61
"para.setAttribute('onclick','goBack()'); " +
62
'element.appendChild(para); ' +
63
"var element = document.getElementById('back_button_a'); " +
64
"var para = document.createElement('div'); " +
65
"para.setAttribute('class', '_crp8c' ); " +
66
"para.setAttribute('id', 'back_button_div' ); " +
67
"para.setAttribute('style', 'background-color: #262626; " +
68
' border-radius: 50%; ' +
69
" height: 24px; width: 24px;' ); " +
70
'element.appendChild(para); ' +
71
"var para = document.createElement('script'); " +
72
"para.setAttribute('type', 'text/javascript' ); " +
73
"var node = document.createTextNode('function goBack() { " +
74
' window.history.back(); ' +
75
" }'); " +
76
'para.appendChild(node); ' +
77
'document.head.appendChild(para);} '
78
79
win.webContents.executeJavaScript(jscode)
80
}
81
}
82
83
setInterval(intervalFunc, 50)
84
85
// [function] Open Window
86
function openWindow (name) {
87
if (registeredWindows[name]) {
88
const wanted = registeredWindows[name]
89
90
// Restore window size if useLastState
91
if (wanted.options.useLastState) {
92
const lastWindowState = config.get(name + 'LastState')
93
if (lastWindowState) {
94
wanted.def.x = lastWindowState.x
95
wanted.def.y = lastWindowState.y
96
wanted.def.width = lastWindowState.width
97
wanted.def.height = lastWindowState.height
98
}
99
}
100
101
// Hide window until it is ready
102
wanted.def.show = false
103
104
win = new BrowserWindow(wanted.def)
105
106
if (win) {
107
if (wanted.options.agent) {
108
win.webContents.setUserAgent(wanted.options.agent)
109
}
110
111
// Load URL
112
if (wanted.options.url) {
113
win.loadURL(wanted.options.url)
114
}
115
116
// Add save size event
117
if (wanted.options.useLastState) {
118
win.on('close', e => {
119
config.set(name + 'LastState', win.getBounds())
120
})
121
}
122
123
// Add de-reference event
124
win.on('closed', e => {
125
delete currentWindows[name]
126
})
127
128
// Show window when ready
129
win.once('ready-to-show', () => {
130
win.show()
131
})
132
133
// Setup reference
134
currentWindows[name] = win
135
136
return win
137
}
138
}
139
}
140
141
// [function] Get window
142
function getWindow (name) {
143
if (currentWindows[name]) {
144
return currentWindows[name]
145
}
146
}
147
148
// [function] Window(s) is/are open
149
function isOpen (name) {
150
if (name && currentWindows[name]) {
151
return true
152
}
153
}
154
155
// [function] Count open windows
156
function countOpen () {
157
return Object.keys(currentWindows).length
158
}
159
160
// [function] Close window
161
function closeWindow (name) {
162
if (currentWindows[name]) {
163
currentWindows[name].close()
164
return true
165
}
166
}
167
168
// [function] LoadURL
169
function loadURL (name, url) {
170
if (currentWindows[name] && url) {
171
currentWindows[name].loadURL(url)
172
}
173
}
174
175
// [function] For each window
176
function each (func) {
177
if (countOpen() > 0) {
178
for (var name in currentWindows) {
179
if (currentWindows.hasOwnProperty(name)) {
180
func(currentWindows[name])
181
}
182
}
183
return true
184
}
185
}
186
187
/**
188
* IPC
189
*/
190
191
ipcMain.on('window-open', function (event, name) {
192
openWindow(name)
193
})
194
195
ipcMain.on('window-close', function (event, name) {
196
closeWindow(name)
197
})
198
199
ipcMain.on('window-load', function (event, name, url) {
200
console.log('Hope')
201
loadURL(name, url)
202
})
203
204
/**
205
* Exposed Functions
206
*/
207
208
module.exports = {
209
register: registerWindow,
210
open: openWindow,
211
get: getWindow,
212
isOpen: isOpen,
213
countOpen: countOpen,
214
close: closeWindow,
215
load: loadURL,
216
each: each
217
}
218
219