forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-explorer.html
More file actions
362 lines (329 loc) · 12.5 KB
/
github-explorer.html
File metadata and controls
362 lines (329 loc) · 12.5 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta charset="UTF-8">
<title>MakeCode GitHub Explorer</title>
<link rel="stylesheet"
href="https://pxt.azureedge.net/blob/2163189fd5e35c0981ed55318415582a7c9aeb12/doccdn/semantic.css"
type="text/css">
<style>
@targetstyle@
label, nav.ui.menu {
font-size: 0.8rem !important;
}
#signin {
margin-left: calc(50% - 10rem);
margin-top: 10rem;
width: 20rem;
text-align: center;
margin-right: calc(50% - 10rem);
}
#user {
width: 14rem;
}
#repoes {
overflow-y: auto;
}
#repocolumn {
padding: 0.5rem;
position: absolute;
width: 18rem;
height: calc(100% - 5.3rem);
border: none;
left: 0;
right: calc(100% - 18rem);
bottom: 3rem;
top: 2.3rem;
overflow-x: hidden;
overflow-y: auto;
}
#makecodecolumn {
position: absolute;
width: calc(100% - 18rem);
height: calc(100% - 2.3rem);
border: none;
left: 18rem;
right: 0;
bottom: 0rem;
top: 2.3rem;
}
#footer {
position: absolute !important;
left: 0;
right: 0;
width: 18rem;
bottom: 0 !important;
font-size: 0.7rem;
margin: 0.5rem;
}
#footer a {
color: black;
}
.ui.selection.list>.item.active {
border: solid 2px grey;
}
</style>
<script type="text/javascript"
src="https://pxt.azureedge.net/blob/4d9b3a258759c53e7bc66b6fc554c51e2434437c/doccdn/jquery.js"></script>
<script type="text/javascript"
src="https://pxt.azureedge.net/blob/fc2f56f46189a2e8be3743a1a5abed3b676f9318/doccdn/semantic.js"></script>
<script>
const api = "https://api.github.com";
const targets = {
"ev3": {
name: "LEGO® MINDSTORMS® Education EV3",
url: "https://makecode.mindstorms.com/beta"
},
"arcade": {
name: "Arcade",
url: "https://arcade.makecode.com/beta"
},
"microbit": {
name: "micro:bit",
url: "https://makecode.microbit.org/beta"
},
"adafruit": {
name: "Adafruit Circuit Playground Express",
url: "https://makecode.adafruit.com/beta",
icon: "https://pxt.azureedge.net/blob/1215b9ef46d3e04bc9923a1a333d92db92eb5419/static/logo.square.black.svg"
},
"minecraft": {
name: "Minecraft",
url: "https://minecraft.makecode.com/beta"
},
"maker": {
name: "Maker",
url: "https://maker.makecode.com"
},
"calliopemini": {
name: "Calliope Mini",
url: "https://makecode.calliope.cc/beta"
}
}
function trackClick(id) {
if (typeof mscc !== "undefined" && !mscc.hasConsent())
mscc.setConsent();
if (typeof pxt !== "undefined")
pxt.aiTrackEvent(id);
}
async function sniffTarget(repo) {
const response = await fetch(`https://raw.githubusercontent.com/${repo.full_name}/master/README.md?token=${token()}`);
const msg = await response.text();
return Object.keys(targets)
.find(k => msg.indexOf(`PXT/${k}`) > -1)
}
async function fetchUserRepos(user) {
const query = `user:${user} in:name,description,readme "for PXT/"`;
const url = `${api}/search/repositories?q=${encodeURIComponent(query)}&sort=updated&access_token=${token()}&anti_cache=${Math.random()}`;
const response = await fetch(url);
const { items } = await response.json();
for (let i = 0; i < items.length; ++i) {
const target = await sniffTarget(items[i]);
if (!target)
items[i] = undefined;
else
items[i].target = target;
}
return items.filter(m => !!m);
}
async function fetchUser(user) {
const response = await fetch(`${api}/users/${user}?access_token=${token()}`);
if (response.status != 200)
return undefined;
const msg = await response.json();
return msg;
}
async function fetchCurrentUser() {
const response = await fetch(`${api}/user?access_token=${token()}`);
if (response.status != 200)
return undefined;
const msg = await response.json();
return msg;
}
async function update(userName) {
$('#userparent').addClass('loading');
try {
$("#repoes")
.empty();
$("#makecodecolumn").attr("src", "");
const user = await fetchUser(userName);
if (!user) {
$('#repoes').append(`<div class="ui item">
user not found
</div>`)
return;
}
$('#repoes')
.append(`
<a href="https://github.com/${user.login}" target="_blank" class="ui link item" title="Open user profile">
<div class="ui mini image">
<img src="${user.avatar_url}">
</div>
<div class="content">
<div class="header">${user.name}</div>
<div class="description">${user.login}</div>
</div>
</a>`)
const repoes = await fetchUserRepos(userName);
$("#repoes")
.append(`<div class="ui divider"></div>`)
.append(
repoes.map(repo => {
const target = targets[repo.target];
const icon = target.icon || (target.url.replace(/\/\w+$/, '') + "/favicon.ico");
const item = $(
`<div class="ui link item">
<div class="ui mini image">
<img src="${icon}">
</div>
<div class="content">
<div class="header">${repo.name.replace(/^pxt-/, '')}</div>
<div class="description">${target.name}</div>
</div>
</div>`);
item.click(event => {
trackClick("github.explorer.open")
$('#repoes').children().removeClass('active');
item.addClass('active');
const readOnly = !$('#edit').is(':checked');
const url = targets[repo.target].url +
`?nocookiebanner=1&${readOnly ? 'controller=1&readonly=1&ws=mem&' : 'editorLayout=ide&nosandbox=1'}#pub:github:`
+ repo.full_name + "#master";
$("#makecodecolumn").attr("src", url);
})
return item;
}))
} finally {
$('#userparent').removeClass('loading');
}
}
function token() {
return localStorage["core/githubtoken"];
}
function checkToken() {
// sniff oauth
let keys = {};
window.location.hash.replace(/^#/, '').split('&')
.map(v => v.split('=', 2))
.forEach(a => keys[a[0]] = a[1]);
if (keys["access_token"]) {
if (keys["state"] == localStorage["core/oauthState"]) {
localStorage["core/githubtoken"] = keys["access_token"];
}
delete localStorage["core/oauthState"];
window.location.hash = "";
}
// force sign...
if (!token()) {
$("#signin").show();
$('#signout').hide();
$('#userparent').hide();
$('#repocolumn').hide();
$('#makecodecolumn').hide();
} else {
$("#signin").hide();
$('#signout').show();
$('#userparent').show();
$('#repocolumn').show();
$('#makecodecolumn').show();
}
}
function navigateSignin() {
trackClick("github.explorer.signin")
const state = Math.random().toString();
localStorage["core/oauthState"] = state;
const login = "https://makecode.com/oauth/login?state=" + state +
"&response_type=token&client_id=gh-token&redirect_uri=" +
encodeURIComponent(window.location.href.split('#', 1)[0])
window.location.href = login;
}
function navigateSignout() {
trackClick("github.explorer.signout")
delete localStorage["core/oauthState"];
delete localStorage["core/githubtoken"];
window.location.reload();
}
$(function () {
console.log(`loaded...`);
checkToken();
$('#signinbtn').click(navigateSignin)
$('#signout').click(navigateSignout);
$("#user").on("keydown", function search(e) {
if (e.keyCode == 13) {
trackClick("github.explorer.search")
const userName = $("#user").val();
update(userName);
}
});
$('#edit').change(function () {
trackClick("github.explorer.edit")
const userName = $("#user").val();
update(userName);
})
console.log("starting...");
if (typeof pxt !== "undefined")
pxt.aiTrackEvent("github.explorer.loaded")
fetchCurrentUser()
.then(user => {
if (user) {
$("#user").val(user.login);
update(user.login);
}
})
});
</script>
</head>
<body id='root' class='root'>
<nav class="ui menu fixed borderless">
<div class="menu left">
<div class="ui item">
MakeCode GitHub Explorer
</div>
</div>
<div class="menu right">
<!--
<div class="ui item">
<div class="ui toggle checkbox">
<input type="checkbox" name="edit" id="edit">
<label id="editlabel">Edit</label>
</div>
</div>-->
<a href="https://forum.makecode.com" target="_blank" class="ui item">
Forum
</a>
<a href="/github/explorer" target="_blank" class="ui item">
Docs
</a>
<div id="signout" class="ui link item">
Sign out
</div>
</div>
</nav>
<div id="repocolumn">
<div id="userparent" class="ui left icon input">
<input id="user" class="ui fluid" type="text" placeholder="Enter GitHub user" />
<i class="search icon"></i>
</div>
<div id="repoes" class="ui selection list"></div>
</div>
<iframe id="makecodecolumn"></iframe>
<div id="signin">
<p>
Sign in to access your MakeCode repositories.
</p>
<button id="signinbtn" class="ui icon basic button">
<i class="ui github icon"></i>
Sign in
</button>
</div>
<footer id="footer" class="hideprint">
<a class="item" href="https://makecode.com/privacy" target="_blank" rel="noopener">Privacy & Cookies</a>
<a class="item" href="https://makecode.com/termsofuse" target="_blank" rel="noopener"> Terms Of Use</a>
<a class="item" href="https://makecode.com/trademarks" target="_blank" rel="noopener">Trademarks</a>
<div class="item">© 2019 Microsoft</div>
</footer>
<script type="text/javascript"
src="https://pxt.azureedge.net/blob/795d8506c80a04f5ca26f577a8d6152e2fa3e7a6/doccdn/pxtweb.js"></script>
<!-- @include tracking.html -->
</body>
</html>