src/javascript/enhancements/anilyr.ts   A
last analyzed

Complexity

Total Complexity 16
Complexity/F 2.67

Size

Lines of Code 88
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 16
mnd 10
bc 10
fnc 6
bpm 1.6666
cpm 2.6666
noi 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A anilyr.ts ➔ init 0 23 4
A anilyr.ts ➔ observeScreenshotTooltip 0 18 3
A anilyr.ts ➔ findPlayerElement 0 8 2
A anilyr.ts ➔ observeTabFocus 0 13 5
A anilyr.ts ➔ resumePlayer 0 3 1
A anilyr.ts ➔ pausePlayer 0 3 1
1
import { getGlobalConfiguration,
2
    SETTINGS_playerAutoplayAfterScreenshot,
3
    SETTINGS_playerAutopauseAfterFocusLost,
4
    SETTINGS_playerAutoplayAfterFocusGain } from '../configuration/configuration';
5
import * as core from '../utils/aniwatchCore';
6
import * as helper from '../utils/helpers';
7
8
const SCREENSHOT_TOOLTIP_ID = 'anilyr-screenshots-tooltip';
9
const PLAYER_ID = 'player';
10
let resumePlayerOnVisible: boolean;
11
12
export function init(): void {
13
    getGlobalConfiguration().getProperty(SETTINGS_playerAutoplayAfterScreenshot, value => {
14
        if (value) {
15
            core.registerScript((node: Node) => {
16
                let element = node as HTMLElement;
17
                if (helper.assigned(element) && element.id === SCREENSHOT_TOOLTIP_ID) {
18
                    observeScreenshotTooltip(element);
19
                }
20
            }, "^/anime/[0-9]*/[0-9]*$");
21
        }
22
    });
23
24
    getGlobalConfiguration().getProperty(SETTINGS_playerAutopauseAfterFocusLost, value => {
25
        if (value) {
26
            core.registerScript((node: Node) => {
27
                window.addEventListener('visibilitychange', observeTabFocus, false);
28
            }, "^/anime/[0-9]*/[0-9]*$");
29
        }
30
    });
31
32
    getGlobalConfiguration().getProperty(SETTINGS_playerAutoplayAfterFocusGain, value => {
33
        resumePlayerOnVisible = value;
34
    });
35
}
36
37
function observeScreenshotTooltip(tooltip: HTMLElement): void {
38
    let observer = new MutationObserver(mutations => {
39
        mutations.forEach(mutation => {
40
            // Switched to invisible
41
            if (!mutation.oldValue.includes('display: none') && helper.isHtmlElement(mutation.target) && (mutation.target as HTMLElement).style.display == 'none') {
42
                let playerElement = findPlayerElement(PLAYER_ID);
43
                if (helper.assigned(playerElement)) {
44
                    resumePlayer(playerElement);
45
                }
46
            }
47
        });
48
    });
49
50
    observer.observe(tooltip, {
51
        attributes: true,
52
        attributeOldValue: true,
53
        attributeFilter: ['style'],
54
    });
55
}
56
57
58
function observeTabFocus(): void {
59
    let docState = document.visibilityState;
60
    let playerElement = findPlayerElement(PLAYER_ID);
61
    if (docState === 'hidden') {
62
        if (helper.assigned(playerElement)) {
63
            pausePlayer(playerElement);
64
        }
65
    }
66
    else if (docState === 'visible' && resumePlayerOnVisible) {
67
        if (helper.assigned(playerElement)) {
68
            resumePlayer(playerElement);
69
        }
70
    }
71
}
72
73
export function findPlayerElement(id: string): HTMLVideoElement {
74
    let playerCandidate = document.getElementById(id);
75
    if (playerCandidate instanceof HTMLVideoElement) {
76
        return playerCandidate;
77
    }
78
79
    return undefined;
80
}
81
82
function resumePlayer(player: HTMLVideoElement) {
83
    player.play();
84
}
85
86
function pausePlayer(player: HTMLVideoElement) {
87
    player.pause()
88
}