-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
90 lines (79 loc) · 2.67 KB
/
content.js
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
83
84
85
86
87
88
89
90
function addDiffButton() {
const buttonGroup = document.querySelector(".gh-header-actions");
if (!buttonGroup || document.querySelector(".diff-extract-button")) return;
const button = document.createElement("button");
button.className = "btn btn-sm diff-extract-button";
button.textContent = "Do Diffinity";
buttonGroup.appendChild(button);
button.addEventListener("click", () => {
chrome.storage.sync.get(["language"], (data) => {
const language = data.language || "en";
const title = document
.querySelector(".js-issue-title")
?.textContent.trim();
const descriptionElement = document.querySelector(".js-comment-body");
const description = descriptionElement
? descriptionElement.textContent.trim()
: "";
const baseUrl = window.location.href.match(
/https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/
);
if (baseUrl) {
const diffUrl = `${baseUrl[0]}.diff`;
chrome.runtime.sendMessage(
{ type: "fetchDiff", url: diffUrl },
(response) => {
if (response && response.success && response.data) {
const combinedText = `Please review this PR in ${language} language. This review must be provided in ${language}. PR Title: ${title}\n\n${
description ? `PR Description: ${description}` : ""
} Diff:${response.data}`;
showButtonSuccess(button);
chrome.runtime.sendMessage({
type: "openChatGPT",
data: combinedText,
});
} else {
showButtonError(button);
console.error(
"Failed to fetch diff data or data is empty:",
response
);
}
}
);
} else {
console.error("Could not construct diff URL.");
}
});
});
}
function showButtonSuccess(button) {
button.textContent = "Diffinity SUCCESS!";
button.style.backgroundColor = "#2da44e";
button.style.color = "white";
setTimeout(() => {
button.textContent = "Do Diffinity";
button.style.backgroundColor = "";
button.style.color = "";
}, 2000);
}
function showButtonError(button) {
button.textContent = "Diffinity Failed";
button.style.backgroundColor = "#cf222e";
button.style.color = "white";
setTimeout(() => {
button.textContent = "Diffinity Error";
button.style.backgroundColor = "";
button.style.color = "";
}, 2000);
}
addDiffButton();
const observer = new MutationObserver(() => {
if (window.location.href.includes("/pull/")) {
addDiffButton();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});