Skip to content

Commit 7f8633e

Browse files
committed
Added: Userspace example of waiting for a specific condition.
1 parent a99be97 commit 7f8633e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

docs/scripting-ref.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,51 @@ time after each page load in case of redirects:
419419
return nil, "too_many_redirects"
420420
end
421421
422+
It's also common practice to wait for a specific condition, other than a fixed
423+
amount of time:
424+
425+
.. code-block:: lua
426+
427+
function wait_until(splash, timeout, polling_interval, check_func, ...)
428+
-- XXX: polling_interval shall not be too small
429+
local ok, result1, result2 = splash:with_timeout(function()
430+
while true do
431+
local ok, reason = splash:wait(polling_interval)
432+
if not ok then
433+
return ok, string.format('wait failed: %s', reason)
434+
end
435+
local check_result = check_func(...)
436+
if check_result then
437+
return true, check_result
438+
end
439+
end
440+
end, timeout)
441+
if not ok then
442+
return nil, string.format('timeout exceeded: %s', result1)
443+
end
444+
return result1, result2
445+
end
446+
447+
function main(splash)
448+
-- Goto example.com and wait for a specific node to be loaded in the DOM
449+
splash:go('http://example.com')
450+
wait_until(splash, 10, 0.1, splash.select, splash, 'div#example_selector')
451+
452+
-- Goto example.com and wait for a specific response to be downloaded
453+
local example_response_downloaded = false
454+
splash:on_response(function (response)
455+
if string.find(response.url, 'specific_url_pattern_here') then
456+
example_response_downloaded = true
457+
end
458+
end)
459+
splash:go('http://example.com')
460+
wait_until(
461+
splash, 10, 0.1,
462+
function ()
463+
return example_response_downloaded
464+
end
465+
)
466+
end
422467
423468
.. _splash-jsfunc:
424469

0 commit comments

Comments
 (0)