Skip to content

Commit 7a4d757

Browse files
committed
Fixed extension setup process
1 parent e5c2f5a commit 7a4d757

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

public/chrome_manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"name": "tidytube - Declutter Youtube",
44
"short_name": "tidytube",
5-
"version": "1.0",
5+
"version": "1.0.1",
66
"description": "Streamline your Youtube experience",
77
"homepage_url": "https://tidytube.app",
88
"icons": {

public/edge_manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 3,
33
"name": "tidytube - Declutter Youtube",
44
"short_name": "tidytube",
5-
"version": "1.0",
5+
"version": "1.0.1",
66
"description": "Streamline your Youtube experience",
77
"homepage_url": "https://tidytube.app",
88
"icons": {

public/firefox_manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "tidytube - Declutter Youtube",
44
"short_name": "tidytube",
5-
"version": "1.0",
5+
"version": "1.0.1",
66
"description": "Streamline your Youtube experience",
77
"homepage_url": "https://tidytube.app",
88
"icons": {

src/background/background.ts

+61-41
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@ interface StatsRes {
1414
message: string
1515
}
1616

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+
1752
// Setup alarms
1853
browser.alarms.create("sendPageUpdates", {"periodInMinutes": 15});
1954

@@ -22,35 +57,18 @@ browser.runtime.onInstalled.addListener(async function (details) {
2257

2358
if (details.reason == "install") {
2459
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();
5271
}
53-
5472
}
5573

5674
}
@@ -133,7 +151,7 @@ const sendPageUpdates = async () => {
133151

134152
let { clientID } = await browser.storage.local.get("clientID");
135153

136-
let response = await fetch(`${API_URL}/api/updateStats`, {
154+
await fetch(`https://${API_URL}/api/updateStats`, {
137155
method: "POST",
138156
headers: {
139157
"Accept": "application/json",
@@ -142,19 +160,21 @@ const sendPageUpdates = async () => {
142160
"client-id": clientID
143161
},
144162
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);
157167

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+
});
158178
}
159179

160180
}

0 commit comments

Comments
 (0)