excmds.ts: Fix scrollpx not working on some websites

This commit is contained in:
glacambre 2018-02-27 22:30:58 +01:00
parent e122caa326
commit ec6226b1e4
No known key found for this signature in database
GPG key ID: B9625DB1767553AC

View file

@ -203,7 +203,10 @@ export function unfocus() {
//#content //#content
export function scrollpx(a: number, b: number) { export function scrollpx(a: number, b: number) {
window.scrollBy(a, b) let top = document.body.getClientRects()[0].top;
window.scrollBy(a, b);
if (top == document.body.getClientRects()[0].top)
recursiveScroll(a, b, [document.body])
} }
/** If two numbers are given, treat as x and y values to give to window.scrollTo /** If two numbers are given, treat as x and y values to give to window.scrollTo
@ -227,13 +230,32 @@ export function scrollto(a: number, b: number | "x" | "y" = "y") {
} }
} }
//#content_helper
function recursiveScroll(x: number, y: number, nodes: Element[]) {
let rect = null;
let node = null;
do {
node = nodes.splice(0, 1)[0]
if (!node)
return
rect = node.getClientRects()[0]
} while (!rect)
let top = rect.top
node.scrollBy(x, y);
if (top == node.getClientRects()[0].top) {
let children = Array.prototype.slice.call(node.children).filter(DOM.isVisible)
recursiveScroll(x, y, nodes.concat(children))
}
}
//#content //#content
export function scrollline(n = 1) { export function scrollline(n = 1) {
window.scrollByLines(n) window.scrollByLines(n)
} }
//#content //#content
export function scrollpage(n = 1) { export function scrollpage(n = 1) {
window.scrollBy(0, window.innerHeight * n) scrollpx(0, window.innerHeight * n)
} }
/** @hidden */ /** @hidden */