diff --git a/manifest.json b/manifest.json index eedf612a..c4ee64a5 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/hotOSHomePage.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"] } 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..c2f0e827 --- /dev/null +++ b/src/worker/background.ts @@ -0,0 +1,8 @@ +chrome.webRequest.onCompleted.addListener( + (details) => { + chrome.storage.sync.remove("os-access-token"); + }, + { urls: ["https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/logout"] } + ); + +export {}; \ No newline at end of file