diff --git a/src/content-scripts/github.ts b/src/content-scripts/github.ts new file mode 100644 index 00000000..33a87eb3 --- /dev/null +++ b/src/content-scripts/github.ts @@ -0,0 +1,35 @@ +import { + getGithubUsername, + isGithubProfilePage, + isGithubPullRequestPage, +} from "../utils/urlMatchers"; +import { isOpenSaucedUser } from "../utils/fetchOpenSaucedApiData"; +import injectViewOnOpenSauced from "../utils/dom-utils/viewOnOpenSauced"; +import injectInviteToOpenSauced from "../utils/dom-utils/inviteToOpenSauced"; +import { prefersDarkMode } from "../utils/colorPreference"; +import injectAddPRToHighlightsButton from "../utils/dom-utils/addPRToHighlights"; +import domUpdateWatch from "../utils/dom-utils/domUpdateWatcher"; +import { isLoggedIn } from "../utils/checkAuthentication"; + +const processGithubPage = async () => { + if (prefersDarkMode(document.cookie)) { + document.documentElement.classList.add("dark"); + } + + if (isGithubPullRequestPage(window.location.href)) { + setTimeout(injectAddPRToHighlightsButton, 10); + } else if (isGithubProfilePage(window.location.href)) { + const username = getGithubUsername(window.location.href); + + if (!username) return; + if (await isOpenSaucedUser(username)) { + injectViewOnOpenSauced(username); + } else { + injectInviteToOpenSauced(username); + } + } + + domUpdateWatch(processGithubPage); +}; + +void processGithubPage(); diff --git a/src/utils/dom-utils/domUpdateWatcher.ts b/src/utils/dom-utils/domUpdateWatcher.ts new file mode 100644 index 00000000..9849eb9d --- /dev/null +++ b/src/utils/dom-utils/domUpdateWatcher.ts @@ -0,0 +1,21 @@ +const domUpdateWatch = (callback: () => void) => { + const oldLocation = document.location.href; + const observer = new MutationObserver( + (_: unknown, observer: MutationObserver) => { + const newLocation = document.location.href; + + if (oldLocation === newLocation || document.readyState !== "complete") { + return; + } + observer.disconnect(); + setTimeout(callback, 20); + }, + ); + + observer.observe(document.body, { + childList: true, + subtree: true, + }); +}; + +export default domUpdateWatch; diff --git a/src/worker/background.ts b/src/worker/background.ts index 82033f53..a953bd53 100644 --- a/src/worker/background.ts +++ b/src/worker/background.ts @@ -1,12 +1,6 @@ import { checkAuthentication } from "../utils/checkAuthentication"; import { SUPABASE_AUTH_COOKIE_NAME, OPEN_SAUCED_INSIGHTS_DOMAIN } from "../constants"; -chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { - if (changeInfo.url?.includes("github.com")) { - void chrome.tabs.sendMessage(tabId, { message: "GITHUB_URL_CHANGED" }); - } -}); - chrome.cookies.onChanged.addListener(changeInfo => { if ( changeInfo.cookie.name === SUPABASE_AUTH_COOKIE_NAME ||