/
github.ts
82 lines (72 loc) · 3.01 KB
/
github.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {
getGithubUsername,
isGithubProfilePage,
isGithubPullRequestPage,
isPullRequestCreatePage,
isPullRequestFilesChangedPage,
} 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 injectRepoVotingButtons from "../utils/dom-utils/repoVotingButtons";
import domUpdateWatch from "../utils/dom-utils/domUpdateWatcher";
import injectDescriptionGeneratorButton from "../utils/dom-utils/addDescriptionGenerator";
import injectChangeSuggestorButton from "../utils/dom-utils/changeSuggestorButton";
import prEditWatch, { prReviewWatch } from "../utils/dom-utils/prWatcher";
import { getAiDescription } from "./components/GenerateAIDescription/DescriptionGeneratorButton";
const processGithubPage = async () => {
if (prefersDarkMode(document.cookie)) {
document.documentElement.classList.add("dark");
}
if (isPullRequestCreatePage(window.location.href)) {
void injectDescriptionGeneratorButton();
} else if (isPullRequestFilesChangedPage(window.location.href)) {
prReviewWatch(injectChangeSuggestorButton, 500);
} else if (isGithubPullRequestPage(window.location.href)) {
prEditWatch(injectDescriptionGeneratorButton);
void 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);
}
}
/*
* commenting out repo voting because it's not ready yet // issue #106
* } else if (isGithubRepoPage(window.location.href)) {
* const ownerName = getGithubUsername(window.location.href) ?? "";
* const repoName = window.location.href.split("/").pop() ?? "";
*
* await injectRepoVotingButtons(ownerName, repoName);
* }
*/
domUpdateWatch(processGithubPage, 50);
};
void processGithubPage();
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
switch (msg.type) {
case "get_highlight": {
const title = (document.querySelector(".js-issue-title.markdown-title") as HTMLHeadingElement).innerText;
sendResponse(title);
break;
}
case "get_ai_description": {
const asyncRequest = async () => {
const aiText = await getAiDescription();
sendResponse(aiText);
};
asyncRequest().catch((e: Error | undefined) => {
sendResponse(e?.toString());
console.error(e);
});
return true;
}
}
});