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

NikkelM / Random-YouTube-Video / 9858140237

09 Jul 2024 01:37PM UTC coverage: 93.529% (-0.1%) from 93.66%
9858140237

push

github

web-flow
Disabled news check (#314)

302 of 354 branches covered (85.31%)

1431 of 1530 relevant lines covered (93.53%)

490.99 hits per line

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

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

1✔
3
/* c8 ignore start - The console re-routings cannot be tested correctly */
1✔
4
// ---------- Console re-routing ----------
1✔
5
const oldLog = console.log;
1✔
6
console.log = function () {
1✔
7
        // The last argument to console.log is a boolean that determines if the message should be shown in production
1✔
8
        const showInProduction = (arguments.length > 1 && arguments[arguments.length - 1] === true);
1✔
9
        // If we are either in development or the message should be shown in production, log the message
1✔
10
        if (process.env.NODE_ENV !== 'production' || showInProduction) {
1✔
11
                if (arguments[0] !== "[random-youtube-video]:") {
1✔
12
                        Array.prototype.unshift.call(arguments, '[random-youtube-video]:');
1✔
13
                }
1✔
14
                // Remove the showInProduction flag from the arguments so it doesn't get logged
1✔
15
                // Only remove the last argument if there are more than 2 arguments (prefix, normal argument, showInProduction flag)
1✔
16
                if (arguments.length > 2 && typeof arguments[arguments.length - 1] === 'boolean') {
1✔
17
                        Array.prototype.pop.call(arguments);
1✔
18
                }
1✔
19
                oldLog.apply(this, arguments);
1✔
20
        }
1✔
21
}
1✔
22
/* c8 ignore stop */
1✔
23

1✔
24
// ---------- Utility functions ----------
1✔
25

1✔
26
// ----- URLs -----
1✔
27
export function isVideoUrl(url) {
1✔
28
        if (!url) return false;
12✔
29

9✔
30
        const urlParts = url.split("/");
9✔
31
        if (!urlParts[2]?.includes("youtube")) return false;
12✔
32
        return urlParts[3]?.startsWith("watch?v=") ?? false;
12✔
33
}
12✔
34

1✔
35
export function getPageTypeFromURL(url) {
1✔
36
        if (!url) return null;
635✔
37

631✔
38
        const urlParts = url.split("/");
631✔
39
        if (!urlParts[2]?.includes("youtube")) return null;
635✔
40
        if (urlParts[3]?.startsWith("watch?v=")) {
635✔
41
                return "video";
622✔
42
        } else if (urlParts[3]?.startsWith("shorts")) {
635✔
43
                return "short";
1✔
44
        } else {
5✔
45
                return "channel";
4✔
46
        }
4✔
47
}
635✔
48

1✔
49
// ----- DOM -----
1✔
50
export function setDOMTextWithDelay(textElement, newText, delayMS, predicate = () => { return true; }) {
1✔
51
        // Sets the innerHTML of a (text) DOM element after a delay, if a predicate evaluates to true
3✔
52
        // If no predicate is passed, this function will always set the text after the delay
3✔
53
        delay(delayMS).then(() => {
3✔
54
                if (predicate()) {
3✔
55
                        textElement.innerText = newText;
2✔
56
                }
2✔
57
        });
3✔
58
}
3✔
59

1✔
60
export function updateSmallButtonStyleForText(textElement, isTextStyle) {
1✔
61
        textElement.style.fontSize = isTextStyle ? "12px" : "";
457!
62
        textElement.style.position = isTextStyle ? "absolute" : "";
457!
63
        textElement.style.top = isTextStyle ? "50%" : "";
457!
64
        textElement.style.left = isTextStyle ? "50%" : "";
457!
65
        textElement.style.transform = isTextStyle ? "translate(-50%, -50%)" : "";
457!
66
        textElement.style.alignItems = isTextStyle ? "center" : "";
457!
67
        textElement.style.justifyContent = isTextStyle ? "center" : "";
457!
68
        textElement.style.display = isTextStyle ? "flex" : "";
457!
69

457✔
70
        if (isTextStyle) {
457✔
71
                textElement.classList.remove("material-symbols-outlined");
457✔
72
        } else {
457!
73
                textElement.classList.add("material-symbols-outlined");
×
74
        }
×
75
}
457✔
76

1✔
77
// ----- Small utilities -----
1✔
78
// Waits for a certain amount of milliseconds
1✔
79
export function delay(ms) {
1✔
80
        return new Promise(resolve => setTimeout(resolve, ms));
4✔
81
}
4✔
82

1✔
83
// Determines if an object is empty
1✔
84
export function isEmpty(obj) {
1✔
85
        return Object.keys(obj).length === 0;
1,689✔
86
}
1,689✔
87

1✔
88
// Gets the length of an object
1✔
89
export function getLength(obj) {
1✔
90
        return Object.keys(obj ?? {}).length;
1,354✔
91
}
1,354✔
92

1✔
93
// Adds a number of hours to a date
1✔
94
export function addHours(date, hours) {
1✔
95
        return new Date(date.getTime() + hours * 3600000);
1,398✔
96
}
1,398✔
97

1✔
98
// ----- Errors -----
1✔
99
export class RandomYoutubeVideoError extends Error {
1✔
100
        constructor({ code = "RYV-0", message = "", solveHint = "", showTrace = true, canSavePlaylist = false }) {
1✔
101
                super(message);
282✔
102
                this.code = code;
282✔
103
                this.message = message;
282✔
104
                this.solveHint = solveHint;
282✔
105
                this.showTrace = showTrace;
282✔
106
                // This should be set to true for 'non-fatal' errors, where the playlist is in a safe state
282✔
107
                this.canSavePlaylist = canSavePlaylist;
282✔
108
                this.name = "RandomYoutubeVideoError";
282✔
109
        }
282✔
110
}
1✔
111

1✔
112
export class YoutubeAPIError extends RandomYoutubeVideoError {
1✔
113
        constructor(code = "YAPI-0", message = "", reason = "", solveHint = "", showTrace = true, canSavePlaylist = false) {
1✔
114
                super(message);
9✔
115
                this.code = code;
9✔
116
                this.message = message;
9✔
117
                this.reason = reason;
9✔
118
                this.solveHint = solveHint;
9✔
119
                this.showTrace = showTrace;
9✔
120
                this.canSavePlaylist = canSavePlaylist;
9✔
121
                this.name = "YoutubeAPIError";
9✔
122
        }
9✔
123
}
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