6565import org .htmlunit .corejs .javascript .Function ;
6666import org .htmlunit .corejs .javascript .Script ;
6767import org .htmlunit .corejs .javascript .Scriptable ;
68+ import org .htmlunit .corejs .javascript .ScriptableObject ;
6869import org .htmlunit .css .ComputedCssStyleDeclaration ;
6970import org .htmlunit .css .CssStyleSheet ;
7071import org .htmlunit .html .FrameWindow .PageDenied ;
@@ -1005,9 +1006,11 @@ JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final
10051006 return JavaScriptLoadResult .NOOP ;
10061007 }
10071008
1009+ final AbstractJavaScriptEngine <Object > engine = (AbstractJavaScriptEngine <Object >) client .getJavaScriptEngine ();
1010+ final ScriptableObject scope = getEnclosingWindow ().getScriptableObject ();
10081011 final Object script ;
10091012 try {
1010- script = loadJavaScriptFromUrl (scriptURL , scriptCharset );
1013+ script = loadJavaScriptFromUrl (scriptURL , scriptCharset , engine , scope );
10111014 }
10121015 catch (final IOException e ) {
10131016 client .getJavaScriptErrorListener ().loadScriptError (this , scriptURL , e );
@@ -1025,9 +1028,7 @@ JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final
10251028 return JavaScriptLoadResult .COMPILATION_ERROR ;
10261029 }
10271030
1028- @ SuppressWarnings ("unchecked" )
1029- final AbstractJavaScriptEngine <Object > engine = (AbstractJavaScriptEngine <Object >) client .getJavaScriptEngine ();
1030- engine .execute (this , getEnclosingWindow ().getScriptableObject (), script );
1031+ engine .execute (this , scope , script );
10311032 return JavaScriptLoadResult .SUCCESS ;
10321033 }
10331034
@@ -1037,13 +1038,16 @@ JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final
10371038 *
10381039 * @param url the URL of the script
10391040 * @param scriptCharset the charset from the script tag
1041+ * @param engine the js engine
1042+ * @param scope the scope
10401043 * @return the content of the file, or {@code null} if we ran into a compile error
10411044 * @throws IOException if there is a problem downloading the JavaScript file
10421045 * @throws FailingHttpStatusCodeException if the request's status code indicates a request
10431046 * failure and the {@link WebClient} was configured to throw exceptions on failing
10441047 * HTTP status codes
10451048 */
1046- private Object loadJavaScriptFromUrl (final URL url , final Charset scriptCharset ) throws IOException ,
1049+ private Object loadJavaScriptFromUrl (final URL url , final Charset scriptCharset ,
1050+ final AbstractJavaScriptEngine <?> engine , final Scriptable scope ) throws IOException ,
10471051 FailingHttpStatusCodeException {
10481052
10491053 final WebRequest referringRequest = getWebResponse ().getWebRequest ();
@@ -1099,12 +1103,12 @@ private Object loadJavaScriptFromUrl(final URL url, final Charset scriptCharset)
10991103 final String contentType = response .getContentType ();
11001104 if (contentType != null ) {
11011105 if (MimeType .isObsoleteJavascriptMimeType (contentType )) {
1102- getWebClient () .getIncorrectnessListener ().notify (
1106+ client .getIncorrectnessListener ().notify (
11031107 "Obsolete content type encountered: '" + contentType + "' "
11041108 + "for remotely loaded JavaScript element at '" + url + "'." , this );
11051109 }
11061110 else if (!MimeType .isJavascriptMimeType (contentType )) {
1107- getWebClient () .getIncorrectnessListener ().notify (
1111+ client .getIncorrectnessListener ().notify (
11081112 "Expect content type of '" + MimeType .TEXT_JAVASCRIPT + "' "
11091113 + "for remotely loaded JavaScript element at '" + url + "', "
11101114 + "but got '" + contentType + "'." , this );
@@ -1114,9 +1118,7 @@ else if (!MimeType.isJavascriptMimeType(contentType)) {
11141118 final Charset scriptEncoding = response .getContentCharset ();
11151119 final String scriptCode = response .getContentAsString (scriptEncoding );
11161120 if (null != scriptCode ) {
1117- final AbstractJavaScriptEngine <?> javaScriptEngine = client .getJavaScriptEngine ();
1118- final Scriptable scope = getEnclosingWindow ().getScriptableObject ();
1119- final Object script = javaScriptEngine .compile (this , scope , scriptCode , url .toExternalForm (), 1 );
1121+ final Object script = engine .compile (this , scope , scriptCode , url .toExternalForm (), 1 );
11201122 if (script != null && cache .cacheIfPossible (request , response , script )) {
11211123 // no cleanup if the response is stored inside the cache
11221124 return script ;
0 commit comments