From 06b070fbd1a39f043a640d3f6e3de278a880b983 Mon Sep 17 00:00:00 2001 From: Anush Date: Thu, 27 Apr 2023 06:06:02 +0530 Subject: [PATCH] feat: GitHub page update listener (#37) * feat: gh-page-update-listener * fix: Updated GH username matcher to ignore query params * chore: Added auth-cookie-change handler * chore: Removed debug comments --------- Co-authored-by: Brian Douglas --- manifest.json | 2 +- src/constants.ts | 2 ++ src/content-scripts/profileScreen.ts | 9 +++++++- src/utils/urlMatchers.ts | 2 +- src/worker/background.ts | 32 ++++++++++++++++++++++------ 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/manifest.json b/manifest.json index 3d904328..7c06d6af 100644 --- a/manifest.json +++ b/manifest.json @@ -25,5 +25,5 @@ "128": "src/assets/os-icons/os-icon-128.png" }, "host_permissions": [""], - "permissions": ["storage","webRequest", "activeTab"] + "permissions": ["storage","webRequest", "activeTab", "tabs", "cookies"] } diff --git a/src/constants.ts b/src/constants.ts index 5fec56c3..9dd2c751 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,4 +1,6 @@ export const SUPABASE_LOCAL_STORAGE_KEY = "supabase.auth.token" +export const SUPABASE_AUTH_DOMAIN = "ibcwmlhcimymasokhgvn.supabase.co" +export const SUPABASE_COOKIE_NAME = "sb-access-token" export const SUPABASE_LOGIN_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/authorize?provider=github&redirect_to=https://hot.opensauced.pizza/" export const SUPABASE_LOGOUT_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/logout" export const OPEN_SAUCED_AUTH_TOKEN_KEY = "os-access-token" diff --git a/src/content-scripts/profileScreen.ts b/src/content-scripts/profileScreen.ts index acde284f..495f2a15 100644 --- a/src/content-scripts/profileScreen.ts +++ b/src/content-scripts/profileScreen.ts @@ -11,4 +11,11 @@ if (username != null) { else injectInviteToOpenSauced(username); } }; -processProfilePage(); + +chrome.runtime.onMessage.addListener((request) => { + if (request.message === "GITHUB_URL_CHANGED") { + processProfilePage(); + } +}); + +processProfilePage(); \ No newline at end of file diff --git a/src/utils/urlMatchers.ts b/src/utils/urlMatchers.ts index 7a51e5b7..af1eb703 100644 --- a/src/utils/urlMatchers.ts +++ b/src/utils/urlMatchers.ts @@ -1,5 +1,5 @@ export const getGithubUsername = (url: string) => { - const match = url.match(/github\.com\/([^/]+)/); + const match = url.match(/github\.com\/([\w.-]+)/); return match && match[1]; }; diff --git a/src/worker/background.ts b/src/worker/background.ts index 3e9f8919..b3779bc4 100644 --- a/src/worker/background.ts +++ b/src/worker/background.ts @@ -1,8 +1,28 @@ -import { SUPABASE_LOGOUT_URL, OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants"; +import { SUPABASE_LOGOUT_URL, SUPABASE_AUTH_DOMAIN, SUPABASE_COOKIE_NAME, OPEN_SAUCED_AUTH_TOKEN_KEY } from "../constants"; +import { checkTokenValidity } from "../utils/fetchOpenSaucedApiData"; +import setAccessTokenInChromeStorage from "../utils/setAccessToken"; chrome.webRequest.onCompleted.addListener( - (details) => { - chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); - }, - { urls: [SUPABASE_LOGOUT_URL] } - ); \ No newline at end of file + (details) => { + chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + }, + { urls: [SUPABASE_LOGOUT_URL] } +); + +chrome.tabs.onUpdated.addListener(function (tabId, changeInfo) { + if (changeInfo.url?.includes("github.com")) { + chrome.tabs.sendMessage(tabId, { message: "GITHUB_URL_CHANGED" }); + } +}); + +chrome.cookies.onChanged.addListener(async (changeInfo) => { + try { + if (changeInfo.cookie.name != SUPABASE_COOKIE_NAME || changeInfo.cookie.domain != SUPABASE_AUTH_DOMAIN) return; + if (changeInfo.removed) return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + const isValidToken = await checkTokenValidity(changeInfo.cookie.value) + if (!isValidToken) return chrome.storage.sync.remove(OPEN_SAUCED_AUTH_TOKEN_KEY); + setAccessTokenInChromeStorage(changeInfo.cookie.value); + } catch(error) { + console.error("Error processing cookie update:", error); + } +}) \ No newline at end of file