@@ -96,8 +96,10 @@ public Object execute(final String scriptBody, final Object[] parameters) {
9696
9797 private Function createInjectedScriptFunction (final String userScript , final AsyncScriptResult asyncResult ) {
9898 final String script =
99- "let huDriverAsyncFoo = function() {"
99+ "let huDriverAsyncFoo = "
100+ + "function() {"
100101 + " var self = this, timeoutId;"
102+
101103 + " var cleanUp = function() {"
102104 + " window.clearTimeout(timeoutId);"
103105 + " if (window.detachEvent) {"
@@ -106,36 +108,36 @@ private Function createInjectedScriptFunction(final String userScript, final Asy
106108 + " window.removeEventListener('unload', catchUnload, false);"
107109 + " }"
108110 + " };"
111+
109112 + " var self = this, timeoutId, catchUnload = function() {"
110113 + " cleanUp();"
111114 + " self.host.unload();"
112115 + " };"
116+
113117 // Convert arguments into an actual array, then add the callback object.
114118 + " arguments = Array.prototype.slice.call(arguments, 0);"
115119 + " arguments.push(function(value) {"
116120 + " cleanUp();"
117121 + " self.host.callback(typeof value == 'undefined' ? null : value);"
118122 + " });"
123+
119124 // Add an event listener to trap unload events; page loads are not supported
120125 // with async script execution.
121126 + " if (window.attachEvent) {"
122127 + " window.attachEvent('onunload', catchUnload);"
123128 + " } else {"
124129 + " window.addEventListener('unload', catchUnload, false);"
125130 + " }"
131+
126132 // Execute the user's script
127- + " (function() {"
128- + userScript
129- + "}).apply(null, arguments);"
133+ + " (function() {" + userScript + "}).apply(null, arguments);"
134+
130135 // Register our timeout for the script. If the script invokes the callback
131136 // immediately (e.g. it's not really async), then this will still fire.
132137 // That's OK because the host object should ignore the extra timeout.
133- + " timeoutId = window.setTimeout(function() {"
134- + " self.host.timeout();"
135- + " }, "
136- + timeoutMillis_
137- + ");"
138- + "}; huDriverAsyncFoo;" ;
138+ + " timeoutId = window.setTimeout(function() { self.host.timeout(); }, " + timeoutMillis_ + ");"
139+ + "}; "
140+ + "huDriverAsyncFoo;" ;
139141
140142 // Compile our script.
141143 final ScriptResult result = page_ .executeJavaScript (script );
0 commit comments