Feature/#161 autoscroll w2g #176

Merged
Serraniel merged 7 commits from feature/#161-autoscroll-w2g into develop 2021-02-13 21:42:24 +01:00
Showing only changes of commit fea7d68963 - Show all commits

View file

@ -7,6 +7,7 @@ export function init(): void {
if (value) { if (value) {
core.runAfterLocationChange(() => { core.runAfterLocationChange(() => {
let element = findSearchResults(); let element = findSearchResults();
console.log(element);
if (assigned(element)) { if (assigned(element)) {
observeSearchResults(element); observeSearchResults(element);
} }
@ -17,26 +18,26 @@ export function init(): void {
function observeSearchResults(searchRes: Element): void { function observeSearchResults(searchRes: Element): void {
let observer = new MutationObserver(mutations => { let observer = new MutationObserver(mutations => {
let scrollTarget = searchRes.querySelector('.ep-view md-list-item:not(.animelist-completed)') as HTMLElement; let scrollTarget = searchRes.querySelector('md-list-item:not(.animelist-completed):not(.animelist-completed-add)') as HTMLElement;
if (assigned(scrollTarget)) { if (assigned(scrollTarget)) {
// The node isn´t in its correct position directly when it´s added so we wait a small bit of time before we start scrolling.
// Also works for long loading lists which need more time to load than we wait (for example One Piece).
window.setTimeout(() => {
// scroll container to episode first // scroll container to episode first
searchRes.scrollTop = scrollTarget.offsetTop; searchRes.scrollTop = scrollTarget.offsetTop;
// then scroll page to episode if neccessarry // then scroll page to episode if neccessarry
scrollTarget.scrollIntoView({ behavior: "smooth", block: "nearest" }); scrollTarget.scrollIntoView({ behavior: "smooth", block: "nearest" });
}, 500);
} }
}); });
observer.observe(searchRes, { observer.observe(searchRes, {
childList: true, childList: true,
subtree: true,
}); });
} }
function findSearchResults(): Element { function findSearchResults(): Element {
let searchResults = document.getElementsByClassName('search-results'); return document.querySelector('.search-results .ep-view');
if (searchResults[0] instanceof Element) {
return searchResults[0];
}
return undefined;
} }