-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
210 lines (177 loc) · 5.65 KB
/
options.js
File metadata and controls
210 lines (177 loc) · 5.65 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"use strict";
const isMac = navigator.platform.toUpperCase().includes("MAC");
const DEFAULT_SHORTCUT = { key: "u", mod: true, ctrlKey: false, shiftKey: true };
const DEFAULT_MESSAGE = "";
const TOKEN_CREATE_URL =
"https://github.com/settings/tokens/new?scopes=repo&description=PR+Quick+Approve+Extension";
const welcomeEl = document.getElementById("welcome");
const tokenInput = document.getElementById("token-input");
const tokenStatus = document.getElementById("token-status");
const createTokenBtn = document.getElementById("create-token-btn");
const input = document.getElementById("shortcut-input");
const messageInput = document.getElementById("message-input");
const saveBtn = document.getElementById("save-btn");
const resetBtn = document.getElementById("reset-btn");
const statusEl = document.getElementById("status");
let pendingShortcut = null;
// --- Token validation ---
async function validateToken(token) {
if (!token) {
setTokenStatus("", "");
return;
}
setTokenStatus("Checking...", "checking");
try {
const resp = await fetch("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
},
});
if (!resp.ok) {
setTokenStatus("Invalid token — authentication failed", "invalid");
return false;
}
const user = await resp.json();
const scopes = resp.headers.get("x-oauth-scopes") || "";
// Classic PAT — check for repo scope
if (scopes) {
const scopeList = scopes.split(",").map((s) => s.trim());
if (!scopeList.includes("repo")) {
setTokenStatus(
`Authenticated as ${user.login}, but missing "repo" scope`,
"invalid"
);
return false;
}
setTokenStatus(`Valid — ${user.login} (classic PAT, repo scope)`, "valid");
return true;
}
// Fine-grained PAT — no x-oauth-scopes header. Verify by testing PR access.
setTokenStatus(`Valid — ${user.login} (fine-grained PAT)`, "valid");
return true;
} catch (err) {
setTokenStatus(`Error: ${err.message}`, "invalid");
return false;
}
}
function setTokenStatus(text, className) {
tokenStatus.textContent = text;
tokenStatus.className = className;
}
// --- Create token flow ---
createTokenBtn.addEventListener("click", () => {
window.open(TOKEN_CREATE_URL, "_blank");
setTokenStatus(
'After creating the token, paste it above and click Save.',
"checking"
);
tokenInput.focus();
});
// Validate on paste (user pastes token from GitHub)
tokenInput.addEventListener("paste", () => {
setTimeout(() => validateToken(tokenInput.value.trim()), 100);
});
// Also validate on input (e.g. autofill)
let validateDebounce;
tokenInput.addEventListener("input", () => {
clearTimeout(validateDebounce);
validateDebounce = setTimeout(() => validateToken(tokenInput.value.trim()), 500);
});
// --- Shortcut formatting ---
function modLabel() {
return isMac ? "\u2318" : "Alt";
}
function formatShortcut(s) {
const parts = [];
if (s.ctrlKey) parts.push(isMac ? "\u2303" : "Ctrl");
if (s.mod) parts.push(modLabel());
if (s.shiftKey) parts.push(isMac ? "\u21E7" : "Shift");
parts.push(s.key.length === 1 ? s.key.toUpperCase() : s.key);
return parts.join(" + ");
}
function eventToShortcut(e) {
const mod = isMac ? e.metaKey : e.altKey;
return {
key: e.key.length === 1 ? e.key.toLowerCase() : e.key,
mod,
ctrlKey: e.ctrlKey,
shiftKey: e.shiftKey,
};
}
function hasModifier(s) {
return s.mod || s.ctrlKey || s.shiftKey;
}
// --- Load / Save ---
function loadSettings() {
chrome.storage.sync.get(
{ shortcut: DEFAULT_SHORTCUT, approveMessage: DEFAULT_MESSAGE, githubToken: "" },
(data) => {
pendingShortcut = data.shortcut;
input.value = formatShortcut(data.shortcut);
messageInput.value = data.approveMessage;
tokenInput.value = data.githubToken;
// Hide welcome banner if already configured
if (data.githubToken) {
welcomeEl.style.display = "none";
validateToken(data.githubToken);
}
}
);
}
input.addEventListener("focus", () => {
input.classList.add("recording");
input.value = "Press a key combination...";
});
input.addEventListener("blur", () => {
input.classList.remove("recording");
if (pendingShortcut) {
input.value = formatShortcut(pendingShortcut);
}
});
input.addEventListener("keydown", (e) => {
e.preventDefault();
e.stopPropagation();
if (["Control", "Alt", "Shift", "Meta"].includes(e.key)) return;
const s = eventToShortcut(e);
if (!hasModifier(s)) return;
pendingShortcut = s;
input.value = formatShortcut(pendingShortcut);
input.classList.remove("recording");
input.blur();
});
saveBtn.addEventListener("click", async () => {
if (!pendingShortcut) return;
const token = tokenInput.value.trim();
// Validate token before saving
if (token) {
const valid = await validateToken(token);
if (!valid) return;
}
chrome.storage.sync.set(
{
shortcut: pendingShortcut,
approveMessage: messageInput.value,
githubToken: token,
},
() => {
statusEl.classList.add("saved");
setTimeout(() => statusEl.classList.remove("saved"), 2000);
}
);
});
resetBtn.addEventListener("click", () => {
pendingShortcut = DEFAULT_SHORTCUT;
input.value = formatShortcut(DEFAULT_SHORTCUT);
messageInput.value = DEFAULT_MESSAGE;
tokenInput.value = "";
setTokenStatus("", "");
chrome.storage.sync.set(
{ shortcut: DEFAULT_SHORTCUT, approveMessage: DEFAULT_MESSAGE, githubToken: "" },
() => {
statusEl.classList.add("saved");
setTimeout(() => statusEl.classList.remove("saved"), 2000);
}
);
});
loadSettings();