]> prime8.dev >> repos - ttyd.git/commitdiff
Add confirm for window closing
authorShuanglei Tao <tsl0922@gmail.com>
Sat, 4 Feb 2017 14:02:44 +0000 (22:02 +0800)
committerShuanglei Tao <tsl0922@gmail.com>
Sun, 5 Feb 2017 10:06:32 +0000 (18:06 +0800)
html/js/app.js
src/index.html

index da8ddad9914433bef70cfba25b587c96d5204d35..ad1687d2201972501cdb6841c525055c0a5b16f6 100644 (file)
@@ -9,6 +9,11 @@
 
     var openWs = function() {
         var ws = new WebSocket(url, protocols);
+        var unloadCallback = function(event) {
+            var message = 'Close terminal? this will also terminate the command.';
+            (event || window.event).returnValue = message;
+            return message;
+        };
 
         ws.onopen = function(event) {
             console.log("Websocket connection opened");
             });
 
             term.on('open', function() {
-                window.onresize = function(event) {
+                window.addEventListener('resize', function(event) {
                     term.fit();
-                };
+                });
+                window.addEventListener('beforeunload', unloadCallback);
                 term.fit();
                 term.focus();
             });
@@ -86,6 +92,7 @@
                     term.showOverlay("Connection Closed", null);
                 }
             }
+            window.removeEventListener('beforeunload', unloadCallback);
             clearInterval(pingTimer);
             if (autoReconnect > 0) {
                 setTimeout(openWs, autoReconnect * 1000);
index a0c65aa4025f184e2dddfc645cef7ef3bd500c2a..0bb92bdb0406110ce2047a1188e0c859e3e4d61e 100644 (file)
@@ -16,6 +16,6 @@ break;case 33:t.shiftKey?e.scrollDisp=-(this.rows-1):e.key="\e[5~";break;case 34:
 <body>
        <div id="terminal-container"></div>
        <script src="auth_token.js"></script>
-       <script>!function(){var e,o,n,t=document.getElementById("terminal-container"),c="https:"==window.location.protocol,s=(c?"wss://":"ws://")+window.location.host+window.location.pathname+"ws",i="undefined"!=typeof tty_auth_token?tty_auth_token:null,a=["tty"],r=-1,l=function(){var c=new WebSocket(s,a);c.onopen=function(s){for(console.log("Websocket connection opened"),n=!1,c.send(JSON.stringify({AuthToken:i})),o=setInterval(d,3e4,c),"undefined"!=typeof e&&e.destroy(),e=new Terminal,e.on("resize",function(o){c.readyState===WebSocket.OPEN&&c.send("2"+JSON.stringify({columns:o.cols,rows:o.rows})),setTimeout(function(){e.showOverlay(o.cols+"x"+o.rows)},500)}),e.on("data",function(e){c.readyState===WebSocket.OPEN&&c.send("0"+e)}),e.on("open",function(){window.onresize=function(o){e.fit()},e.fit(),e.focus()});t.firstChild;)t.removeChild(t.firstChild);e.open(t)},c.onmessage=function(o){var n=o.data.slice(1);switch(o.data[0]){case"0":e.writeUTF8(window.atob(n));break;case"1":break;case"2":document.title=n;break;case"3":var t=JSON.parse(n);Object.keys(t).forEach(function(o){console.log("Setting "+o+": "+t[o]),e.setOption(o,t[o])});break;case"4":r=JSON.parse(n),console.log("Enabling reconnect: "+r+" seconds")}},c.onclose=function(t){console.log("Websocket connection closed with code: "+t.code),e&&(e.off("data"),e.off("resize"),n||e.showOverlay("Connection Closed",null)),clearInterval(o),r>0&&setTimeout(l,1e3*r)}},d=function(e){e.send("1")};l()}();</script>
+       <script>!function(){var e,n,o,t=document.getElementById("terminal-container"),i="https:"==window.location.protocol,s=(i?"wss://":"ws://")+window.location.host+window.location.pathname+"ws",a="undefined"!=typeof tty_auth_token?tty_auth_token:null,c=["tty"],r=-1,l=function(){var i=new WebSocket(s,c),f=function(e){var n="Close terminal? this will also terminate the command.";return(e||window.event).returnValue=n,n};i.onopen=function(s){for(console.log("Websocket connection opened"),o=!1,i.send(JSON.stringify({AuthToken:a})),n=setInterval(d,3e4,i),"undefined"!=typeof e&&e.destroy(),e=new Terminal,e.on("resize",function(n){i.readyState===WebSocket.OPEN&&i.send("2"+JSON.stringify({columns:n.cols,rows:n.rows})),setTimeout(function(){e.showOverlay(n.cols+"x"+n.rows)},500)}),e.on("data",function(e){i.readyState===WebSocket.OPEN&&i.send("0"+e)}),e.on("open",function(){window.addEventListener("resize",function(n){e.fit()}),window.addEventListener("beforeunload",f),e.fit(),e.focus()});t.firstChild;)t.removeChild(t.firstChild);e.open(t)},i.onmessage=function(n){var o=n.data.slice(1);switch(n.data[0]){case"0":e.writeUTF8(window.atob(o));break;case"1":break;case"2":document.title=o;break;case"3":var t=JSON.parse(o);Object.keys(t).forEach(function(n){console.log("Setting "+n+": "+t[n]),e.setOption(n,t[n])});break;case"4":r=JSON.parse(o),console.log("Enabling reconnect: "+r+" seconds")}},i.onclose=function(t){console.log("Websocket connection closed with code: "+t.code),e&&(e.off("data"),e.off("resize"),o||e.showOverlay("Connection Closed",null)),window.removeEventListener("beforeunload",f),clearInterval(n),r>0&&setTimeout(l,1e3*r)}},d=function(e){e.send("1")};l()}();</script>
 </body>
 </html>