From e2e43653c89da022634d0ea5aaff6d8649c4ceca Mon Sep 17 00:00:00 2001 From: RuralHunter Date: Tue, 11 Jan 2022 17:25:21 +0800 Subject: [PATCH] Execute js initialization jobs during html page initialization --- .../htmlunit/html/HtmlPage.java | 36 +++++++++++++++++++ .../htmlunit/javascript/TimeoutError.java | 4 +-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java b/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java index 67d9038897c..229db462f91 100644 --- a/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java +++ b/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java @@ -94,6 +94,9 @@ import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine; import com.gargoylesoftware.htmlunit.javascript.PostponedAction; import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; +import com.gargoylesoftware.htmlunit.javascript.TimeoutError; +import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob; +import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager; import com.gargoylesoftware.htmlunit.javascript.host.Window; import com.gargoylesoftware.htmlunit.javascript.host.css.CSS2Properties; import com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent; @@ -343,6 +346,7 @@ && hasFeature(EVENT_FOCUS_ON_LOAD)) { catch (final Exception e) { throw new RuntimeException(e); } + executeInitializationJobs(enclosingWindow); executeRefreshIfNeeded(); } @@ -2908,4 +2912,36 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot computedStyles_ = new WeakHashMap<>(); } } + + /** + * Executes js jobs usually needed to initialize the page. + * @param webWindow The enclosing window of the page. + */ + private void executeInitializationJobs(WebWindow webWindow) { + if (!getWebClient().isJavaScriptEnabled()) { + return; + } + final JavaScriptJobManager jobManager = webWindow.getJobManager(); + if (jobManager != null) { + int jobsDone = 0; + JavaScriptJob earliestJob = jobManager.getEarliestJob(); + try { + while(earliestJob != null && (earliestJob.isExecuteAsap() || earliestJob.getTargetExecutionTime() <= System.currentTimeMillis())) { + jobManager.runSingleJob(earliestJob); + jobsDone++; + if(jobsDone > 1000) { + LOG.warn("Too many init jobs executed for window " + webWindow + ". Return for now."); + break; + } + earliestJob = jobManager.getEarliestJob(); + } + } catch(TimeoutError e) { + getWebClient().getJavaScriptErrorListener().timeoutError(this, e.getAllowedTime(), e.getExecutionTime()); + if (getWebClient().getOptions().isThrowExceptionOnScriptError()) { + throw new RuntimeException(e); + } + LOG.info("Caught script timeout error", e); + } + } + } } diff --git a/src/main/java/com/gargoylesoftware/htmlunit/javascript/TimeoutError.java b/src/main/java/com/gargoylesoftware/htmlunit/javascript/TimeoutError.java index 4b084ae7f72..c6d1604e05d 100644 --- a/src/main/java/com/gargoylesoftware/htmlunit/javascript/TimeoutError.java +++ b/src/main/java/com/gargoylesoftware/htmlunit/javascript/TimeoutError.java @@ -35,7 +35,7 @@ public class TimeoutError extends Error { * Returns the allowed time. * @return the allowed time */ - long getAllowedTime() { + public long getAllowedTime() { return allowedTime_; } @@ -43,7 +43,7 @@ long getAllowedTime() { * Returns the execution time. * @return the execution time */ - long getExecutionTime() { + public long getExecutionTime() { return executionTime_; } }