-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
60 lines (56 loc) · 2.02 KB
/
popup.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
document.addEventListener('DOMContentLoaded', function () {
const token = 'Your Github Personal Acess token';
const username = 'Github Username';
const repoNames = ['Repo1', 'Repo2', 'Repo3'];
const updateReadme = (repoName, inputId, buttonId) => {
document.getElementById(buttonId).addEventListener('click', () => {
const textToAdd = document.getElementById(inputId).value;
if (textToAdd) {
fetch(`https://api.github.com/repos/${username}/${repoName}/contents/README.md`, {
headers: {
'Authorization': `token ${token}`
}
})
.then(response => response.json())
.then(file => {
const content = atob(file.content);
const newContent = content + '\n' + textToAdd;
const encodedContent = btoa(newContent);
fetch(`https://api.github.com/repos/${username}/${repoName}/contents/README.md`, {
method: 'PUT',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: 'Updated README.md',
content: encodedContent,
sha: file.sha
})
})
.then(response => response.json())
.then(data => {
if (data.content) {
alert(`README.md updated successfully in ${repoName}!`);
} else {
alert('Error updating README.md: ' + (data.message || 'Unknown error'));
}
})
.catch(error => {
console.error('Error updating README.md', error);
});
})
.catch(error => {
console.error('Error fetching README.md', error);
});
} else {
alert('Please enter the text to add.');
}
});
};
repoNames.forEach((repoName, index) => {
const inputId = `readmeUpdateInput${index + 1}`;
const buttonId = `updateReadmeButton${index + 1}`;
updateReadme(repoName, inputId, buttonId);
});
});