Skip to content

Instantly share code, notes, and snippets.

@kmike
Last active September 21, 2022 09:35
Show Gist options
  • Save kmike/fe13f5e3bafb5a44eee7 to your computer and use it in GitHub Desktop.
Save kmike/fe13f5e3bafb5a44eee7 to your computer and use it in GitHub Desktop.
function scroll_to(splash, x, y)
local js = string.format(
"window.scrollTo(%s, %s);",
tonumber(x),
tonumber(y)
)
return splash:runjs(js)
end
function get_doc_height(splash)
return splash:runjs([[
Math.max(
Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
Math.max(document.body.clientHeight, document.documentElement.clientHeight)
)
]])
end
function scroll_to_bottom(splash)
local y = get_doc_height(splash)
return scroll_to(splash, 0, y)
end
function main(splash)
-- e.g. http://infiniteajaxscroll.com/examples/basic/page1.html
local url = splash.args.url
assert(splash:go(url))
assert(splash:wait(0.5))
splash:stop()
for i=1,10 do
scroll_to_bottom(splash)
assert(splash:wait(0.2))
end
splash:set_viewport("full")
return {
html = splash:html(),
png = splash:png{width=640},
har = splash:har(),
}
end
@danmash
Copy link

danmash commented Oct 14, 2016

Perfect script! But how can I use "while" instead of "for"? I don't know amount of required iterations. if scrollHeight increased I need to do scrolling again. My code doesn't work.

    local scroll_screen = splash:jsfunc([[
        function () {
            window.scrollTo(0,document.body.scrollHeight);
            return document.body.scrollHeight
        }
    ]])
    local last_scroll_height = 0
    local scroll_height = scroll_screen()
    while last_scroll_height < scroll_height do
        last_scroll_height = scroll_height
        scroll_height = scroll_screen()
        splash:wait(0.2)
    end

@shangliuyan
Copy link

excuse me, what's splash:stop() means

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment