@@ -14,6 +14,41 @@ interface StatsRes {
14
14
message : string
15
15
}
16
16
17
+ const setupExtension = async ( ) => {
18
+ let API_URL = process . env . NODE_ENV == "development" ? process . env . LOCAL_API_URL : process . env . API_URL ;
19
+ let SITE_URL = process . env . NODE_ENV == "development" ? process . env . LOCAL_SITE_URL : process . env . SITE_URL ;
20
+
21
+ await fetch ( `https://${ API_URL } /api/install` , {
22
+ headers : {
23
+ "Content-Type" : "application/json" ,
24
+ "install-key" : process . env . HASHED_INSTALL_KEY
25
+ }
26
+ } ) . then ( async ( response ) => {
27
+ if ( response ?. ok ) {
28
+ console . log ( "Authenticated sucessfully..." ) ;
29
+
30
+ let jsonRes :Promise < InstallRes > = await response . json ( ) ;
31
+ let clientID = ( await jsonRes ) . clientID ;
32
+ let uninstallKey = ( await jsonRes ) . uninstallKey ;
33
+
34
+ // Store client ID + uninstall key locally
35
+ browser . storage . local . set ( { "clientID" : clientID } ) ;
36
+ browser . storage . local . set ( { "uninstallKey" : uninstallKey } ) ;
37
+
38
+ // Setup uninstall URL
39
+ browser . runtime . setUninstallURL ( `https://${ SITE_URL } /uninstall?clientID=${ clientID } &uninstallKey=${ uninstallKey } ` ) ;
40
+
41
+ console . log ( "Extension fully set up" ) ;
42
+ } else {
43
+ console . warn ( `There was an error when trying to authenticate install with the server...` )
44
+ }
45
+ } )
46
+ . catch ( error => {
47
+ console . log ( `An error occured authenticating extension: ${ error } ` )
48
+ } ) ;
49
+
50
+ }
51
+
17
52
// Setup alarms
18
53
browser . alarms . create ( "sendPageUpdates" , { "periodInMinutes" : 15 } ) ;
19
54
@@ -22,35 +57,18 @@ browser.runtime.onInstalled.addListener(async function (details) {
22
57
23
58
if ( details . reason == "install" ) {
24
59
console . log ( "Setting up extension..." ) ;
25
-
26
- if ( config . apiEnabled || process . env . NODE_ENV == "production" ) {
27
-
28
- let API_URL = process . env . NODE_ENV == "development" ? process . env . LOCAL_API_URL : process . env . API_URL ;
29
-
30
- let response = await fetch ( `${ API_URL } /api/install` , {
31
- headers : {
32
- "Content-Type" : "application/json" ,
33
- "install-key" : process . env . HASHED_INSTALL_KEY
34
- }
35
- } ) ;
36
-
37
- if ( response ?. ok ) {
38
- let jsonRes :Promise < InstallRes > = await response . json ( ) ;
39
- let clientID = ( await jsonRes ) . clientID ;
40
- let uninstallKey = ( await jsonRes ) . uninstallKey ;
41
-
42
- // Store client ID + uninstall key locally
43
- browser . storage . local . set ( { "clientID" : clientID } ) ;
44
- browser . storage . local . set ( { "uninstallKey" : uninstallKey } ) ;
45
-
46
- // Setup uninstall URL
47
- let uninstallURL = process . env . NODE_ENV == "development" ? process . env . LOCAL_SITE_URL : process . env . SITE_URL ;
48
- browser . runtime . setUninstallURL ( `${ uninstallURL } /uninstall?clientID=${ clientID } &uninstallKey=${ uninstallKey } ` ) ;
49
-
50
- } else {
51
- console . warn ( `There was an error when trying to authenticate install with the server...` )
60
+
61
+ setupExtension ( ) ;
62
+
63
+ } else if ( details . reason == "update" ) {
64
+
65
+ // Check if existing users are still authenticated after an update
66
+ if ( process . env . NODE_ENV == "production" ) {
67
+ let { clientID } = await browser . storage . local . get ( "clientID" ) ;
68
+
69
+ if ( ! clientID ) {
70
+ setupExtension ( ) ;
52
71
}
53
-
54
72
}
55
73
56
74
}
@@ -133,7 +151,7 @@ const sendPageUpdates = async () => {
133
151
134
152
let { clientID } = await browser . storage . local . get ( "clientID" ) ;
135
153
136
- let response = await fetch ( `${ API_URL } /api/updateStats` , {
154
+ await fetch ( `https:// ${ API_URL } /api/updateStats` , {
137
155
method : "POST" ,
138
156
headers : {
139
157
"Accept" : "application/json" ,
@@ -142,19 +160,21 @@ const sendPageUpdates = async () => {
142
160
"client-id" : clientID
143
161
} ,
144
162
body : JSON . stringify ( pageChangeData )
145
- } )
146
-
147
- if ( response ?. ok ) {
148
- let jsonRes :Promise < StatsRes > = await response . json ( ) ;
149
- console . log ( ( await jsonRes ) . message ) ;
150
-
151
- console . log ( "Clearing page change data..." ) ;
152
- await clearPageChangeStore ( ) ;
153
- } else {
154
- console . warn ( "An error occurred sending page change data to server..." ) ;
155
- console . log ( "A successful update will be attempted later..." ) ;
156
- }
163
+ } ) . then ( async ( response ) => {
164
+ if ( response ?. ok ) {
165
+ let jsonRes :Promise < StatsRes > = await response . json ( ) ;
166
+ console . log ( ( await jsonRes ) . message ) ;
157
167
168
+ console . log ( "Clearing page change data..." ) ;
169
+ await clearPageChangeStore ( ) ;
170
+ } else {
171
+ console . warn ( "An error occurred sending page change data to server..." ) ;
172
+ console . log ( "A successful update will be attempted later..." ) ;
173
+ }
174
+ } )
175
+ . catch ( error => {
176
+ console . log ( `An error occured updating extension stats on the server: ${ error } ` )
177
+ } ) ;
158
178
}
159
179
160
180
}
0 commit comments