1+ const {
2+ shell,
3+ app,
4+ net,
5+ session,
6+ BrowserWindow,
7+ } = require ( 'electron' ) ;
8+ const url = require ( 'url' ) ;
9+ const shorcuts = require ( './shortcuts' ) ;
10+ const menu = require ( './menu' ) ;
11+
12+ const HOME = 'https://www.figma.com'
13+ const winOptions = {
14+ width : 1200 ,
15+ height : 900 ,
16+ frame : false ,
17+ webPreferences : {
18+ nodeIntegration : false ,
19+ 'web-security' : false ,
20+ webgl : true ,
21+ experimentalFeatures : true ,
22+ experimentalCanvasFeatures : true ,
23+ zoomFactor : 0.7
24+ }
25+ } ;
26+
27+ app . on ( 'ready' , ( ) => {
28+ let window = new BrowserWindow ( winOptions ) ;
29+
30+ window . setMenuBarVisibility ( false ) ;
31+ window . loadURL ( HOME ) ;
32+
33+ window . webContents . on ( 'will-navigate' , ( event , url ) => {
34+ parts = url . split ( "/" ) ;
35+ if ( parts [ 0 ] + "//" + parts [ 2 ] != HOME ) {
36+ event . preventDefault ( )
37+ shell . openExternal ( url )
38+ } ;
39+ } ) ;
40+
41+ shorcuts ( window ) ;
42+ menu ( window ) ;
43+
44+
45+ window . webContents . on ( 'will-navigate' , ( event , newUrl ) => {
46+ const currentUrl = event . sender . getURL ( ) ;
47+
48+ if ( newUrl === currentUrl ) {
49+ window . reload ( ) ;
50+
51+ event . preventDefault ( ) ;
52+ return ;
53+ }
54+
55+ const from = url . parse ( currentUrl ) ;
56+ const to = url . parse ( newUrl ) ;
57+
58+ if ( from . pathname === '/login' ) {
59+ window . reload ( ) ;
60+ return ;
61+ }
62+
63+ if ( to . pathname === '/logout' ) {
64+ net . request ( `${ HOME } /logout` ) . on ( 'response' , response => {
65+ response . on ( 'data' , chunk => { } ) ;
66+ response . on ( 'error' , err => {
67+ console . log ( 'Request error: ' , err ) ;
68+ } ) ;
69+ response . on ( 'end' , ( ) => {
70+ console . log ( 'response.statusCode: ' , response . statusCode ) ;
71+ if ( response . statusCode >= 200 && response . statusCode <= 299 ) {
72+
73+ session . defaultSession . cookies . flushStore ( ( ) => {
74+ const reload = ( ) => app . relaunch ( {
75+ args : process . argv . slice ( 1 ) . concat ( [ '--reset' ] )
76+ } ) ;
77+
78+ app . on ( 'will-quit' , reload ) ;
79+ app . quit ( ) ;
80+ } ) ;
81+ }
82+ } ) ;
83+ } ) . end ( ) ; ;
84+
85+ event . preventDefault ( ) ;
86+ return ;
87+ }
88+ } ) ;
89+
90+ // window.webContents.on('did-navigate', (event) => {
91+ // console.log('did-navigate event args:', event.sender.getURL());
92+ // });
93+
94+ window . webContents . on ( 'new-window' , ( ...args ) => {
95+ console . log ( 'new-window event args:' , args ) ;
96+ } ) ;
97+
98+ window . on ( 'closed' , ( ) => {
99+ window = null ;
100+ } ) ;
101+ } ) ;
102+
103+ app . on ( 'window-all-closed' , ( ) => {
104+
105+ if ( process . platform !== 'darwin' ) {
106+ app . quit ( ) ;
107+ }
108+ } ) ;
0 commit comments