1
- import { app , BrowserWindow , globalShortcut , webFrameMain } from ' electron' ;
2
- import { ipcMain as ipc } from ' electron-better-ipc' ;
3
- import serve from ' electron-serve' ;
4
- import { readFile } from ' fs/promises' ;
5
- import fetch from 'node -fetch' ;
6
- import { join } from ' path' ;
7
- import { createWindow } from ' ./helpers' ;
8
- import { omitSecurityHeadersAndBlockAds } from ' ./helpers/web-requests' ;
9
-
10
- const isProd : boolean = process . env . NODE_ENV === ' production' ;
11
- const isLinux = process . platform === ' linux' ;
1
+ import { app , BrowserWindow , globalShortcut , webFrameMain } from " electron" ;
2
+ import { ipcMain as ipc } from " electron-better-ipc" ;
3
+ import serve from " electron-serve" ;
4
+ import { readFile } from " fs/promises" ;
5
+ import fetch from "cross -fetch" ;
6
+ import { join } from " path" ;
7
+ import { createWindow } from " ./helpers" ;
8
+ import { omitSecurityHeadersAndBlockAds } from " ./helpers/web-requests" ;
9
+
10
+ const isProd : boolean = process . env . NODE_ENV === " production" ;
11
+ const isLinux = process . platform === " linux" ;
12
12
13
13
const port = process . argv [ 2 ] ;
14
14
const devURL = `http://localhost:${ port } ` ;
15
15
16
16
let mainWindow : BrowserWindow | undefined ;
17
17
let experimentalVideoCSS = false ;
18
18
let forceDetachKeybindActive = false ;
19
- const videoCSSOnScript = 'document.documentElement.classList.add("flot-video");' ;
20
- const videoCSSOffScript = 'document.documentElement.classList.remove("flot-video");' ;
19
+ const videoCSSOnScript =
20
+ 'document.documentElement.classList.add("flot-video");' ;
21
+ const videoCSSOffScript =
22
+ 'document.documentElement.classList.remove("flot-video");' ;
21
23
22
24
if ( isLinux ) {
23
25
// https://github.com/electron/electron/issues/25153#issuecomment-843688494
24
- app . commandLine . appendSwitch ( ' use-gl' , ' desktop' ) ;
26
+ app . commandLine . appendSwitch ( " use-gl" , " desktop" ) ;
25
27
}
26
28
27
29
if ( isProd ) {
28
- serve ( { directory : ' app' } ) ;
30
+ serve ( { directory : " app" } ) ;
29
31
} else {
30
- app . setPath ( ' userData' , `${ app . getPath ( ' userData' ) } (development)` ) ;
32
+ app . setPath ( " userData" , `${ app . getPath ( " userData" ) } (development)` ) ;
31
33
}
32
34
33
35
( async ( ) => {
34
36
await app . whenReady ( ) ;
35
37
36
- globalShortcut . register ( ' CmdOrCtrl+Alt+I' , ( ) => {
38
+ globalShortcut . register ( " CmdOrCtrl+Alt+I" , ( ) => {
37
39
mainWindow ?. setIgnoreMouseEvents ( ! forceDetachKeybindActive ) ;
38
40
forceDetachKeybindActive = ! forceDetachKeybindActive ;
39
41
} ) ;
40
42
41
43
const embedCssFilePromise = isProd
42
- ? readFile ( join ( __dirname , ' css' , ' embed.css' ) , { encoding : ' utf8' } )
44
+ ? readFile ( join ( __dirname , " css" , " embed.css" ) , { encoding : " utf8" } )
43
45
: fetch ( `${ devURL } /css/embed.css` ) . then ( ( r ) => r . text ( ) ) ;
44
46
45
47
const embedCss = embedCssFilePromise . catch ( ( e ) => {
46
48
console . error ( e ) ;
47
- return '' ;
49
+ return "" ;
48
50
} ) ;
49
51
50
52
await new Promise ( ( r ) => setTimeout ( r , isLinux ? 750 : 350 ) ) ; // required otherwise transparency won't work consistently cross-platform...
51
53
52
- mainWindow = createWindow ( ' main' , {
54
+ mainWindow = createWindow ( " main" , {
53
55
transparent : true ,
54
- title : ' Flōt' ,
56
+ title : " Flōt" ,
55
57
titleBarStyle : undefined ,
56
58
trafficLightPosition : undefined ,
57
59
skipTaskbar : false ,
@@ -66,7 +68,7 @@ if (isProd) {
66
68
minWidth : 300 ,
67
69
maxHeight : 1000 ,
68
70
maxWidth : 1000 ,
69
- icon : join ( __dirname , ' images' , ' logo' , ' 256x256.png' ) ,
71
+ icon : join ( __dirname , " images" , " logo" , " 256x256.png" ) ,
70
72
alwaysOnTop : true ,
71
73
hasShadow : false ,
72
74
webPreferences : {
@@ -79,17 +81,34 @@ if (isProd) {
79
81
mainWindow . setVisibleOnAllWorkspaces ( true ) ;
80
82
81
83
mainWindow . webContents . on (
82
- 'did-fail-provisional-load' ,
83
- ( event , code , desc , validatedUrl , isMainFrame , frameProcessId , frameRoutingId ) => {
84
- if ( mainWindow ) ipc . callRenderer ( mainWindow , 'iframe-load-failure' , validatedUrl ) ;
84
+ "did-fail-provisional-load" ,
85
+ (
86
+ event ,
87
+ code ,
88
+ desc ,
89
+ validatedUrl ,
90
+ isMainFrame ,
91
+ frameProcessId ,
92
+ frameRoutingId
93
+ ) => {
94
+ if ( mainWindow )
95
+ ipc . callRenderer ( mainWindow , "iframe-load-failure" , validatedUrl ) ;
85
96
}
86
97
) ;
87
98
88
99
mainWindow . webContents . on (
89
- 'did-frame-navigate' ,
90
- async ( event , url , status , statusText , isMainFrame , frameProcessId , frameRoutingId ) => {
100
+ "did-frame-navigate" ,
101
+ async (
102
+ event ,
103
+ url ,
104
+ status ,
105
+ statusText ,
106
+ isMainFrame ,
107
+ frameProcessId ,
108
+ frameRoutingId
109
+ ) => {
91
110
if ( isMainFrame ) return ;
92
- if ( url === ' about:blank' ) return ;
111
+ if ( url === " about:blank" ) return ;
93
112
94
113
const frame = webFrameMain . fromId ( frameProcessId , frameRoutingId ) ;
95
114
@@ -121,55 +140,62 @@ if (isProd) {
121
140
}
122
141
) ;
123
142
124
- mainWindow . webContents . on ( 'did-navigate-in-page' , async ( event , url , isMainFrame , frameProcessId , frameRoutingId ) => {
125
- if ( isMainFrame ) return ;
126
- if ( url === 'about:blank' ) return ;
143
+ mainWindow . webContents . on (
144
+ "did-navigate-in-page" ,
145
+ async ( event , url , isMainFrame , frameProcessId , frameRoutingId ) => {
146
+ if ( isMainFrame ) return ;
147
+ if ( url === "about:blank" ) return ;
127
148
128
- const frame = webFrameMain . fromId ( frameProcessId , frameRoutingId ) ;
149
+ const frame = webFrameMain . fromId ( frameProcessId , frameRoutingId ) ;
129
150
130
- if ( frame ) {
131
- frame . executeJavaScript ( `parent.postMessage({key: 'location', msg: location.href}, "*")` ) ;
151
+ if ( frame ) {
152
+ frame . executeJavaScript (
153
+ `parent.postMessage({key: 'location', msg: location.href}, "*")`
154
+ ) ;
155
+ }
132
156
}
133
- } ) ;
157
+ ) ;
134
158
135
- mainWindow . webContents . setWindowOpenHandler ( ( ) => ( { action : ' deny' } ) ) ;
159
+ mainWindow . webContents . setWindowOpenHandler ( ( ) => ( { action : " deny" } ) ) ;
136
160
137
161
if ( isProd ) {
138
- await mainWindow . loadURL ( ' app://./index.html' ) ;
162
+ await mainWindow . loadURL ( " app://./index.html" ) ;
139
163
140
164
app
141
165
. whenReady ( )
142
- . then ( ( ) => import ( ' electron-updater' ) )
166
+ . then ( ( ) => import ( " electron-updater" ) )
143
167
. then ( ( { autoUpdater } ) => autoUpdater . checkForUpdatesAndNotify ( ) )
144
- . catch ( ( e ) => console . error ( ' Failed check updates:' , e ) ) ;
168
+ . catch ( ( e ) => console . error ( " Failed check updates:" , e ) ) ;
145
169
} else {
146
170
await mainWindow . loadURL ( `${ devURL } /` ) ;
147
171
// mainWindow.webContents.openDevTools({ mode: 'detach', activate: false });
148
172
}
149
173
} ) ( ) ;
150
174
151
- ipc . answerRenderer ( ' please-enable-video-css' , ( ) => {
175
+ ipc . answerRenderer ( " please-enable-video-css" , ( ) => {
152
176
getFlotEmbed ( ) ?. executeJavaScript ( videoCSSOnScript ) ;
153
177
experimentalVideoCSS = true ;
154
178
} ) ;
155
- ipc . answerRenderer ( ' please-disable-video-css' , ( ) => {
179
+ ipc . answerRenderer ( " please-disable-video-css" , ( ) => {
156
180
getFlotEmbed ( ) ?. executeJavaScript ( videoCSSOffScript ) ;
157
181
experimentalVideoCSS = false ;
158
182
} ) ;
159
- ipc . answerRenderer ( 'please-detach' , ( ) => mainWindow ?. setIgnoreMouseEvents ( true ) ) ;
160
- ipc . answerRenderer ( 'please-attach' , ( ) => {
183
+ ipc . answerRenderer ( "please-detach" , ( ) =>
184
+ mainWindow ?. setIgnoreMouseEvents ( true )
185
+ ) ;
186
+ ipc . answerRenderer ( "please-attach" , ( ) => {
161
187
if ( ! forceDetachKeybindActive ) mainWindow ?. setIgnoreMouseEvents ( false ) ;
162
188
} ) ;
163
- ipc . answerRenderer ( ' please-quit' , ( ) => app . quit ( ) ) ;
189
+ ipc . answerRenderer ( " please-quit" , ( ) => app . quit ( ) ) ;
164
190
165
- app . on ( ' window-all-closed' , ( ) => app . quit ( ) ) ;
166
- app . on ( ' will-quit' , ( ) => globalShortcut . unregisterAll ( ) ) ;
191
+ app . on ( " window-all-closed" , ( ) => app . quit ( ) ) ;
192
+ app . on ( " will-quit" , ( ) => globalShortcut . unregisterAll ( ) ) ;
167
193
168
194
function getFlotEmbed ( ) {
169
195
const frames = mainWindow ?. webContents . mainFrame . frames ?? [ ] ;
170
196
171
197
// Some websites seem to cause the embed frame to be renamed...not sure how, it's
172
198
// supposed to be a readonly property, but ah well, if this happens, just try the
173
199
// using the first frame
174
- return frames . find ( ( f ) => f . name === ' flot-embed' ) ?? frames [ 0 ] ;
200
+ return frames . find ( ( f ) => f . name === " flot-embed" ) ?? frames [ 0 ] ;
175
201
}
0 commit comments