Passed
Push — develop ( d6666c...ebd0be )
by Daniel
02:51 queued 10s
created

anilyr.ts ➔ findPlayerElement   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
import { getGlobalConfiguration, SETTINGS_playerAutoplayAfterScreenshot } from '../configuration/configuration';
2
import * as core from '../utils/aniwatchCore';
3
import * as helper from '../utils/helpers';
4
5
const SCREENSHOT_TOOLTIP_ID = 'anilyr-screenshots-tooltip';
6
const PLAYER_ID = 'player';
7
8
export function init(): void {
9
    getGlobalConfiguration().getProperty(SETTINGS_playerAutoplayAfterScreenshot, value => {
10
        if (value) {
11
            core.registerScript((node: Node) => {
12
                let element = node as HTMLElement;
13
                if (helper.assigned(element) && element.id === SCREENSHOT_TOOLTIP_ID) {
14
                    observeScreenshotTooltip(element);
15
                }
16
            }, "^/anime/[0-9]*/[0-9]*$");
17
        }
18
    });
19
}
20
21
function observeScreenshotTooltip(tooltip: HTMLElement): void {
22
    let observer = new MutationObserver(mutations => {
23
        mutations.forEach(mutation => {
24
            // Switched to invisible
25
            if (!mutation.oldValue.includes('display: none') && helper.isHtmlElement(mutation.target) && (mutation.target as HTMLElement).style.display == 'none') {
26
                let playerElement = findPlayerElement();
27
                if (helper.assigned(playerElement)) {
28
                    resumePlayer(playerElement);
29
                }
30
            }
31
        });
32
    });
33
34
    observer.observe(tooltip, {
35
        attributes: true,
36
        attributeOldValue: true,
37
        attributeFilter: ['style'],
38
    });
39
}
40
41
function findPlayerElement(): HTMLVideoElement {
42
    let playerCandidate = document.getElementById(PLAYER_ID);
43
    if (playerCandidate instanceof HTMLVideoElement) {
44
        return playerCandidate;
45
    }
46
47
    return undefined;
48
}
49
50
function resumePlayer(player: HTMLVideoElement) {
51
    player.play();
52
}