2014-12-23 17:27:33 +00:00
|
|
|
// asciinema embedded player
|
2013-06-12 19:33:30 +00:00
|
|
|
|
|
|
|
(function() {
|
2013-06-12 20:47:39 +00:00
|
|
|
function insertAfter(referenceNode, newNode) {
|
|
|
|
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
function params(container, script) {
|
2015-02-18 11:28:15 +00:00
|
|
|
function format(name) {
|
|
|
|
var value = script.getAttribute('data-' + name);
|
|
|
|
if (value) {
|
|
|
|
return name + '=' + value;
|
|
|
|
}
|
2014-07-01 20:44:43 +00:00
|
|
|
}
|
2013-12-14 13:54:01 +00:00
|
|
|
|
2015-02-18 11:28:15 +00:00
|
|
|
var options = ['size', 'speed', 'autoplay', 'loop', 'theme'];
|
|
|
|
|
|
|
|
return '?' + options.map(format).filter(Boolean).join('&');
|
2013-12-14 13:54:01 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 17:27:33 +00:00
|
|
|
function locationFromString(string) {
|
|
|
|
var parser = document.createElement('a');
|
|
|
|
parser.href = string;
|
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
function apiHostFromScript(script) {
|
|
|
|
var location = locationFromString(script.src);
|
|
|
|
return location.protocol + '//' + location.host;
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
function insertPlayer(script) {
|
2014-12-23 17:27:33 +00:00
|
|
|
// do not insert player if there's one already associated with this script
|
|
|
|
if (script.dataset.player) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiHost = apiHostFromScript(script);
|
|
|
|
var apiUrl = apiHost + '/api';
|
|
|
|
|
|
|
|
var asciicastId = script.id.split('-')[1];
|
|
|
|
|
2013-06-12 20:47:39 +00:00
|
|
|
var container = document.createElement('div');
|
2014-12-23 17:27:33 +00:00
|
|
|
container.id = "asciicast-container-" + asciicastId;
|
2013-06-12 20:47:39 +00:00
|
|
|
container.className = 'asciicast';
|
|
|
|
container.style.display = 'block';
|
|
|
|
container.style.float = 'none';
|
|
|
|
container.style.overflow = 'hidden';
|
2013-12-14 13:54:01 +00:00
|
|
|
container.style.padding = 0;
|
|
|
|
container.style.margin = '20px 0';
|
2013-10-13 09:40:15 +00:00
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
insertAfter(script, container);
|
2013-10-13 09:40:15 +00:00
|
|
|
|
2014-12-23 17:27:33 +00:00
|
|
|
var iframe = document.createElement('iframe');
|
|
|
|
iframe.src = apiUrl + "/asciicasts/" + asciicastId + params(container, script);
|
|
|
|
iframe.id = "asciicast-iframe-" + asciicastId;
|
|
|
|
iframe.name = "asciicast-iframe-" + asciicastId;
|
2013-12-14 13:54:01 +00:00
|
|
|
iframe.scrolling = "no";
|
2014-06-24 20:56:37 +00:00
|
|
|
iframe.setAttribute('allowFullScreen', 'true');
|
2013-12-14 13:54:01 +00:00
|
|
|
iframe.style.overflow = "hidden";
|
|
|
|
iframe.style.margin = 0;
|
|
|
|
iframe.style.border = 0;
|
2014-06-24 18:34:34 +00:00
|
|
|
iframe.style.display = "inline-block";
|
|
|
|
iframe.style.width = "100%";
|
2013-12-14 13:54:01 +00:00
|
|
|
iframe.style.float = "none";
|
|
|
|
iframe.style.visibility = "hidden";
|
|
|
|
iframe.onload = function() { this.style.visibility = 'visible' };
|
2013-10-13 09:40:15 +00:00
|
|
|
|
2013-12-14 13:54:01 +00:00
|
|
|
container.appendChild(iframe);
|
2013-10-13 09:40:15 +00:00
|
|
|
|
2014-12-23 17:27:33 +00:00
|
|
|
function receiveSize(e) {
|
|
|
|
if (e.origin === apiHost) {
|
|
|
|
var event = e.data[0];
|
|
|
|
var data = e.data[1];
|
|
|
|
if (event == 'asciicast:size' && data.id == asciicastId) {
|
|
|
|
iframe.style.width = '' + data.width + 'px';
|
|
|
|
iframe.style.height = '' + data.height + 'px';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-14 13:54:01 +00:00
|
|
|
|
|
|
|
window.addEventListener("message", receiveSize, false);
|
2014-12-23 17:27:33 +00:00
|
|
|
|
|
|
|
script.dataset.player = container;
|
|
|
|
}
|
|
|
|
|
|
|
|
var scripts = document.querySelectorAll("script[id^='asciicast-']")
|
|
|
|
for (var i = 0; i < scripts.length; i++) {
|
|
|
|
insertPlayer(scripts[i]);
|
2013-06-12 20:47:39 +00:00
|
|
|
}
|
2014-12-23 17:27:33 +00:00
|
|
|
|
2013-06-12 19:33:30 +00:00
|
|
|
})();
|