1+ const form = document . querySelector ( 'form' ) ;
2+ const error = document . querySelector ( '.red' ) ;
3+ const logs = document . querySelector ( '.logs' ) ;
4+ const controls = document . querySelector ( '.controls' ) ;
5+
6+ form . addEventListener ( 'submit' , ( e ) => {
7+ e . preventDefault ( ) ;
8+
9+ fetch ( '/form' , {
10+ method : 'POST' ,
11+ mode : 'cors' ,
12+ cache : 'no-cache' ,
13+ credentials : 'same-origin' ,
14+ headers : {
15+ 'Content-Type' : 'application/json' ,
16+ } ,
17+ redirect : 'follow' ,
18+ referrerPolicy : 'no-referrer' ,
19+ body : JSON . stringify ( {
20+ url : e . srcElement . children . url . value ,
21+ } ) ,
22+ } )
23+ . then ( ( res ) => res . json ( ) )
24+ . then ( ( res ) => {
25+ if ( ! res . error ) {
26+ if ( res . port ) {
27+ error . textContent = '' ;
28+ form . remove ( ) ;
29+ logs . classList . remove ( 'hidden' ) ;
30+ controls . classList . remove ( 'hidden' ) ;
31+
32+ var win = window . open ( `http://localhost:${ res . port } /${ e . srcElement . children . filename . value } ` , 'popup' , `left=${ window . screen . width } ,top=${ window . screen . height } ,width=600,height=700` ) ;
33+ var done = false ;
34+ win . focus ( ) ;
35+
36+ window . onfocus = ( ) => {
37+ document . documentElement . click ( ) ;
38+ } ;
39+
40+ window . onbeforeunload = ( e ) => {
41+ win . close ( ) ;
42+ } ;
43+
44+ const close = setInterval ( ( ) => {
45+ if ( win . closed && ! done ) {
46+ clearInterval ( close ) ;
47+
48+ ws . send (
49+ JSON . stringify ( {
50+ action : 'stop' ,
51+ } )
52+ ) ;
53+
54+ location . reload ( ) ;
55+ }
56+ } , 1 ) ;
57+
58+ ws . onmessage = ( e ) => {
59+ const data = JSON . parse ( e . data ) ;
60+
61+ if ( data . type == 'log' || data . type == 'error' ) {
62+ const log = document . createElement ( 'div' ) ;
63+ log . textContent = data . msg ;
64+ log . classList = data . type ;
65+ logs . appendChild ( log ) ;
66+
67+ window . scrollTo ( 0 , document . body . offsetHeight ) ;
68+ } else if ( data . type == 'action' ) {
69+ if ( data . action === 'closed' ) {
70+ win . close ( ) ;
71+
72+ logs . classList . add ( 'hidden' ) ;
73+ controls . classList . add ( 'hidden' ) ;
74+ }
75+ }
76+ } ;
77+
78+ document . documentElement . addEventListener ( 'click' , ( e ) => {
79+ win . focus ( ) ;
80+ } ) ;
81+
82+ controls . querySelector ( '#stop' ) . addEventListener ( 'click' , ( e ) => {
83+ ws . send (
84+ JSON . stringify ( {
85+ action : 'stop' ,
86+ } )
87+ ) ;
88+
89+ location . reload ( ) ;
90+ } ) ;
91+
92+ controls . querySelector ( '#done' ) . addEventListener ( 'click' , ( e ) => {
93+ done = true ;
94+
95+ ws . send (
96+ JSON . stringify ( {
97+ action : 'done' ,
98+ } )
99+ ) ;
100+
101+ error . textContent = 'Your website has been downloaded, check the /downloads folder inside the blaze server directory' ;
102+ } ) ;
103+ } else {
104+ error . textContent = 'The server did not provide a port' ;
105+ }
106+ } else {
107+ error . textContent = res . errorMsg ;
108+ }
109+ } ) ;
110+
111+ const ws = new WebSocket ( window . origin . replace ( 'http' , 'ws' ) . replace ( 'https' , 'wss' ) ) ;
112+
113+ ws . onerror = ( e ) => {
114+ error . textContent = 'Failed to connect to server' ;
115+ } ;
116+ } ) ;
0 commit comments