#161 Only observe the anime list to prevent bugs in subtrees.

Subtree isn´t required to be observed any more, too.
This commit is contained in:
Serraniel 2021-02-13 19:22:50 +01:00
parent 6c0a197c83
commit fea7d68963
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3

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;
} }