-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgist.js
60 lines (51 loc) · 1.5 KB
/
gist.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
//created by chatgpt
const url = "https://api.github.com/gists";
const filename = "odinochka.md";
const description = "Odinochka Saved Links";
const isPublic = false; // Set to false if you want to create a private gist. Can always make public later.
async function createGist(filename, content, description, isPublic, token) {
const data = {
description: description,
public: isPublic,
files: {
[filename]: {
content: content
}
}
};
return fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
if (data.html_url) {
console.log("Gist created: " + data.html_url);
return data.html_url
} else {
console.error("Failed to create Gist", data);
}
})
.catch(error => {
console.error("Error:", error);
});
}
export async function toGist(content) {
console.log("Checking github host permission.");
const {ghpat} = await chrome.storage.local.get("ghpat");
if(!ghpat) {
alert("No GH PAT")
return;
}
console.log("Checking github host permission.");
const granted = await chrome.permissions.request({origins:[url]});
if(!granted) {
console.error("No host permission!")
return;
}
return createGist(filename, content, description, isPublic, ghpat);
}