• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NikkelM / Random-YouTube-Video / 7090316771

04 Dec 2023 05:31PM UTC coverage: 93.91%. Remained the same
7090316771

Pull #254

github

web-flow
Merge e058d82f0 into 2e93157e5
Pull Request #254: Fixed version update not being handled correctly

291 of 338 branches covered (0.0%)

1357 of 1445 relevant lines covered (93.91%)

512.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.23
/src/utils.js
1
// Global utilities
1✔
2

1✔
3
/* c8 ignore start - The console reroutings cannot be tested correctly */
1✔
4
// ---------- Console rerouting ----------
1✔
5
var oldLog = console.log;
1✔
6
console.log = function () {
1✔
7
        if (arguments[0] !== "[random-youtube-video]:") {
1✔
8
                Array.prototype.unshift.call(arguments, '[random-youtube-video]:');
1✔
9
        }
1✔
10
        oldLog.apply(this, arguments);
1✔
11
}
1✔
12
/* c8 ignore stop */
1✔
13

1✔
14
// ---------- Utility functions ----------
1✔
15

1✔
16
// ----- URLs -----
1✔
17
export function isVideoUrl(url) {
1✔
18
        if (!url) return false;
12✔
19

9✔
20
        const urlParts = url.split("/");
9✔
21
        if (!urlParts[2]?.includes("youtube")) return false;
12✔
22
        return urlParts[3]?.startsWith("watch?v=") ?? false;
12✔
23
}
12✔
24

1✔
25
export function getPageTypeFromURL(url) {
1✔
26
        if (!url) return null;
637✔
27

633✔
28
        const urlParts = url.split("/");
633✔
29
        if (!urlParts[2]?.includes("youtube")) return null;
637✔
30
        if (urlParts[3]?.startsWith("watch?v=")) {
637✔
31
                return "video";
624✔
32
        } else if (urlParts[3]?.startsWith("shorts")) {
637✔
33
                return "short";
1✔
34
        } else {
5✔
35
                return "channel";
4✔
36
        }
4✔
37
}
637✔
38

1✔
39
// ----- DOM -----
1✔
40
export function setDOMTextWithDelay(textElement, newText, delayMS, predicate = () => { return true; }) {
1✔
41
        // Sets the innerHTML of a (text) DOM element after a delay, if a predicate evaluates to true
3✔
42
        // If no predicate is passed, this function will always set the text after the delay
3✔
43
        delay(delayMS).then(() => {
3✔
44
                if (predicate()) {
3✔
45
                        textElement.innerText = newText;
2✔
46
                }
2✔
47
        });
3✔
48
}
3✔
49

1✔
50
export function updateSmallButtonStyleForText(textElement, isTextStyle) {
1✔
51
        textElement.style.fontSize = isTextStyle ? "12px" : "";
463!
52
        textElement.style.position = isTextStyle ? "absolute" : "";
463!
53
        textElement.style.top = isTextStyle ? "50%" : "";
463!
54
        textElement.style.left = isTextStyle ? "50%" : "";
463!
55
        textElement.style.transform = isTextStyle ? "translate(-50%, -50%)" : "";
463!
56
        textElement.style.alignItems = isTextStyle ? "center" : "";
463!
57
        textElement.style.justifyContent = isTextStyle ? "center" : "";
463!
58
        textElement.style.display = isTextStyle ? "flex" : "";
463!
59

463✔
60
        if (isTextStyle) {
463✔
61
                textElement.classList.remove("material-symbols-outlined");
463✔
62
        } else {
463!
63
                textElement.classList.add("material-symbols-outlined");
×
64
        }
×
65
}
463✔
66

1✔
67
// ----- Small utilities -----
1✔
68
// Waits for a certain amount of milliseconds
1✔
69
export function delay(ms) {
1✔
70
        return new Promise(resolve => setTimeout(resolve, ms));
4✔
71
}
4✔
72

1✔
73
// Determines if an object is empty
1✔
74
export function isEmpty(obj) {
1✔
75
        return Object.keys(obj).length === 0;
1,693✔
76
}
1,693✔
77

1✔
78
// Gets the length of an object
1✔
79
export function getLength(obj) {
1✔
80
        return Object.keys(obj ?? {}).length;
1,357✔
81
}
1,357✔
82

1✔
83
// Adds a number of hours to a date
1✔
84
export function addHours(date, hours) {
1✔
85
        return new Date(date.getTime() + hours * 3600000);
1,401✔
86
}
1,401✔
87

1✔
88
// ----- Errors -----
1✔
89
export class RandomYoutubeVideoError extends Error {
1✔
90
        constructor({ code = "RYV-0", message = "", solveHint = "", showTrace = true, canSavePlaylist = false }) {
1✔
91
                super(message);
282✔
92
                this.code = code;
282✔
93
                this.message = message;
282✔
94
                this.solveHint = solveHint;
282✔
95
                this.showTrace = showTrace;
282✔
96
                // This should be set to true for 'non-fatal' errors, where the playlist is in a safe state
282✔
97
                this.canSavePlaylist = canSavePlaylist;
282✔
98
                this.name = "RandomYoutubeVideoError";
282✔
99
        }
282✔
100
}
1✔
101

1✔
102
export class YoutubeAPIError extends RandomYoutubeVideoError {
1✔
103
        constructor(code = "YAPI-0", message = "", reason = "", solveHint = "", showTrace = true, canSavePlaylist = false) {
1✔
104
                super(message);
9✔
105
                this.code = code;
9✔
106
                this.message = message;
9✔
107
                this.reason = reason;
9✔
108
                this.solveHint = solveHint;
9✔
109
                this.showTrace = showTrace;
9✔
110
                this.canSavePlaylist = canSavePlaylist;
9✔
111
                this.name = "YoutubeAPIError";
9✔
112
        }
9✔
113
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc