-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
106 lines (91 loc) · 2.78 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function insertDownloadButton() {
const buttonContainerSelector = ".gh-header-actions";
const buttonContainer =
document.querySelector(buttonContainerSelector);
if (buttonContainer && !document.getElementById("export-pr-json-btn")) {
const downloadButton = document.createElement("button");
downloadButton.id = "export-pr-json-btn";
downloadButton.textContent = "JSON Export";
downloadButton.className = "btn btn-sm flex-md-order-2";
downloadButton.onclick = handleDownloadClick;
buttonContainer.appendChild(downloadButton);
}
}
function openPopupMessage(message) {
alert(message + "\nClick the extension icon to open the setup.");
}
function setButtonLoadingState(isLoading) {
const downloadButton = document.getElementById("export-pr-json-btn");
if (downloadButton) {
if (isLoading) {
downloadButton.disabled = true;
downloadButton.style.cursor = "wait";
downloadButton.classList.add("disabled");
} else {
console.log("download complete")
setTimeout(() => {
downloadButton.disabled = false;
downloadButton.style.cursor = "pointer";
downloadButton.classList.remove("disabled");
}, 1000);
}
}
}
async function handleDownloadClick() {
chrome.storage.local.get(["githubToken"], async (result) => {
const token = result.githubToken;
if (!token) {
openPopupMessage("Please enter your GitHub Token.");
return;
}
const isValid = await validateToken(token);
if (!isValid) {
openPopupMessage("Your GitHub token is invalid or expired. Please update it.");
return;
}
setButtonLoadingState(true);
chrome.runtime.sendMessage(
{ type: "startDownload", token: token },
(response) => {
if (chrome.runtime.lastError) {
console.error(
"Error starting download:",
chrome.runtime.lastError.message,
);
} else {
console.log("Download initiated.");
}
},
);
});
}
chrome.runtime.onMessage.addListener((message) => {
if (message.type === "downloadComplete") {
setButtonLoadingState(false);
}
});
async function validateToken(token) {
const response = await fetch("https://api.github.com/user", {
headers: {
Authorization: `token ${token}`,
},
});
return response.ok;
}
function triggerPRDownload(token) {
chrome.runtime.sendMessage(
{ type: "startDownload", token: token },
(response) => {
if (chrome.runtime.lastError) {
console.error(
"Error starting download:",
chrome.runtime.lastError.message,
);
} else {
console.log("Download initiated.");
}
},
);
}
window.addEventListener("load", insertDownloadButton);
document.addEventListener("pjax:end", insertDownloadButton);