From 7f8633eb2a934ebfcd3810ab10b96ab581807504 Mon Sep 17 00:00:00 2001 From: Pengyu Chen Date: Fri, 19 Oct 2018 00:01:03 +0100 Subject: [PATCH 1/4] Added: Userspace example of waiting for a specific condition. --- docs/scripting-ref.rst | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/docs/scripting-ref.rst b/docs/scripting-ref.rst index 224707a2c..6ab1a2cf4 100644 --- a/docs/scripting-ref.rst +++ b/docs/scripting-ref.rst @@ -419,6 +419,51 @@ time after each page load in case of redirects: return nil, "too_many_redirects" end +It's also common practice to wait for a specific condition, other than a fixed +amount of time: + +.. code-block:: lua + + function wait_until(splash, timeout, polling_interval, check_func, ...) + -- XXX: polling_interval shall not be too small + local ok, result1, result2 = splash:with_timeout(function() + while true do + local ok, reason = splash:wait(polling_interval) + if not ok then + return ok, string.format('wait failed: %s', reason) + end + local check_result = check_func(...) + if check_result then + return true, check_result + end + end + end, timeout) + if not ok then + return nil, string.format('timeout exceeded: %s', result1) + end + return result1, result2 + end + + function main(splash) + -- Goto example.com and wait for a specific node to be loaded in the DOM + splash:go('http://example.com') + wait_until(splash, 10, 0.1, splash.select, splash, 'div#example_selector') + + -- Goto example.com and wait for a specific response to be downloaded + local example_response_downloaded = false + splash:on_response(function (response) + if string.find(response.url, 'specific_url_pattern_here') then + example_response_downloaded = true + end + end) + splash:go('http://example.com') + wait_until( + splash, 10, 0.1, + function () + return example_response_downloaded + end + ) + end .. _splash-jsfunc: From 3a9fc3e0db26f569a7044c784c9b15ed0bdfe697 Mon Sep 17 00:00:00 2001 From: Pengyu Chen Date: Fri, 22 Feb 2019 11:07:38 +0000 Subject: [PATCH 2/4] Fixed: Incorrectly handled variable length arguments in the code example --- docs/scripting-ref.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/scripting-ref.rst b/docs/scripting-ref.rst index 6ab1a2cf4..384b91c34 100644 --- a/docs/scripting-ref.rst +++ b/docs/scripting-ref.rst @@ -426,13 +426,14 @@ amount of time: function wait_until(splash, timeout, polling_interval, check_func, ...) -- XXX: polling_interval shall not be too small + local vargs = ... local ok, result1, result2 = splash:with_timeout(function() while true do local ok, reason = splash:wait(polling_interval) if not ok then return ok, string.format('wait failed: %s', reason) end - local check_result = check_func(...) + local check_result = check_func(vargs) if check_result then return true, check_result end From 00dee4a1791051db9d68620d90237f4fcd351f64 Mon Sep 17 00:00:00 2001 From: Pengyu Chen Date: Thu, 4 Apr 2019 01:12:37 +0100 Subject: [PATCH 3/4] Fixed the previously introduced (stupid) issue of missing the unpack function --- docs/scripting-ref.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/scripting-ref.rst b/docs/scripting-ref.rst index 384b91c34..e6a00a5e6 100644 --- a/docs/scripting-ref.rst +++ b/docs/scripting-ref.rst @@ -426,14 +426,14 @@ amount of time: function wait_until(splash, timeout, polling_interval, check_func, ...) -- XXX: polling_interval shall not be too small - local vargs = ... + local vargs = {...} local ok, result1, result2 = splash:with_timeout(function() while true do local ok, reason = splash:wait(polling_interval) if not ok then return ok, string.format('wait failed: %s', reason) end - local check_result = check_func(vargs) + local check_result = check_func(table.unpack(vargs)) if check_result then return true, check_result end From 03b01b71e49465fd08932de369d4ee4f826b26bc Mon Sep 17 00:00:00 2001 From: Pengyu Chen Date: Thu, 4 Apr 2019 02:13:31 +0100 Subject: [PATCH 4/4] Fixed indentation --- docs/scripting-ref.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/scripting-ref.rst b/docs/scripting-ref.rst index e6a00a5e6..38449f62a 100644 --- a/docs/scripting-ref.rst +++ b/docs/scripting-ref.rst @@ -429,14 +429,14 @@ amount of time: local vargs = {...} local ok, result1, result2 = splash:with_timeout(function() while true do - local ok, reason = splash:wait(polling_interval) - if not ok then - return ok, string.format('wait failed: %s', reason) - end - local check_result = check_func(table.unpack(vargs)) - if check_result then - return true, check_result - end + local ok, reason = splash:wait(polling_interval) + if not ok then + return ok, string.format('wait failed: %s', reason) + end + local check_result = check_func(table.unpack(vargs)) + if check_result then + return true, check_result + end end end, timeout) if not ok then