Skip to content

Commit

Permalink
feat: GitHub page update listener (#37)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Anush008 and bdougie authored Apr 27, 2023
1 parent 8a43288 commit 06b070f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
"128": "src/assets/os-icons/os-icon-128.png"
},
"host_permissions": ["<all_urls>"],
"permissions": ["storage","webRequest", "activeTab"]
"permissions": ["storage","webRequest", "activeTab", "tabs", "cookies"]
}
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
9 changes: 8 additions & 1 deletion src/content-scripts/profileScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ if (username != null) {
else injectInviteToOpenSauced(username);
}
};
processProfilePage();

chrome.runtime.onMessage.addListener((request) => {
if (request.message === "GITHUB_URL_CHANGED") {
processProfilePage();
}
});

processProfilePage();
2 changes: 1 addition & 1 deletion src/utils/urlMatchers.ts
Original file line number Diff line number Diff line change
@@ -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];
};

Expand Down
32 changes: 26 additions & 6 deletions src/worker/background.ts
Original file line number Diff line number Diff line change
@@ -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] }
);
(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);
}
})

0 comments on commit 06b070f

Please sign in to comment.