From 6f4b1e4071192589f40047d26407a4fce6386578 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Wed, 17 Oct 2012 11:47:07 -0500 Subject: [PATCH] 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. --- debian/novnc.install | 2 -- include/rfb.js | 5 +++-- include/start.js | 1 - include/ui.js | 5 +++++ include/util.js | 39 +++++++++++++++++++++++++++++++++++++++ include/vnc.js | 35 ----------------------------------- include/websock.js | 15 +++------------ tests/vnc_perf.html | 8 ++++++-- tests/vnc_playback.html | 13 ++++++++----- vnc.html | 7 +++---- vnc_auto.html | 8 ++++++-- 11 files changed, 73 insertions(+), 65 deletions(-) delete mode 100644 include/start.js delete mode 100644 include/vnc.js diff --git a/debian/novnc.install b/debian/novnc.install index 9f6a3260..92867bce 100644 --- a/debian/novnc.install +++ b/debian/novnc.install @@ -23,9 +23,7 @@ include/playback.js /usr/share/novnc/include include/rfb.js /usr/share/novnc/include include/ui.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/webutil.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 diff --git a/include/rfb.js b/include/rfb.js index 2110b7af..5b46c12b 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -1841,15 +1841,16 @@ that.clipboardPasteFrom = function(text) { }; // Override internal functions for testing -that.testMode = function(override_send) { +that.testMode = function(override_send, data_mode) { test_mode = true; - that.recv_message = ws.testMode(override_send); + that.recv_message = ws.testMode(override_send, data_mode); checkEvents = function () { /* Stub Out */ }; that.connect = function(host, port, password) { rfb_host = host; rfb_port = port; rfb_password = password; + init_vars(); updateState('ProtocolVersion', "Starting VNC handshake"); }; }; diff --git a/include/start.js b/include/start.js deleted file mode 100644 index b5d90b4c..00000000 --- a/include/start.js +++ /dev/null @@ -1 +0,0 @@ -window.onload = UI.load; diff --git a/include/ui.js b/include/ui.js index 44e91628..f6eda04c 100644 --- a/include/ui.js +++ b/include/ui.js @@ -10,6 +10,11 @@ /*jslint white: false, browser: true */ /*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 = { rfb_state : 'loaded', diff --git a/include/util.js b/include/util.js index fc9242e1..7d1e4c02 100644 --- a/include/util.js +++ b/include/util.js @@ -207,6 +207,45 @@ Util.conf_defaults = function(cfg, api, defaults, arr) { * 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= 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 Util.getPosition = function (obj) { var x = 0, y = 0; diff --git a/include/vnc.js b/include/vnc.js deleted file mode 100644 index bb4c3ad0..00000000 --- a/include/vnc.js +++ /dev/null @@ -1,35 +0,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 var INCLUDE_URI= "../include/"; - + - - + + --> - - - - @@ -178,5 +174,8 @@ + + + diff --git a/vnc_auto.html b/vnc_auto.html index 2434d97e..a6273256 100644 --- a/vnc_auto.html +++ b/vnc_auto.html @@ -17,7 +17,7 @@ --> - + @@ -41,6 +41,10 @@ /*global window, $, Util, RFB, */ "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; function passwordRequired(rfb) { @@ -84,7 +88,7 @@ } } - window.onload = function () { + window.onscriptsload = function () { var host, port, password, path, token; $D('sendCtrlAltDelButton').style.display = "inline";