Completed
Push — develop ( 7839ac...7da9dd )
by Daniel
02:43 queued 11s
created

src/javascript/enhancements/anilyr.js   A

Complexity

Total Complexity 11
Complexity/F 1.57

Size

Lines of Code 48
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 28
mnd 4
bc 4
fnc 7
dl 0
loc 48
rs 10
bpm 0.5714
cpm 1.5713
noi 1
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A anilyr.js ➔ init 0 7 3
A anilyr.js ➔ findPlayer 0 10 2
A anilyr.js ➔ observeScreenshotTooltip 0 19 5
A anilyr.js ➔ resumePlayer 0 3 1
1
import * as core from '../utils/aniwatchCore';
2
import * as helper from '../utils/helpers';
3
4
const SCREENSHOT_TOOLTIP_ID = 'anilyr-screenshots-tooltip';
5
const PLAYER_ID = 'player';
6
7
export function init() {
8
    core.registerScript(node => {
9
        if (helper.isHtmlElement(node) && node.id === SCREENSHOT_TOOLTIP_ID) {
10
            observeScreenshotTooltip(node);
11
        }
12
    }, "^/anime/[0-9]*/[0-9]*$");
13
}
14
15
function observeScreenshotTooltip(tooltip) {
16
    let observer = new MutationObserver(mutations => {
0 ignored issues
show
Bug introduced by
The variable MutationObserver seems to be never declared. If this is a global, consider adding a /** global: MutationObserver */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
17
        mutations.forEach(mutation => {
18
            // Switched to invisible
19
            if (!mutation.oldValue.includes('display: none') && mutation.target.style.display == 'none') {
20
                let player = findPlayer();
21
                if(typeof player !== 'undefined'){
22
                    resumePlayer(player);
23
                }
24
            }
25
        });
26
    });
27
28
    observer.observe(tooltip, {
29
        attributes: true,
30
        attributeOldValue: true,
31
        attributeFilter: ['style'],
32
    });
33
}
34
35
function findPlayer() {
36
    const PLAYER_TAG_NAME = 'VIDEO'; // tagName gives UpperCase
37
38
    let playerCandidate = document.getElementById(PLAYER_ID);
39
    if (playerCandidate.tagName === PLAYER_TAG_NAME) {
40
        return playerCandidate;
41
    }
42
43
    return undefined;
44
}
45
46
function resumePlayer(player) {
47
    player.play();
48
}