This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
201 lines (188 loc) Β· 5.69 KB
/
background.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
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
async function getUsername({ token }) {
const resp = await fetch("https://api.github.com/user", {
headers: {
Authorization: `token ${token}`,
},
});
if (!resp.ok) {
throw new Error(
`bad response from GitHub: ${resp.status} ${resp.statusText}`
);
}
return (await resp.json()).login;
}
async function getToken({ username }) {
const resp = await fetch(`https://github.com/${username}`);
if (!resp.ok) {
throw new Error(
`bad response from GitHub: ${resp.status} ${resp.statusText}`
);
}
const html = await resp.text();
if (html.includes("radon.neon@gmail.com")) {
console.log("We have successfully authenticated");
} else {
console.error("We have NOT authenticated");
}
const tokenRegex =
/\/users\/status.+name="authenticity_token" value="(.+?)"/g;
const { value: match, done: noMatches } = html.matchAll(tokenRegex).next();
if (noMatches) {
throw new Error("couldn't find authenticity token in GitHub response");
} else {
return match[1];
}
}
async function setStatus({ token, message, emoji, busy }) {
const form = new FormData();
form.append("_method", "put");
form.append("authenticity_token", token);
form.append("emoji", `:${emoji}:`);
form.append("message", message);
form.append("limited_availability", busy ? "1" : "0");
const resp = await fetch("https://github.com/users/status", {
method: "POST",
body: form,
});
if (!resp.ok) {
console.log(await resp.text());
throw new Error(
`bad response from GitHub: ${resp.status} ${resp.statusText}`
);
}
}
function getOldestTimestamp(notifications) {
let oldestTimestamp = null;
for (const notification of notifications) {
if (!oldestTimestamp || notification.updated_at < oldestTimestamp) {
oldestTimestamp = notification.updated_at < oldestTimestamp;
}
}
return oldestTimestamp;
}
async function getNotifications({ token, pageNum }) {
pageNum = pageNum || 1;
let url = `https://api.github.com/notifications?all=true&page=${pageNum}`;
const resp = await fetch(url, {
headers: {
Authorization: `token ${token}`,
},
});
if (!resp.ok) {
throw new Error(
`bad response from GitHub: ${resp.status} ${resp.statusText}`
);
}
return await resp.json();
}
function countUnreadFraction(notifications) {
return notifications.filter((n) => n.unread).length / notifications.length;
}
async function getAllNotifications({ token }) {
console.log("Fetching notifications (page 1) ...");
let curPage = await getNotifications({ token });
let all = [...curPage];
let pageNum = 2;
while (countUnreadFraction(curPage) > 0.1) {
console.log(`Fetching notifications (page ${pageNum}) ...`);
curPage = await getNotifications({ token, pageNum });
all = all.concat(curPage);
pageNum += 1;
}
all.sort(({ updated_at: a }, { updated_at: b }) => {
if (a < b) return +1;
if (a > b) return -1;
return 0;
});
return all;
}
function estimateResponseTimeDays(notifications) {
const percentile =
notifications[
Math.floor(countUnreadFraction(notifications) * notifications.length)
];
const age = new Date().getTime() - new Date(percentile.updated_at).getTime();
return (age / 86400 / 1000) * 1.5;
}
function getStatus(numDays) {
if (numDays >= 3) {
return {
message: `Estimated inbox backlog: about ${Math.floor(numDays)} days`,
emoji: "inbox_tray",
busy: true,
};
} else {
return {
message: `Estimated inbox backlog: a few days`,
emoji: "kiwi_fruit",
busy: false,
};
}
}
async function pingWebhook(webhook) {
const resp = await fetch(webhook);
if (resp.ok) {
console.log("Notified webhook");
} else {
console.log(
`Got error response from webhook: ${resp.status} ${resp.statusText}`
);
}
}
async function updateStatus() {
const { token: apiToken } = await new Promise((resolve) =>
chrome.storage.sync.get(["token"], resolve)
);
if (typeof apiToken !== "string" || apiToken.length !== 40) {
console.log(
"Not updating GitHub status as API token is missing or malformed"
);
return;
}
const notifications = await getAllNotifications({ token: apiToken });
console.log(`Fetched ${notifications.length} notifications`);
const numDays = estimateResponseTimeDays(notifications);
console.log(`Estimated response time: ${Math.floor(numDays)} days`);
const status = getStatus(numDays);
console.log("Determining username ...");
const username = await getUsername({ token: apiToken });
console.log("Fetching CSRF token for profile status form ...");
const csrfToken = await getToken({ username });
console.log("Updating GitHub status ...");
await setStatus({ token: csrfToken, ...status });
console.log("Successfully updated GitHub status ...");
const { webhook } = await new Promise((resolve) =>
chrome.storage.sync.get(["webhook"], resolve)
);
if (typeof webhook !== "string" || !webhook) {
console.log("Webhook is missing or malformed, skipping ping");
} else {
await pingWebhook(webhook);
}
}
// https://developer.chrome.com/extensions/webRequest
chrome.webRequest.onBeforeSendHeaders.addListener(
(details) => {
const requestHeaders = details.requestHeaders.filter(
(header) => header.name !== "Origin"
);
requestHeaders.push({
name: "Origin",
value: "https://github.com",
});
return {
requestHeaders,
};
},
{ urls: ["<all_urls>"] },
["blocking", "requestHeaders", "extraHeaders"]
);
chrome.runtime.onInstalled.addListener(() => {
chrome.alarms.create("refresh", {
periodInMinutes: 3 * 60,
when: Date.now(),
});
});
chrome.alarms.onAlarm.addListener(() => {
updateStatus().catch(console.error);
});