<!DOCTYPE HTML>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="{{viewport}}">
  <title>Sparkle Preview</title>
  <script language="javascript" type="text/javascript">
//<![CDATA[
var wsUri = "{{ws}}";

function init() {
    start();
    ws.onopen = function (evt) {
        window.clearInterval(window.timerID);
        window.timerID=0;
    };
    ws.onclose = function (evt) {
        restart();
    };
    ws.onmessage = function (evt) {
        if(evt.data == "reload") {
            document.location.reload();
        } else {
            document.getElementById('content').contentWindow.location = evt.data;
        }
    };
    ws.onerror = function (evt) {
        restart();
    };
}
  
function restart() {
    if(! window.timerID) {
        window.timerID=setInterval(function(){ ws.close(); init()}, 500);
    }
}

function start() {
    window.clearInterval(window.timerID);
    window.timerID=0;
    ws = new WebSocket(wsUri);
    restart();
}

window.addEventListener("load", init, false);
  //]]>
  </script>
<style>
body { margin: 0; padding: 0; width: 100%; height: 100%; }
#content { position: absolute; padding: 0; border: 0; width: 100%; height: 100%; }
</style>
</head>

<body>
<iframe id="content" src="{{iframe}}">
</iframe>
</body>
</html>
