From 3689d935ad4474bd8575d7c8c11f5c350706f28c Mon Sep 17 00:00:00 2001 From: Anush008 Date: Sat, 22 Apr 2023 22:36:19 +0530 Subject: [PATCH 1/4] chore: Added worker script --- manifest.json | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index eedf612a..763b0097 100644 --- a/manifest.json +++ b/manifest.json @@ -7,13 +7,23 @@ { "js": ["src/content-scripts/profileScreen.ts"], "matches": ["https://github.com/*"] + }, + { + "js": ["src/content-scripts/hotHomePage.ts"], + "matches": ["https://hot.opensauced.pizza/*"], + "run_at": "document_end" } ], + "background": { + "service_worker": "src/worker/background.ts", + "type": "module" + }, "icons": { "16": "src/assets/os-icons/os-icon-16.png", "32": "src/assets/os-icons/os-icon-32.png", "48": "src/assets/os-icons/os-icon-48.png", "128": "src/assets/os-icons/os-icon-128.png" }, - "permissions": ["storage"] + "host_permissions": [""], + "permissions": ["storage","webRequest"] } From df79bc9a827b96c637302ddf9f483dd37acbc573 Mon Sep 17 00:00:00 2001 From: Anush008 Date: Sat, 22 Apr 2023 22:46:11 +0530 Subject: [PATCH 2/4] feat: auto-pat-authentication --- manifest.json | 4 +- src/App.tsx | 5 +- src/content-scripts/hotOSHomePage.ts | 22 +++++++++ src/pages/signin.tsx | 69 ---------------------------- src/pages/start.tsx | 12 +++-- src/utils/getAccessToken.ts | 8 ++++ src/utils/setAccessToken.ts | 14 ++++++ src/worker/background.ts | 7 +++ 8 files changed, 61 insertions(+), 80 deletions(-) create mode 100644 src/content-scripts/hotOSHomePage.ts delete mode 100644 src/pages/signin.tsx create mode 100644 src/utils/getAccessToken.ts create mode 100644 src/utils/setAccessToken.ts create mode 100644 src/worker/background.ts diff --git a/manifest.json b/manifest.json index 763b0097..ac5d6f68 100644 --- a/manifest.json +++ b/manifest.json @@ -9,8 +9,8 @@ "matches": ["https://github.com/*"] }, { - "js": ["src/content-scripts/hotHomePage.ts"], - "matches": ["https://hot.opensauced.pizza/*"], + "js": ["src/content-scripts/hotOSHomePage.ts"], + "matches": ["https://hot.opensauced.pizza/"], "run_at": "document_end" } ], diff --git a/src/App.tsx b/src/App.tsx index 9c3c7158..1f300517 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,7 +1,6 @@ import { useState, useEffect } from "react"; import Start from "./pages/start"; -import SignIn from "./pages/signin"; import Home from "./pages/home"; import Loading from "./pages/loading"; @@ -9,7 +8,7 @@ import { checkTokenValidity } from "./utils/fetchOpenSaucedApiData"; function App() { const [osAccessToken, setOsAccessToken] = useState(""); - // renderedPage can be either "start", "home", "signin" or "loading" + // renderedPage can be either "start", "home" or "loading" const [renderedPage, setRenderedPage] = useState("loading"); useEffect(() => { @@ -36,8 +35,6 @@ function App() { ) : renderedPage === "home" ? ( - ) : renderedPage === "signin" ? ( - ) : ( )} diff --git a/src/content-scripts/hotOSHomePage.ts b/src/content-scripts/hotOSHomePage.ts new file mode 100644 index 00000000..0ebf3e9e --- /dev/null +++ b/src/content-scripts/hotOSHomePage.ts @@ -0,0 +1,22 @@ +import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData"; +import getAccessToken from "../utils/getAccessToken"; +import setAccessTokenInChromeStorage from "../utils/setAccessToken"; + +const processHotOSHomePage = async () => { + const data = await chrome.storage.sync.get(["os-access-token"]); + if (data["os-access-token"]) return; + try { + const accessToken = getAccessToken(); + if (!accessToken) return; + + const isValid = await checkTokenValidity(accessToken); + if (!isValid) return; + + await setAccessTokenInChromeStorage(accessToken); + } catch (error) { + console.error("Error processing Hot home page:", error); + } +}; + +//The local storage takes time to set after the document is idle +setTimeout(processHotOSHomePage, 1000); diff --git a/src/pages/signin.tsx b/src/pages/signin.tsx deleted file mode 100644 index 45c9051f..00000000 --- a/src/pages/signin.tsx +++ /dev/null @@ -1,69 +0,0 @@ -import React from "react"; -import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData"; -import { BiArrowBack, BiInfoCircle } from "react-icons/bi"; - -interface SignInProps { - setRenderedPage: (page: string) => void; -} - -function SignIn({ setRenderedPage }: SignInProps) { - const [token, setToken] = React.useState(""); - - const authenticateUser = (token: string) => { - if (token === "") { - alert("Please enter a valid token"); - return; - } - checkTokenValidity(token) - .then((valid) => { - if (valid) { - chrome.storage.sync.set({ "os-access-token": token }, () => { - setRenderedPage("home"); - }); - } else { - alert("Token is invalid"); - } - }) - .catch((err) => { - console.log(err); - alert("An error occurred while authenticating"); - }); - }; - - return ( -
- setRenderedPage("start")} - /> -

Sign In

-

- Enter your Personal Access Token to sign in. - - window.open( - "https://docs.opensauced.pizza/contributing/set-up-authentication/", - "_blank" - ) - } - /> -

- setToken(e.target.value)} - /> - -
- ); -} - -export default SignIn; diff --git a/src/pages/start.tsx b/src/pages/start.tsx index 5c8d7c1d..007b03e1 100644 --- a/src/pages/start.tsx +++ b/src/pages/start.tsx @@ -18,13 +18,15 @@ function Start({ setRenderedPage }: StartProps) { {" "} browser extension.

- + Login! + ); } diff --git a/src/utils/getAccessToken.ts b/src/utils/getAccessToken.ts new file mode 100644 index 00000000..44b2a40d --- /dev/null +++ b/src/utils/getAccessToken.ts @@ -0,0 +1,8 @@ +const getAccessToken = (): string | null => { + const localStore = window.localStorage.getItem("supabase.auth.token"); + if (localStore === null) return null; + return JSON.parse(localStore)?.currentSession?.access_token; + }; + + export default getAccessToken; + \ No newline at end of file diff --git a/src/utils/setAccessToken.ts b/src/utils/setAccessToken.ts new file mode 100644 index 00000000..9f35d935 --- /dev/null +++ b/src/utils/setAccessToken.ts @@ -0,0 +1,14 @@ +const setAccessTokenInChromeStorage = (accessToken: string): Promise => { + return new Promise((resolve, reject) => { + chrome.storage.sync.set({ "os-access-token": accessToken }, () => { + if (chrome.runtime.lastError) { + reject(chrome.runtime.lastError); + } else { + resolve(); + } + }); + }); + }; + + export default setAccessTokenInChromeStorage; + \ No newline at end of file diff --git a/src/worker/background.ts b/src/worker/background.ts new file mode 100644 index 00000000..ea861056 --- /dev/null +++ b/src/worker/background.ts @@ -0,0 +1,7 @@ +chrome.webRequest.onCompleted.addListener( + (details) => { + chrome.storage.sync.remove("os-access-token"); + }, + { urls: ["https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/logout"] } + ); + \ No newline at end of file From a0c4077da26c127b3de82653c18937ccade7a413 Mon Sep 17 00:00:00 2001 From: Anush008 Date: Sat, 22 Apr 2023 22:51:20 +0530 Subject: [PATCH 3/4] chore: Added empty export from the worker file to build as module --- src/worker/background.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/worker/background.ts b/src/worker/background.ts index ea861056..c2f0e827 100644 --- a/src/worker/background.ts +++ b/src/worker/background.ts @@ -4,4 +4,5 @@ chrome.webRequest.onCompleted.addListener( }, { urls: ["https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/logout"] } ); - \ No newline at end of file + +export {}; \ No newline at end of file From a56f81ed25fbd32baef62fca7fbfbc338e440c80 Mon Sep 17 00:00:00 2001 From: Anush008 Date: Sun, 23 Apr 2023 00:20:13 +0530 Subject: [PATCH 4/4] fix: Added * in the manifest to resolve prod build error --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index ac5d6f68..c4ee64a5 100644 --- a/manifest.json +++ b/manifest.json @@ -10,7 +10,7 @@ }, { "js": ["src/content-scripts/hotOSHomePage.ts"], - "matches": ["https://hot.opensauced.pizza/"], + "matches": ["https://hot.opensauced.pizza/*"], "run_at": "document_end" } ],