-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
308 lines (259 loc) · 9.03 KB
/
script.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const urlParams = new URLSearchParams(window.location.search);
function getParameterByName(name, url) {
if (!url) url = window.location.href;
let searchParams = new URLSearchParams(new URL(url).search);
return searchParams.get(name);
}
function extractIdFromUrl(url) {
// Define the pattern to match the ID in the URL
var pattern = /\/([a-f0-9]{32})$/;
// Use the match method to find the match in the URL
var match = url.match(pattern);
// Check if a match is found
if (match) {
// Extract and return the ID
return match[1];
} else {
// Return null if no match is found
return null;
}
}
let CodeId = getParameterByName("id") || extractIdFromUrl(window.location.href) ;
let editor,code;
require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.20.0/min/vs' }});
require(["vs/editor/editor.main"], function() {
editor = monaco.editor.create(document.getElementById('editor'), {
value: [
'<!DOCTYPE html>',
'<html>',
' <head>',
' <title>Code Editor</title>',
' </head>',
'<style>',
'',
'</style>',
' <body>',
' <p>Hello World!</p>',
'',
'',
'<script>',
'',
'</script>',
' </body>',
'</html>'
].join('\n'),
language: 'html',
theme: 'vs-dark'
});
(localStorage.code)?editor.setValue(localStorage.code):true;
document.getElementById('preview').srcdoc = editor.getValue();
editor.onDidChangeModelContent(() => {
console.log('Value changed!',code);
code = editor.getValue();
localStorage.code = code ;
document.getElementById('preview').srcdoc = code;
// do something with updated code
});
});
const runbtn = document.getElementById('runbtn');
runbtn.addEventListener('click', () => {
const code = editor.getValue();
console.log(code);
});
function getGistData(gistId) {
// GitHub API endpoint for retrieving a specific Gist by ID
const apiUrl = `https://api.github.com/gists/${gistId}`;
// Set up the fetch request
return fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.id) {
// Extract the description
const description = data.description;
document.title - description ;
// Extract the content of index.html
const indexHtmlContent = data.files['index.html'].content;
// Extract the content of all files
const allFilesContent = {};
for (const fileName in data.files) {
allFilesContent[fileName] = data.files[fileName].content;
}
// Return all the information in an object
const gistInfo = {
description: description,
indexHtmlContent: indexHtmlContent,
allFilesContent: allFilesContent,
gistJson: data
};
return gistInfo;
} else {
// Gist with the provided ID does not exist
throw new Error('Gist not found.');
}
})
.catch(error => {
// Handle errors
console.error('An error occurred:', error);
return null;
});
}
// Get the value of the "id" parameter from the URL
const id = CodeId;
console.log(id);
if(id){
getGistData(id)
.then(gistInfo => {
if (gistInfo) {
//console.log('Description:', gistInfo.description);
//console.log('Content of index.html:', gistInfo.indexHtmlContent);
//console.log('Content of all files:', gistInfo.allFilesContent);
//console.log('Gist JSON:', gistInfo.gistJson);
document.title = gistInfo.description;
document.getElementById('title').value = gistInfo.description;
editor.setValue(gistInfo.indexHtmlContent);
} else {
console.log('Unable to retrieve Gist information.');
}
});
}
//--------------------ghp_Psc3jiNSR0--uxcM2SCkQsO0C--Mr5gXOo1rbRIz
let accessToken;
let refreshToken = () => {
if(localStorage.accessToken){
accessToken = localStorage.accessToken;
} else {
accessToken = prompt("Enter Your GitHub Access Token :- \n We will not save it", "ForSavingGists");
if(accessToken != null) localStorage.accessToken = accessToken ;
}
}
let savegist = () =>
{
refreshToken();
// Gist data
const gistData = {
description: document.getElementById('title').value || 'Codes20 Gist',
public: true, // Set to true for public, false for private
files: {
'index.html': {
content: editor.getValue()
},
'codes20.md': {
content: 'Codes20 :- https://codes20.github.io/'
},
'poster.png': {
content: '...'
}
}
};
// GitHub API endpoint for creating Gists
const apiUrl = 'https://api.github.com/gists';
// Set up the fetch request
fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `token ${accessToken}`,
'User-Agent': 'Codes20', // Replace with your app name
'Content-Type': 'application/json'
},
body: JSON.stringify(gistData)
})
.then(response => response.json())
.then(data => {
if (data.id) {
// Gist created successfully
const gistId = data.id;
const gistUrl = data.html_url;
console.log(`Gist created successfully. Gist ID: ${gistId}, Gist URL: ${gistUrl}`);
// Open the Gist URL in a new window
window.open(gistUrl, '_blank');
window.open(location.origin+'/'+gistId, '_blank');
} else {
// Failed to create Gist
console.error('Failed to create Gist.');
console.log(data);
}
})
.catch(error => {
console.error('An error occurred:', error);
});
}
let updategist = (gistId) => {
refreshToken();
// Gist data for updating
const gistData = {
description: document.getElementById('title').value || 'Codes20 Gist',
public: true, // Set to true for public, false for private
files: {
'index.html': {
content: editor.getValue()
},
'codes20.md': {
content: 'Codes20 :- https://codes20.github.io/' + gistId
},
'poster.png': {
content: '...'
}
}
};
// GitHub API endpoint for updating a Gist by ID
const apiUrl = `https://api.github.com/gists/${gistId}`;
// Set up the fetch request
fetch(apiUrl, {
method: 'PATCH', // Use PATCH to update an existing Gist
headers: {
'Authorization': `token ${accessToken}`,
'User-Agent': 'Codes20', // Replace with your app name
'Content-Type': 'application/json'
},
body: JSON.stringify(gistData)
})
.then(response => response.json())
.then(data => {
if (data.id) {
// Gist updated successfully
const updatedGistId = data.id;
const gistUrl = data.html_url;
console.log(`Gist updated successfully. Gist ID: ${updatedGistId}, Gist URL: ${gistUrl}`);
// Open the updated Gist URL in a new window
window.open(gistUrl, '_blank');
} else {
// Failed to update Gist
console.error('Failed to update Gist.');
console.log(data);
}
})
.catch(error => {
console.error('An error occurred:', error);
});
}
document.getElementById('update').addEventListener('click', () => {
// Get the gist ID from the URL parameter (you need to implement this)
const gistId = CodeId;
if (gistId) {
updategist(gistId);
} else {
console.error('No Gist ID found in the URL parameter.');
}
});
document.getElementById('save').addEventListener('click', savegist );
if(CodeId){
document.getElementById('update').style.display = 'inline-block';
document.getElementById('save').style.display = 'none';
} else {
document.getElementById('update').style.display = 'none';
document.getElementById('save').style.display = 'inline-block';
}
// Embed JS
let embed = document.getElementById('embed');
embed.addEventListener("click", () => {
prompt('Copy the iframe CODE','<iframe width="560" height="315" src="'+ location.href +'?embed=true" title="Codes20 code player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>')
})
(getParameterByName(embed)) ?document.querySelector('nav').style.display = "none":null;
if(getParameterByName("prefill")){
editor.setValue(getParameterByName("prefill"));
}
window.onload = function() {
if (getParameterByName("prefill")) {
editor.setValue(getParameterByName("prefill"));
}
};