-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
39 lines (33 loc) · 1 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
chrome.runtime.onMessage.addListener((message) => {
chrome.storage.local.get(["gistId", "token"], (result) => {
const gistId = result.gistId;
const token = result.token;
if (gistId && token) {
updateGist(message.title, message.url, gistId, token);
}
});
});
async function updateGist(title, url, gistId, token) {
const headers = new Headers();
headers.append("Authorization", `token ${token}`);
headers.append("Content-Type", "application/json");
const response = await fetch(`https://api.github.com/gists/${gistId}`, {
method: "GET",
headers: headers,
});
const gist = await response.json();
const content = gist.files["articles-ive-read.md"].content;
const updatedContent = `- [${title}](${url})\n` + content;
const body = JSON.stringify({
files: {
"articles-ive-read.md": {
content: updatedContent,
},
},
});
await fetch(`https://api.github.com/gists/${gistId}`, {
method: "PATCH",
headers: headers,
body: body,
});
}