From 908dee56461ce740886349b29109fd3e76e8c949 Mon Sep 17 00:00:00 2001 From: kaffem <29717789+kaffem@users.noreply.github.com> Date: Mon, 8 Feb 2021 05:58:28 +0100 Subject: [PATCH] #161 PoC for the autoscroll feature --- src/html/settings.html | 2 + src/javascript/app.ts | 2 + src/javascript/configuration/configuration.ts | 1 + .../enhancements/watch2getherAutoscroll.ts | 43 +++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 src/javascript/enhancements/watch2getherAutoscroll.ts diff --git a/src/html/settings.html b/src/html/settings.html index ff1ea70..bd7f0b0 100644 --- a/src/html/settings.html +++ b/src/html/settings.html @@ -47,6 +47,8 @@

Watch2gether


+ +


diff --git a/src/javascript/app.ts b/src/javascript/app.ts index 0e714aa..6b5f329 100644 --- a/src/javascript/app.ts +++ b/src/javascript/app.ts @@ -11,6 +11,7 @@ import { init as notifications } from './enhancements/notifications'; import { init as quickSearch } from './enhancements/quickSearch'; import { init as timeConversion } from './enhancements/timeConversion'; import { init as watch2getherChat } from './enhancements/watch2getherChat'; +import { init as watch2getherAutoscroll } from './enhancements/watch2getherAutoscroll'; // css import { init as cssEnhancements } from './enhancements/cssEnhancements'; @@ -29,6 +30,7 @@ notifications(); quickSearch(); timeConversion(); watch2getherChat(); +watch2getherAutoscroll(); // css cssEnhancements(); \ No newline at end of file diff --git a/src/javascript/configuration/configuration.ts b/src/javascript/configuration/configuration.ts index 64207c6..a589ba1 100644 --- a/src/javascript/configuration/configuration.ts +++ b/src/javascript/configuration/configuration.ts @@ -16,6 +16,7 @@ export const SETTINGS_requestBeautifyPage = 'requestBeautifyPage'; export const SETTINGS_playerAutoplayAfterScreenshot = 'playerAutoplayAfterScreenshot'; // w2g export const SETTINGS_w2gDisplayCharacterCounter = 'w2gDisplayCharacterCounter'; +export const SETTINGS_w2gAutoscrollToUnseen = 'w2gAutoscrollToUnseen'; class Configuration { settingsCache: Map; diff --git a/src/javascript/enhancements/watch2getherAutoscroll.ts b/src/javascript/enhancements/watch2getherAutoscroll.ts new file mode 100644 index 0000000..e0e44f6 --- /dev/null +++ b/src/javascript/enhancements/watch2getherAutoscroll.ts @@ -0,0 +1,43 @@ +import { getGlobalConfiguration, SETTINGS_w2gAutoscrollToUnseen } from '../configuration/configuration'; +import * as core from '../utils/aniwatchCore'; +import { assigned } from '../utils/helpers'; + +export function init(): void { + getGlobalConfiguration().getProperty(SETTINGS_w2gAutoscrollToUnseen, value => { + if (value) { + core.runAfterLocationChange(() => { + let element = findSearchResults(); + if (assigned(element)) { + observeSearchResults(element); + } + }, "^/watch2gether/.*$"); + } + }); +} + +function observeSearchResults(searchRes: Element): void { + let observer = new MutationObserver(mutations => { + mutations.forEach(mutation => { + let num = searchRes.getElementsByClassName('md-2-line border _md-button-wrap _md md-clickable animelist-completed').length; + scrollTo(searchRes, num); + }); + }); + + observer.observe(searchRes, { + childList: true, + subtree: true, + }); +} + +function scrollTo(el: Element, pos: number): void { + let offset: number = 24; + el.scrollTo(0, pos * 72 + offset) +} + +function findSearchResults(): Element { + let searchResults = document.getElementsByClassName('search-results'); + if (searchResults[0] instanceof Element) { + return searchResults[0]; + } + return undefined; +}