Skip to content

Commit 0d18833

Browse files
committed
initial commit
0 parents  commit 0d18833

12 files changed

+304
-0
lines changed

background.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
chrome.runtime.onInstalled.addListener(() => {
2+
chrome.action.openPopup();
3+
});
4+
5+
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
6+
if (message.type === "fetchDiff") {
7+
fetch(message.url)
8+
.then((response) => response.text())
9+
.then((diffText) => {
10+
sendResponse({ success: true, data: diffText });
11+
})
12+
.catch((error) => {
13+
console.error("Failed to fetch diff:", error);
14+
sendResponse({ success: false });
15+
});
16+
return true;
17+
} else if (message.type === "openChatGPT") {
18+
chrome.storage.local.set({ copiedText: message.data }, () => {
19+
chrome.tabs.create({
20+
url: "https://chatgpt.com/g/g-jYKKhgJny-code-review-based-by-git-diff",
21+
});
22+
});
23+
}
24+
});
25+
26+
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
27+
if (
28+
changeInfo.status === "complete" &&
29+
tab.url &&
30+
tab.url.includes("chatgpt.com")
31+
) {
32+
setTimeout(() => {
33+
chrome.scripting.executeScript({
34+
target: { tabId: tabId },
35+
files: ["chatgpt_injector.js"],
36+
});
37+
}, 1000);
38+
}
39+
});

chatgpt_injector.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
chrome.storage.local.get("copiedText", (result) => {
2+
if (result.copiedText) {
3+
const injectText = () => {
4+
const inputDiv = document.querySelector("#prompt-textarea");
5+
6+
if (inputDiv) {
7+
try {
8+
const pTag = inputDiv.querySelector("p");
9+
10+
if (pTag) {
11+
pTag.textContent = result.copiedText;
12+
}
13+
14+
setTimeout(() => {
15+
const sendButton = document.querySelector(
16+
'[data-testid="send-button"]'
17+
);
18+
19+
if (sendButton) {
20+
sendButton.click();
21+
chrome.storage.local.remove("copiedText");
22+
} else {
23+
console.error("Cannot find the send button");
24+
}
25+
}, 500);
26+
} catch (error) {
27+
console.error("Error injecting text", error);
28+
}
29+
} else {
30+
console.log("#prompt-textarea not found");
31+
setTimeout(injectText, 500);
32+
}
33+
};
34+
35+
// 초기 시도
36+
injectText();
37+
}
38+
});

content.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
function addDiffButton() {
2+
const buttonGroup = document.querySelector(".gh-header-actions");
3+
if (!buttonGroup || document.querySelector(".diff-extract-button")) return;
4+
5+
const button = document.createElement("button");
6+
button.className = "btn btn-sm diff-extract-button";
7+
button.textContent = "Do Diffinity";
8+
buttonGroup.appendChild(button);
9+
10+
button.addEventListener("click", () => {
11+
// Retrieve stored language settings
12+
chrome.storage.sync.get(["language"], (data) => {
13+
const language = data.language || "en";
14+
15+
// Construct the .diff URL
16+
const baseUrl = window.location.href.match(
17+
/https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+/
18+
);
19+
if (baseUrl) {
20+
const diffUrl = `${baseUrl[0]}.diff`;
21+
22+
chrome.runtime.sendMessage(
23+
{ type: "fetchDiff", url: diffUrl },
24+
(response) => {
25+
if (response && response.success && response.data) {
26+
const combinedText = `Answer in this language: ${language}\n\n${response.data}`;
27+
showButtonSuccess(button);
28+
chrome.runtime.sendMessage({
29+
type: "openChatGPT",
30+
data: combinedText,
31+
});
32+
} else {
33+
showButtonError(button);
34+
console.error(
35+
"Failed to fetch diff data or data is empty:",
36+
response
37+
);
38+
}
39+
}
40+
);
41+
} else {
42+
console.error("Could not construct diff URL.");
43+
}
44+
});
45+
});
46+
}
47+
48+
function showButtonSuccess(button) {
49+
button.textContent = "Copied!";
50+
button.style.backgroundColor = "#2da44e";
51+
button.style.color = "white";
52+
setTimeout(() => {
53+
button.textContent = "Do Diffinity";
54+
button.style.backgroundColor = "";
55+
button.style.color = "";
56+
}, 2000);
57+
}
58+
59+
function showButtonError(button) {
60+
button.textContent = "Copy Failed";
61+
button.style.backgroundColor = "#cf222e";
62+
button.style.color = "white";
63+
setTimeout(() => {
64+
button.textContent = "Diffinity Error";
65+
button.style.backgroundColor = "";
66+
button.style.color = "";
67+
}, 2000);
68+
}
69+
70+
addDiffButton();
71+
72+
const observer = new MutationObserver(() => {
73+
if (window.location.href.includes("/pull/")) {
74+
addDiffButton();
75+
}
76+
});
77+
78+
observer.observe(document.body, {
79+
childList: true,
80+
subtree: true,
81+
});

icons/icon128.png

33.3 KB
Loading

icons/icon16.png

858 Bytes
Loading

icons/icon32.png

2.69 KB
Loading

icons/icon48.png

5.76 KB
Loading

manifest.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "Diffinity",
4+
"version": "1.0",
5+
"permissions": ["activeTab", "storage", "scripting"],
6+
"host_permissions": [
7+
"https://patch-diff.githubusercontent.com/*",
8+
"https://chatgpt.com/*"
9+
],
10+
"background": {
11+
"service_worker": "background.js"
12+
},
13+
"content_scripts": [
14+
{
15+
"matches": ["https://github.com/*/pull/*"],
16+
"js": ["content.js"],
17+
"css": ["styles.css"]
18+
},
19+
{
20+
"matches": ["https://chatgpt.com/*"],
21+
"js": ["chatgpt_injector.js"]
22+
}
23+
],
24+
25+
"action": {
26+
"default_popup": "popup.html"
27+
},
28+
29+
"icons": {
30+
"16": "icons/icon16.png",
31+
"32": "icons/icon32.png",
32+
"48": "icons/icon48.png",
33+
"128": "icons/icon128.png"
34+
}
35+
}

popup.css

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* popup.css */
2+
body {
3+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
4+
Arial, sans-serif;
5+
margin: 0;
6+
padding: 0;
7+
background-color: #f6f8fa;
8+
}
9+
10+
.popup-container {
11+
padding: 16px;
12+
width: 250px;
13+
background-color: #ffffff;
14+
border: 1px solid #e1e4e8;
15+
border-radius: 6px;
16+
}
17+
18+
.popup-label {
19+
font-size: 12px;
20+
color: #24292e;
21+
font-weight: 600;
22+
margin-top: 10px;
23+
display: block;
24+
}
25+
26+
textarea,
27+
input[type="text"] {
28+
width: 100%;
29+
padding: 8px;
30+
margin-top: 4px;
31+
margin-bottom: 12px;
32+
border: 1px solid #e1e4e8;
33+
border-radius: 6px;
34+
font-size: 14px;
35+
resize: vertical;
36+
}
37+
38+
textarea {
39+
min-height: 80px;
40+
max-height: 150px;
41+
}
42+
43+
input[type="text"]:focus,
44+
textarea:focus {
45+
border-color: #0366d6;
46+
outline: none;
47+
box-shadow: 0 0 0 3px rgba(3, 102, 214, 0.3);
48+
}
49+
50+
button {
51+
width: 100%;
52+
padding: 10px;
53+
background-color: #2da44e;
54+
border: none;
55+
border-radius: 6px;
56+
color: white;
57+
font-weight: 600;
58+
cursor: pointer;
59+
font-size: 14px;
60+
}
61+
62+
button:hover {
63+
background-color: #22863a;
64+
}
65+
66+
button:focus {
67+
outline: none;
68+
box-shadow: 0 0 0 3px rgba(34, 134, 58, 0.4);
69+
}

popup.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- popup.html -->
2+
<div class="popup-container">
3+
<label for="language" class="popup-label">Language</label>
4+
<input type="text" id="language" placeholder="Enter language..." />
5+
6+
<button id="saveSettings">Save Settings</button>
7+
</div>
8+
<script src="popup.js"></script>
9+
<link rel="stylesheet" href="popup.css" />

popup.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
const languageInput = document.getElementById("language");
3+
const saveButton = document.getElementById("saveSettings");
4+
5+
// Load saved values from Chrome storage
6+
chrome.storage.sync.get(["language"], (data) => {
7+
languageInput.value = data.language || "en";
8+
});
9+
10+
// Save values to Chrome storage
11+
saveButton.addEventListener("click", () => {
12+
const language = languageInput.value;
13+
chrome.storage.sync.set({ language }, () => {
14+
alert("Settings saved!");
15+
});
16+
});
17+
});

styles.css

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.diff-extract-button {
2+
margin-left: 8px;
3+
padding: 5px 12px;
4+
font-size: 12px;
5+
font-weight: 500;
6+
line-height: 20px;
7+
color: #24292f;
8+
background-color: #f6f8fa;
9+
border: 1px solid #1b1f2426;
10+
border-radius: 6px;
11+
cursor: pointer;
12+
}
13+
14+
.diff-extract-button:hover {
15+
background-color: #f3f4f6;
16+
}

0 commit comments

Comments
 (0)