asciinema.org/app/views/asciicasts/show.js.erb

49 lines
1.8 KiB
Plaintext

// asciinema.org - embeddable player
(function() {
function receiveSize(e) {
if (e.origin === document.location.protocol + "//<%= request.host_with_port %>") {
var event = e.data[0];
var data = e.data[1];
if (event == 'asciicast:size' && data.id == <%= @asciicast.id %>) {
var player = document.getElementById("asciicast-iframe-<%= @asciicast.id %>");
if (player) {
player.style.width = data.width;
player.style.height = data.height;
}
}
}
}
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
var scriptTag = document.getElementById("asciicast-<%= @asciicast.id %>");
if (scriptTag) {
window.addEventListener("message", receiveSize, false);
var container = document.createElement('div');
container.className = 'asciicast';
container.style.display = 'block';
container.style.float = 'none';
container.style.overflow = 'hidden';
container.style.padding = '0';
insertAfter(scriptTag, container);
var params = '?container_width=' + container.offsetWidth;
var size = scriptTag.getAttribute('data-size');
if (size) {
params += '&size=' + size;
}
var speed = scriptTag.getAttribute('data-speed');
if (speed) {
params += '&speed=' + speed;
}
container.innerHTML = '<iframe src="http://<%= request.host_with_port %>/a/<%= @asciicast.id %>/raw' + params + '" name="asciicast-iframe-<%= @asciicast.id %>" id="asciicast-iframe-<%= @asciicast.id %>" width="300" height="300" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="display: block; float: none; visibility: hidden;" onload="this.style.visibility=\'visible\';"></iframe>';
}
})();