Commit 6f4b1e40 authored by Joel Martin's avatar Joel Martin

Refactor dynamic script loading. Add util.js:load_scripts()

Related to issue/pulls:
https://github.com/kanaka/noVNC/issues/194
https://github.com/kanaka/noVNC/pull/201
https://github.com/kanaka/noVNC/pull/202

In IE9, the window.onload event can fire before dynamically loaded
scripts have finished loading. This can result in either WebSocket (in
the case of vnc_auto.html) or RFB (in the case of vnc.html) not being
defined at the point when window.onload is called.

- Move the load_scripts routine from vnc.js to util.js (so that
  websockify can use it too). Also, refactor to work when load_scripts
  is called by a script that itself uses load_scripts. When the whole
  chain of dynamically loaded scripts is finished then call
  window.onscriptsload. Use this mechanism in all the places that
  depend on dynamic loading of scripts: vnc.html, vnc_auto.html,
  websock.js, tests/vnc_playback.html, and tests/vnc_perf.html.

- Use the new window.onscriptsload handler instead of window.onload.

- Remove include/start.js and do the script loading and startup event
  handling in include/ui.js instead.
parent 7a6c94b6
...@@ -23,9 +23,7 @@ include/playback.js /usr/share/novnc/include ...@@ -23,9 +23,7 @@ include/playback.js /usr/share/novnc/include
include/rfb.js /usr/share/novnc/include include/rfb.js /usr/share/novnc/include
include/ui.js /usr/share/novnc/include include/ui.js /usr/share/novnc/include
include/util.js /usr/share/novnc/include include/util.js /usr/share/novnc/include
include/vnc.js /usr/share/novnc/include
include/websock.js /usr/share/novnc/include include/websock.js /usr/share/novnc/include
include/webutil.js /usr/share/novnc/include include/webutil.js /usr/share/novnc/include
include/jsunzip.js /usr/share/novnc/include include/jsunzip.js /usr/share/novnc/include
include/start.js /usr/share/novnc/include
include/web-socket-js/* /usr/share/novnc/include/web-socket-js include/web-socket-js/* /usr/share/novnc/include/web-socket-js
...@@ -1841,15 +1841,16 @@ that.clipboardPasteFrom = function(text) { ...@@ -1841,15 +1841,16 @@ that.clipboardPasteFrom = function(text) {
}; };
// Override internal functions for testing // Override internal functions for testing
that.testMode = function(override_send) { that.testMode = function(override_send, data_mode) {
test_mode = true; test_mode = true;
that.recv_message = ws.testMode(override_send); that.recv_message = ws.testMode(override_send, data_mode);
checkEvents = function () { /* Stub Out */ }; checkEvents = function () { /* Stub Out */ };
that.connect = function(host, port, password) { that.connect = function(host, port, password) {
rfb_host = host; rfb_host = host;
rfb_port = port; rfb_port = port;
rfb_password = password; rfb_password = password;
init_vars();
updateState('ProtocolVersion', "Starting VNC handshake"); updateState('ProtocolVersion', "Starting VNC handshake");
}; };
}; };
......
window.onload = UI.load;
...@@ -10,6 +10,11 @@ ...@@ -10,6 +10,11 @@
/*jslint white: false, browser: true */ /*jslint white: false, browser: true */
/*global window, $D, Util, WebUtil, RFB, Display */ /*global window, $D, Util, WebUtil, RFB, Display */
// Load supporting scripts
window.onscriptsload = function () { UI.load(); };
Util.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js",
"input.js", "display.js", "jsunzip.js", "rfb.js"]);
var UI = { var UI = {
rfb_state : 'loaded', rfb_state : 'loaded',
......
...@@ -207,6 +207,45 @@ Util.conf_defaults = function(cfg, api, defaults, arr) { ...@@ -207,6 +207,45 @@ Util.conf_defaults = function(cfg, api, defaults, arr) {
* Cross-browser routines * Cross-browser routines
*/ */
// Dynamically load scripts without using document.write()
// Reference: http://unixpapa.com/js/dyna.html
//
// Handles the case where load_scripts is invoked from a script that
// itself is loaded via load_scripts. Once all scripts are loaded the
// window.onscriptsloaded handler is called (if set).
Util.get_include_uri = function() {
return (typeof INCLUDE_URI !== "undefined") ? INCLUDE_URI : "include/";
}
Util._pending_scripts = [];
Util.load_scripts = function(files) {
var head = document.getElementsByTagName('head')[0],
ps = Util._pending_scripts;
for (var f=0; f<files.length; f++) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = Util.get_include_uri() + files[f];
//console.log("loading script: " + Util.get_include_uri() + files[f]);
head.appendChild(script);
ps.push(script);
script.onload = script.onreadystatechange = function (e) {
if (!this.readyState ||
this.readyState == 'complete' ||
this.readyState == 'loaded') {
this.onload = this.onreadystatechange = null;
if (ps.indexOf(this) >= 0) {
//console.log("loaded script: " + this.src);
ps.splice(ps.indexOf(this), 1);
}
// Call window.onscriptsload after last script loads
if (ps.length === 0 && window.onscriptsload) {
window.onscriptsload();
}
}
}
}
}
// Get DOM element position on page // Get DOM element position on page
Util.getPosition = function (obj) { Util.getPosition = function (obj) {
var x = 0, y = 0; var x = 0, y = 0;
......
/*
* noVNC: HTML5 VNC client
* Copyright (C) 2012 Joel Martin
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* See README.md for usage and integration instructions.
*/
/*jslint evil: true */
/*global window, document, INCLUDE_URI */
/*
* Load supporting scripts
*/
function get_INCLUDE_URI() {
return (typeof INCLUDE_URI !== "undefined") ? INCLUDE_URI : "include/";
}
/*
* Dynamically load a script without using document.write()
* Reference: http://unixpapa.com/js/dyna.html
*/
function load_scripts(base, files) {
var head = document.getElementsByTagName('head')[0];
for (var i=0; i<files.length; i++) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = base + files[i];
head.appendChild(script);
}
}
load_scripts(get_INCLUDE_URI(),
["util.js", "webutil.js", "base64.js", "websock.js", "des.js",
"input.js", "display.js", "rfb.js", "jsunzip.js"]);
...@@ -35,23 +35,14 @@ if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) { ...@@ -35,23 +35,14 @@ if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) {
Websock_native = false; Websock_native = false;
(function () { (function () {
function get_INCLUDE_URI() { window.WEB_SOCKET_SWF_LOCATION = Util.get_include_uri() +
return (typeof INCLUDE_URI !== "undefined") ?
INCLUDE_URI : "include/";
}
var start = "<script src='" + get_INCLUDE_URI(),
end = "'><\/script>", extra = "";
window.WEB_SOCKET_SWF_LOCATION = get_INCLUDE_URI() +
"web-socket-js/WebSocketMain.swf"; "web-socket-js/WebSocketMain.swf";
if (Util.Engine.trident) { if (Util.Engine.trident) {
Util.Debug("Forcing uncached load of WebSocketMain.swf"); Util.Debug("Forcing uncached load of WebSocketMain.swf");
window.WEB_SOCKET_SWF_LOCATION += "?" + Math.random(); window.WEB_SOCKET_SWF_LOCATION += "?" + Math.random();
} }
extra += start + "web-socket-js/swfobject.js" + end; Util.load_scripts(["web-socket-js/swfobject.js",
extra += start + "web-socket-js/web_socket.js" + end; "web-socket-js/web_socket.js"]);
document.write(extra);
}()); }());
} }
......
...@@ -39,11 +39,15 @@ ...@@ -39,11 +39,15 @@
<script type="text/javascript"> <script type="text/javascript">
var INCLUDE_URI= "../include/"; var INCLUDE_URI= "../include/";
</script> </script>
<script src="../include/vnc.js"></script> <script src="../include/util.js"></script>
<script src="../include/playback.js"></script> <script src="../include/playback.js"></script>
<script src="../data/multi.js"></script> <script src="../data/multi.js"></script>
<script> <script>
// Load supporting scripts
Util.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js",
"input.js", "display.js", "jsunzip.js", "rfb.js"]);
var start_time, VNC_frame_data, pass, passes, encIdx, var start_time, VNC_frame_data, pass, passes, encIdx,
encOrder = ['raw', 'rre', 'hextile', 'tightpng', 'copyrect'], encOrder = ['raw', 'rre', 'hextile', 'tightpng', 'copyrect'],
encTot = {}, encMin = {}, encMax = {}, encTot = {}, encMin = {}, encMax = {},
...@@ -188,7 +192,7 @@ ...@@ -188,7 +192,7 @@
" (min/avg/max)"); " (min/avg/max)");
} }
window.onload = function() { window.onscriptsload = function() {
var i, enc; var i, enc;
dbgmsg("Frame lengths:"); dbgmsg("Frame lengths:");
for (i = 0; i < encOrder.length; i++) { for (i = 0; i < encOrder.length; i++) {
......
...@@ -41,8 +41,8 @@ ...@@ -41,8 +41,8 @@
<script type="text/javascript"> <script type="text/javascript">
var INCLUDE_URI= "../include/"; var INCLUDE_URI= "../include/";
</script> </script>
<script src="../include/vnc.js"></script> <script src="../include/util.js"></script>
<script src="../include/playback.js"></script> <script src="../include/webutil.js"></script>
<script> <script>
var fname, start_time; var fname, start_time;
...@@ -55,10 +55,13 @@ ...@@ -55,10 +55,13 @@
} }
fname = WebUtil.getQueryVar('data', null); fname = WebUtil.getQueryVar('data', null);
if (fname) { if (fname) {
message("Loading " + fname); message("Loading " + fname);
document.write('<script src="' + fname + '"><\/script>'); // Load supporting scripts
Util.load_scripts(["base64.js", "websock.js", "des.js",
"input.js", "display.js", "jsunzip.js", "rfb.js",
"playback.js", fname]);
} else { } else {
message("Must specify data=FOO in query string."); message("Must specify data=FOO in query string.");
} }
...@@ -115,7 +118,7 @@ ...@@ -115,7 +118,7 @@
} }
window.onload = function() { window.onscriptsload = function () {
iterations = WebUtil.getQueryVar('iterations', 3); iterations = WebUtil.getQueryVar('iterations', 3);
$D('iterations').value = iterations; $D('iterations').value = iterations;
mode = WebUtil.getQueryVar('mode', 3); mode = WebUtil.getQueryVar('mode', 3);
......
...@@ -39,10 +39,6 @@ ...@@ -39,10 +39,6 @@
src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script> src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
--> -->
<script src="include/vnc.js"></script>
<script src="include/ui.js"></script>
<script src="include/start.js"></script>
</head> </head>
<body> <body>
...@@ -178,5 +174,8 @@ ...@@ -178,5 +174,8 @@
</div> </div>
</div> </div>
<script src="include/util.js"></script>
<script src="include/ui.js"></script>
</body> </body>
</html> </html>
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<script type='text/javascript' <script type='text/javascript'
src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script> src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
--> -->
<script src="include/vnc.js"></script> <script src="include/util.js"></script>
</head> </head>
<body style="margin: 0px;"> <body style="margin: 0px;">
...@@ -41,6 +41,10 @@ ...@@ -41,6 +41,10 @@
/*global window, $, Util, RFB, */ /*global window, $, Util, RFB, */
"use strict"; "use strict";
// Load supporting scripts
Util.load_scripts(["webutil.js", "base64.js", "websock.js", "des.js",
"input.js", "display.js", "jsunzip.js", "rfb.js"]);
var rfb; var rfb;
function passwordRequired(rfb) { function passwordRequired(rfb) {
...@@ -84,7 +88,7 @@ ...@@ -84,7 +88,7 @@
} }
} }
window.onload = function () { window.onscriptsload = function () {
var host, port, password, path, token; var host, port, password, path, token;
$D('sendCtrlAltDelButton').style.display = "inline"; $D('sendCtrlAltDelButton').style.display = "inline";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment