-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathembed.js
57 lines (49 loc) · 1.63 KB
/
embed.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
import {createURL} from '../libs/url.js';
import {loadGistFromSrc} from '../libs/loader.js';
import {getAnonGist, getUserData} from '../libs/GitHub.js';
async function main() {
const github = {
async getGist(gist_id) {
return await getAnonGist(gist_id);
}
};
const params = Object.fromEntries(new URLSearchParams(window.location.search).entries());
const {data, rawData} = await loadGistFromSrc(params.src, github);
const userData = getUserData(rawData);
if (userData) {
const userURL = `https://github.com/${userData.name}/`;
const userElem = document.querySelector('#user');
userElem.href = userURL;
userElem.textContent = userData.name;
const avatarLink = document.querySelector('#avatar-link');
avatarLink.href = userURL;
const avatarElem = document.querySelector('#avatar');
avatarElem.src = userData.avatarURL;
}
const title = data.name || 'jsGist';
document.title = title;
const a = document.querySelector('.head a');
a.textContent = title;
a.href = createURL(window.location.origin, {src: params.src});
if (!params.noheader) {
document.querySelector('.head').style.display = '';
}
const iframe = document.querySelector('iframe');
const handlers = {
gimmeDaCodez: () => {
iframe.contentWindow.postMessage({
type: 'run',
data,
}, "*");
},
};
window.addEventListener('message', (e) => {
const {type, data} = e.data;
const fn = handlers[type];
if (fn) {
fn(data);
}
});
iframe.src = createURL('https://jsgistrunner.devcomments.org/runner-03.html', {url: 'https://jsgist.org/jsgist-runner.js'});
}
main();