-
Notifications
You must be signed in to change notification settings - Fork 80
/
embed.js
56 lines (49 loc) · 1.56 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
var jsonp = require('jsonp')
var url = require('url')
var getGistFiles = require('./get-gist-files')
var $ = window.jQuery
var parsedURL = url.parse(window.location.href, true)
var gistID = parsedURL.query.gist
var binURL = '/?gist=' + gistID
var link = document.querySelector('.requirebin-link')
if (link) link.setAttribute('href', binURL)
if (gistID.indexOf('/') > -1) gistID = gistID.split('/')[1]
loadFromAPI(gistID)
function loadFromAPI (gistID) {
jsonp('https://api.github.com/gists/' + gistID, function (err, gist) {
if (err) return console.log(err)
getGistFiles(gist, ['page-head.html', 'page-body.html', 'head.html', 'minified.js'], function (err) {
if (err) return console.log(err)
var files = gist.data.files
var head
var body
var bundle
var headFile = files['page-head.html'] || files['head.html']
if (headFile) {
head = headFile.content
}
if (files['page-body.html']) {
body = files['page-body.html'].content
}
if (files['minified.js']) {
bundle = files['minified.js'].content
}
render(head, body, bundle)
})
})
}
function render (head, body, bundle) {
if (head) document.head.innerHTML += head
if (body) {
$(document.body).append($.parseHTML(body, document, true))
}
if (!bundle) {
document.body.innerHTML += 'not a valid requirebin gist - missing minified.js'
} else {
$(document.body).append(
$('<script />')
.attr('type', 'text/javascript')
.text('setTimeout(function () {' + bundle + '}, 1000)')
)
}
}