-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add to highlights button (#64)
* feat: add-to-highlights-button * feat: improved-dom-update-watcher * chore: formatting * refactor: replace profileScreen.ts with github.ts * refactor: parameterize delayInMs for reuse, implicit inference * chore: 25ms delay for github.ts domupdate * docs: Add documentation link README.md
- Loading branch information
Showing
15 changed files
with
164 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/content-scripts/components/AddPRToHighlights/AddPRToHighlightsButton.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createHtmlElement } from "../../../utils/createHtmlElement"; | ||
import openSaucedLogoIcon from "../../../assets/opensauced-icon.svg"; | ||
|
||
export const AddPRToHighlightsButton = () => { | ||
const addPRToHighlightsButton = createHtmlElement("a", { | ||
href: `https://insights.opensauced.pizza/feed?url=${encodeURIComponent(window.location.href)}`, | ||
target: "_blank", | ||
rel: "noopener noreferrer", | ||
innerHTML: `<span aria-label="Add PR to OpenSauced highlights." data-view-component="true" class="tooltipped tooltipped-n"> | ||
<img data-view-component="true" class="mr-1 mt-1" height="16px" width="16px" src=${chrome.runtime.getURL(openSaucedLogoIcon)}> | ||
</span>`, | ||
}); | ||
|
||
return addPRToHighlightsButton; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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"; | ||
|
||
const processGithubPage = async () => { | ||
if (prefersDarkMode(document.cookie)) { | ||
document.documentElement.classList.add("dark"); | ||
} | ||
|
||
if (isGithubPullRequestPage(window.location.href)) { | ||
injectAddPRToHighlightsButton(); | ||
} 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, 25); | ||
}; | ||
|
||
void processGithubPage(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { AddPRToHighlightsButton } from "../../content-scripts/components/AddPRToHighlights/AddPRToHighlightsButton"; | ||
import { | ||
GITHUB_LOGGED_IN_USER_USERNAME_SELECTOR, | ||
GITHUB_PR_AUTHOR_USERNAME_SELECTOR, | ||
GITHUB_PR_COMMENT_HEADER_SELECTOR, | ||
} from "../../constants"; | ||
import { isLoggedIn } from "../checkAuthentication"; | ||
|
||
const injectAddPRToHighlightsButton = async () => { | ||
if (!(await isLoggedIn())) { | ||
return; | ||
} | ||
|
||
const prAuthorUserName = document.getElementsByClassName( | ||
GITHUB_PR_AUTHOR_USERNAME_SELECTOR, | ||
)[0].textContent; | ||
const loggedInUserUserName = document | ||
.querySelector(GITHUB_LOGGED_IN_USER_USERNAME_SELECTOR) | ||
?.getAttribute("content"); | ||
|
||
if (loggedInUserUserName && prAuthorUserName === loggedInUserUserName) { | ||
const commentFormatRow = document.getElementsByClassName( | ||
GITHUB_PR_COMMENT_HEADER_SELECTOR, | ||
)[0]; | ||
const addPRToHighlightsButton = AddPRToHighlightsButton(); | ||
|
||
if ( | ||
!commentFormatRow.lastElementChild?.previousElementSibling?.isEqualNode( | ||
addPRToHighlightsButton, | ||
) | ||
) { | ||
commentFormatRow.insertBefore( | ||
addPRToHighlightsButton, | ||
commentFormatRow.lastElementChild, | ||
); | ||
} | ||
} | ||
}; | ||
|
||
export default injectAddPRToHighlightsButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const domUpdateWatch = (callback: () => void, delayInMs = 0) => { | ||
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, delayInMs); | ||
}, | ||
); | ||
|
||
observer.observe(document.body, { | ||
childList: true, | ||
subtree: true, | ||
}); | ||
}; | ||
|
||
export default domUpdateWatch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters