-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
github-api-write.html
184 lines (168 loc) · 6.17 KB
/
github-api-write.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub API File/Image Writer</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
color: #2c3e50;
text-align: center;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"], input[type="password"], select, textarea {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #3498db;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #2980b9;
}
#clearImage {
background-color: #e74c3c;
}
#clearImage:hover {
background-color: #c0392b;
}
#result {
margin-top: 20px;
padding: 10px;
border-radius: 4px;
}
#result:not(:empty) {
background-color: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
}
</style>
</head>
<body>
<h1>GitHub API File/Image Writer</h1>
<form id="fileForm">
<label for="token">GitHub Token:</label>
<input type="password" id="token" required>
<label for="owner">Repository Owner:</label>
<input type="text" id="owner" required>
<label for="repo">Repository Name:</label>
<input type="text" id="repo" required>
<label for="path">File Path:</label>
<input type="text" id="path" required>
<label for="contentType">Content Type:</label>
<select id="contentType">
<option value="text">Text</option>
<option value="image">Image</option>
</select>
<div id="textContent">
<label for="content">File Content:</label>
<textarea id="content" rows="10"></textarea>
</div>
<div id="imageContent" style="display:none;">
<label for="image">Select Image:</label>
<input type="file" id="image" accept="image/*">
<button type="button" id="clearImage">Clear Image</button>
</div>
<button type="submit">Create File/Upload Image</button>
</form>
<div id="result"></div>
<script>
const contentTypeSelect = document.getElementById('contentType');
const textContent = document.getElementById('textContent');
const imageContent = document.getElementById('imageContent');
const clearImageBtn = document.getElementById('clearImage');
const imageInput = document.getElementById('image');
contentTypeSelect.addEventListener('change', function() {
if (this.value === 'text') {
textContent.style.display = 'block';
imageContent.style.display = 'none';
} else {
textContent.style.display = 'none';
imageContent.style.display = 'block';
}
});
clearImageBtn.addEventListener('click', function() {
imageInput.value = '';
});
document.getElementById('fileForm').addEventListener('submit', function(e) {
e.preventDefault();
const token = document.getElementById('token').value;
const owner = document.getElementById('owner').value;
const repo = document.getElementById('repo').value;
const path = document.getElementById('path').value;
const contentType = document.getElementById('contentType').value;
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`;
let content;
if (contentType === 'text') {
content = document.getElementById('content').value;
uploadToGitHub(url, token, btoa(content));
} else {
const file = document.getElementById('image').files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
content = e.target.result.split(',')[1]; // Get base64 data
uploadToGitHub(url, token, content);
};
reader.readAsDataURL(file);
} else {
document.getElementById('result').innerHTML = 'Please select an image file.';
}
}
});
function uploadToGitHub(url, token, content) {
fetch(url, {
method: 'PUT',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: 'Create file/image via API',
content: content
})
})
.then(response => response.json())
.then(data => {
if (data.content) {
document.getElementById('result').innerHTML = `File/Image created successfully. <a href="${data.content.html_url}" target="_blank">View file</a>`;
} else {
document.getElementById('result').innerHTML = `Error: ${JSON.stringify(data)}`;
}
})
.catch(error => {
document.getElementById('result').innerHTML = `Error: ${error.message}`;
});
}
</script>
</body>
</html>