@@ -316,7 +316,7 @@ Code.checkDeviceOnline = function (device) {
316316 device . inputArea . className = device . inputArea . className + "open" ;
317317 }
318318
319- }
319+ } ;
320320
321321Code . copyCode = function ( copy ) {
322322 copy = { } ;
@@ -339,7 +339,7 @@ Code.copyCode = function (copy) {
339339 document . getElementById ( 'tab_blocks' ) . addEventListener ( 'click' , function ( ) {
340340 copy . copyBtn . style . display = 'none' ;
341341 } ) ;
342- }
342+ } ;
343343
344344Code . loadDemoArea = function ( ) {
345345 var area = document . getElementById ( 'demo-area' ) ;
@@ -492,7 +492,7 @@ Code.loadSample = function () {
492492 }
493493
494494 } ;
495- }
495+ } ;
496496
497497/**
498498 * User's language (e.g. "en").
@@ -504,6 +504,8 @@ Code.PAGE = Code.getPage();
504504
505505Code . running = false ;
506506
507+ Code . sandboxLoaded = true ;
508+
507509Code . lastRun = 0 ;
508510
509511/**
@@ -587,7 +589,7 @@ Code.ga = function (blockArea, toolManu, i) {
587589 ga ( 'send' , 'event' , 'Webduino-blockly' , 'menu click' , thisID ) ;
588590 } ) ;
589591 }
590- }
592+ } ;
591593
592594/**
593595 * Populate the currently selected pane with content generated from the blocks.
@@ -718,31 +720,6 @@ Code.init = function(toolbox) {
718720 }
719721
720722 onresize ( ) ;
721-
722- var keyboardDelegator = function ( e ) {
723- if ( Code . running ) {
724- var frame = document . getElementById ( 'demo-frame' ) ,
725- doc = frame . contentWindow . document ,
726- event = doc . createEvent ( 'Event' ) ;
727-
728- event . initEvent ( e . type , true , true ) ;
729- event . key = e . key ;
730- event . keyCode = e . keyCode ;
731- doc . body . dispatchEvent ( event ) ;
732- }
733- } ;
734- window . addEventListener ( 'keydown' , keyboardDelegator , false ) ;
735- window . addEventListener ( 'keyup' , keyboardDelegator , false ) ;
736-
737- var messageDelegator = function ( e ) {
738- var frame = document . getElementById ( 'demo-frame' ) ,
739- msg = e . data ;
740-
741- if ( msg . jsonrpc && msg . result ) {
742- frame . contentWindow . postMessage ( msg , window . location . origin ) ;
743- }
744- } ;
745- window . addEventListener ( 'message' , messageDelegator , false ) ;
746723} ;
747724
748725Code . renderPage = function ( templateStr ) {
@@ -811,6 +788,10 @@ Code.initLanguage = function() {
811788Code . runJS = function ( ) {
812789 var now = Date . now ( ) ;
813790
791+ if ( ! Code . sandboxLoaded ) {
792+ return ;
793+ }
794+
814795 if ( navigator . userAgent . match ( / i P h o n e / i) ||
815796 navigator . userAgent . match ( / i P a d / i) ||
816797 navigator . userAgent . match ( / i P o d / i) ) {
@@ -864,6 +845,7 @@ Code.toggleRunning = function () {
864845Code . reloadSandbox = function ( ) {
865846 var container = document . querySelector ( '.demo-area-content' ) ;
866847 var ctx = Code . getContext ( ) ;
848+ Code . sandboxLoaded = false ;
867849
868850 launcher . loadTemplate ( './templates/' + ctx . tpl + '.html' , function ( data ) {
869851 if ( Code . running ) {
@@ -884,6 +866,7 @@ Code.reloadSandbox = function () {
884866 frame . contentWindow . dispatchEvent ( event ) ;
885867
886868 setTimeout ( function ( ) {
869+ Code . unhookEvents ( window , frame ) ;
887870 container . removeChild ( frame ) ;
888871 frame = null ;
889872 } , 50 ) ;
@@ -897,20 +880,67 @@ Code.reloadSandbox = function () {
897880 frame . style . display = 'block' ;
898881 container . appendChild ( frame ) ;
899882 Code . tabClick ( 'blocks' ) ;
900-
883+ frame . addEventListener ( 'load' , function ( ) {
884+ Code . sandboxLoaded = true ;
885+ } ) ;
901886 launcher . sandbox ( frame , data ) ;
902887
903- frame . contentWindow . addEventListener ( 'message' , function ( e ) {
904- var msg = e . data ;
905-
906- if ( msg . jsonrpc && msg . method ) {
907- window . postMessage ( msg , window . location . origin ) ;
908- }
909- } , false ) ;
888+ if ( Code . running ) {
889+ Code . hookEvents ( window , frame ) ;
890+ }
910891 }
911892 } ) ;
912893} ;
913894
895+ Code . hookEvents = function ( window , frame ) {
896+ window . keyDispatcher = function ( e ) {
897+ var doc = frame . contentWindow . document ,
898+ event = doc . createEvent ( 'Event' ) ;
899+
900+ event . initEvent ( e . type , true , true ) ;
901+ event . key = e . key ;
902+ event . keyCode = e . keyCode ;
903+ doc . body && doc . body . dispatchEvent ( event ) ;
904+ } ;
905+
906+ window . msgDispatcher = function ( e ) {
907+ var msg = e . data ;
908+
909+ if ( msg . jsonrpc && msg . result ) {
910+ frame . contentWindow . postMessage ( msg , window . location . origin ) ;
911+ }
912+ } ;
913+
914+ frame . msgDispatcher = function ( e ) {
915+ var msg = e . data ;
916+
917+ if ( msg . jsonrpc && msg . method ) {
918+ window . postMessage ( msg , window . location . origin ) ;
919+ }
920+ } ;
921+
922+ window . addEventListener ( 'keydown' , window . keyDispatcher , false ) ;
923+ window . addEventListener ( 'keyup' , window . keyDispatcher , false ) ;
924+ window . addEventListener ( 'message' , window . msgDispatcher , false ) ;
925+ frame . contentWindow . addEventListener ( 'message' , frame . msgDispatcher , false ) ;
926+ } ;
927+
928+ Code . unhookEvents = function ( window , frame ) {
929+ if ( frame . msgDispatcher ) {
930+ frame . contentWindow . removeEventListener ( 'message' , frame . msgDispatcher , false ) ;
931+ delete frame . msgDispatcher ;
932+ }
933+ if ( window . msgDispatcher ) {
934+ window . removeEventListener ( 'message' , window . msgDispatcher , false ) ;
935+ delete window . msgDispatcher ;
936+ }
937+ if ( window . keyDispatcher ) {
938+ window . removeEventListener ( 'keyup' , window . keyDispatcher , false ) ;
939+ window . removeEventListener ( 'keydown' , window . keyDispatcher , false ) ;
940+ delete window . keyDispatcher ;
941+ }
942+ } ;
943+
914944Code . getContext = function ( ) {
915945 var code = Blockly . JavaScript . workspaceToCode ( Code . workspace ) ,
916946 babelize = code . indexOf ( 'async function' ) !== - 1 ,
@@ -1010,7 +1040,7 @@ Blockly.JavaScript['procedures_callreturn'] = function (block) {
10101040 return [ 'await ' + codes [ 0 ] , codes [ 1 ] ] ;
10111041 }
10121042 return codes ;
1013- }
1043+ } ;
10141044
10151045Blockly . JavaScript [ '_procedures_callnoreturn' ] = Blockly . JavaScript [ 'procedures_callnoreturn' ] ;
10161046
0 commit comments