Skip to content

Commit

Permalink
feat: improved-dom-update-watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 committed May 4, 2023
1 parent c4b6e9d commit 51a5a4e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
35 changes: 35 additions & 0 deletions src/content-scripts/github.ts
Original file line number Diff line number Diff line change
@@ -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();
21 changes: 21 additions & 0 deletions src/utils/dom-utils/domUpdateWatcher.ts
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 0 additions & 6 deletions src/worker/background.ts
Original file line number Diff line number Diff line change
@@ -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 ||
Expand Down

0 comments on commit 51a5a4e

Please sign in to comment.