@@ -7,38 +7,48 @@ class MVPNWasm {
7
7
this . _guardianOverrideEndpoints = { } ;
8
8
9
9
this . _fxaOverrideEndpoints = { } ;
10
+
11
+ this . instance = null ;
10
12
}
11
13
12
- init ( ) {
13
- const qmlcanvas = document . querySelector ( '#qmlcanvas ' ) ;
14
- const controlcanvas = document . querySelector ( '#controlcanvas ' ) ;
14
+ async init ( ) {
15
+ const spinner = document . querySelector ( '#qtspinner ' ) ;
16
+ const screen = document . querySelector ( '#screen ' ) ;
15
17
const status = document . querySelector ( '#qtstatus' ) ;
18
+ const controlcanvas = document . querySelector ( '#controlcanvas' ) ;
16
19
const bodyClassList = document . body . classList ;
17
20
18
- const qtLoader = QtLoader ( {
19
- canvasElements : [ controlcanvas , qmlcanvas ] ,
20
- showLoader : function ( loaderStatus ) {
21
- status . textContent = loaderStatus + '...' ;
22
- } ,
23
- showError : function ( errorText ) {
24
- bodyClassList . remove ( 'wasm-loading' ) ;
25
- status . textContent = errorText ;
26
- bodyClassList . add ( 'wasm-loading-error' ) ;
27
- } ,
28
- showExit : function ( ) {
29
- status . textContent = 'Application exit' ;
30
- if ( qtLoader . exitCode !== undefined )
31
- status . textContent += ' with code ' + qtLoader . exitCode ;
32
- if ( qtLoader . exitText !== undefined )
33
- status . textContent += ' (' + qtLoader . exitText + ')' ;
34
- } ,
35
- showCanvas : function ( ) {
36
- bodyClassList . remove ( 'wasm-loading' ) ;
37
- bodyClassList . add ( 'wasm-loaded' ) ;
38
- status . textContent = '' ;
39
- } ,
40
- } ) ;
41
- qtLoader . loadEmscriptenModule ( 'mozillavpn' ) ;
21
+ const showUi = ( ui ) => {
22
+ [ spinner , screen ] . forEach ( element => element . style . display = 'none' ) ;
23
+ if ( screen === ui )
24
+ screen . style . position = 'default' ;
25
+ ui . style . display = 'block' ;
26
+ }
27
+
28
+ try {
29
+ showUi ( spinner ) ;
30
+ status . innerHTML = 'Loading...' ;
31
+
32
+ this . instance = await qtLoad ( {
33
+ qt : {
34
+ onLoaded : ( ) => showUi ( screen ) ,
35
+ onExit : exitData =>
36
+ {
37
+ status . innerHTML = 'Application exit' ;
38
+ status . innerHTML +=
39
+ exitData . code !== undefined ? ` with code ${ exitData . code } ` : '' ;
40
+ status . innerHTML +=
41
+ exitData . text !== undefined ? ` (${ exitData . text } )` : '' ;
42
+ showUi ( spinner ) ;
43
+ } ,
44
+ entryFunction : window . createQtAppInstance ,
45
+ containerElements : [ screen ] ,
46
+ }
47
+ } ) ;
48
+ } catch ( e ) {
49
+ console . error ( e ) ;
50
+ console . error ( e . stack ) ;
51
+ }
42
52
43
53
const preset = document . querySelector ( '#preset' ) ;
44
54
for ( let p of MVPNPresets ) {
@@ -73,7 +83,7 @@ class MVPNWasm {
73
83
url == 'https://archive.mozilla.org/pub/vpn/speedtest/50m.data' ) {
74
84
// 50mb of data is too much to be handled in the browser.
75
85
setTimeout (
76
- ( ) => Module . mzNetworkResponse ( id , JSON . stringify ( {
86
+ ( ) => this . instance . mzNetworkResponse ( id , JSON . stringify ( {
77
87
status : 200 ,
78
88
body : btoa ( String . fromCharCode . apply ( null , new Uint8Array ( 1024 ) ) )
79
89
} ) ) ,
@@ -83,14 +93,14 @@ class MVPNWasm {
83
93
84
94
if ( ! obj ) {
85
95
fetch ( url )
86
- . then ( async resp => Module . mzNetworkResponse ( id , JSON . stringify ( {
96
+ . then ( async resp => this . instance . mzNetworkResponse ( id , JSON . stringify ( {
87
97
status : resp . status ,
88
98
body : btoa ( String . fromCharCode . apply (
89
99
null , new Uint8Array ( await resp . arrayBuffer ( ) ) ) )
90
100
} ) ) )
91
101
. catch ( ( ) => {
92
102
console . error ( 'Unable to fetch content for URL' , url ) ;
93
- Module . mzNetworkResponse ( id , JSON . stringify ( { status : 0 , body : '' } ) ) ;
103
+ this . instance . mzNetworkResponse ( id , JSON . stringify ( { status : 0 , body : '' } ) ) ;
94
104
} ) ;
95
105
return ;
96
106
}
@@ -106,7 +116,7 @@ class MVPNWasm {
106
116
}
107
117
108
118
setTimeout ( ( ) => {
109
- Module . mzNetworkResponse (
119
+ this . instance . mzNetworkResponse (
110
120
id ,
111
121
JSON . stringify (
112
122
{ status : obj . status , body : btoa ( JSON . stringify ( obj . body ) ) } ) ) ;
0 commit comments