forked from SimonStnn/pop-a-loon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
195 lines (171 loc) · 5.5 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
import browser from 'webextension-polyfill';
import { Message, BalloonContainerId } from '@/const';
import log from '@/managers/log';
import storage from '@/managers/storage';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function secondsToMilliseconds(seconds: number) {
return seconds * 1000;
}
export function minutesToMilliseconds(minutes: number) {
return secondsToMilliseconds(minutes * 60);
}
export function random(max: number): number;
export function random(min: number, max: number): number;
export function random<T>(items: T[]): T;
export function random<T>(...args: T[]): T;
export function random<T>(
...args: [number] | [number, number] | T[] | [T[]]
): number | T {
// Handle `max: number`
if (args.length === 1 && typeof args[0] === 'number')
return Math.random() * args[0];
// Handle `items: T[]`
else if (args.length === 1 && Array.isArray(args[0]))
return args[0][Math.floor(random(args[0].length))];
// Handle `min: number, max: number`
else if (
args.length === 2 &&
typeof args[0] === 'number' &&
typeof args[1] === 'number'
) {
const [min, max] = args;
return Math.random() * (max - min) + min;
}
// Handle `...args: T[]`
const items = args as T[];
return items[Math.floor(random(items.length))];
}
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function sendMessage(message: Message) {
try {
const res = await browser.runtime.sendMessage(message);
if (browser.runtime.lastError) {
log.error(
'Error sending message:',
message,
'\nError message:',
browser.runtime.lastError
);
browser.runtime.lastError;
}
} catch (e) {}
}
export function getBrowser() {
const userAgent = navigator.userAgent.toLocaleLowerCase();
if (/firefox/i.test(userAgent)) {
return 'Firefox';
} else if (/chrome/i.test(userAgent)) {
return 'Chrome';
} else {
return 'Unknown';
}
}
export function isRunningInBackground() {
const views = browser.extension?.getViews?.() as Window[] | undefined;
const isRunningInPopup =
views?.some((view) => view.location.href.includes('popup.html')) ?? false;
return !isRunningInPopup;
}
type WeightedRandomOptions<T> = {
default?: T;
};
export function weightedRandom<T, D = null>(
results: T[],
weights: number[],
options: WeightedRandomOptions<D> = {}
): T | D {
// Calculate the total weight
const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
// Generate a random number between 0 and the total weight
const randomWeight = Math.random() * totalWeight;
// Find the index of the selected result based on the random weight
let weightSum = 0;
for (let i = 0; i < results.length; i++) {
weightSum += weights[i];
if (randomWeight < weightSum) {
return results[i];
}
}
// Return the default value if no result was selected or null if no default was provided
return (options.default ?? null) as D;
}
export async function calculateBalloonSpawnDelay() {
const config = await storage.sync.get('config');
// Generate a random delay between the min and max spawn interval
const randomDelay = random(
config.spawnInterval.min,
config.spawnInterval.max
);
const spawnRateMultiplier = Math.max(0, Math.min(config.spawnRate, 1));
return randomDelay / spawnRateMultiplier;
}
function createBalloonContainer() {
const balloonContainer = document.createElement('div');
balloonContainer.id = BalloonContainerId;
document.body.appendChild(balloonContainer);
return balloonContainer;
}
export function getBalloonContainer() {
return (
document.getElementById(BalloonContainerId) ?? createBalloonContainer()
);
}
export async function importStylesheet(id: string, href: string) {
if (!document.getElementById(id)) {
// Fetch the CSS file content
const response = await fetch(href);
const css = await response.text();
// Create a <style> element with the CSS content
let style = document.createElement('style');
style.id = id;
style.textContent = css;
// Append the <style> element to the <head>
getBalloonContainer().appendChild(style);
}
}
export async function askOriginPermissions() {
const host_permissions = await browser.runtime.getManifest().host_permissions;
if (!host_permissions) return log.error('No host_permissions found');
const permissions = await browser.permissions.request({
origins: host_permissions,
});
log.debug('Permissions granted for', permissions);
}
export function isFullScreenVideoPlaying() {
const fullscreenElement = document.fullscreenElement;
if (fullscreenElement) {
if (fullscreenElement.tagName.toLowerCase() === 'video') {
return true;
}
const videos = [
...fullscreenElement.getElementsByTagName('video'),
...document.getElementsByTagName('video'),
];
if (videos.length > 0) {
return true;
}
}
return false;
}
export async function isInSnooze(): Promise<boolean> {
const snooze = await storage.sync.get('snooze');
return snooze !== null && (snooze === -1 || Date.now() < snooze);
}
export function joinPaths(...paths: string[]): string {
return paths
.map((part, index) => {
if (index === 0) {
return part.trim().replace(/[/]*$/g, '');
} else {
return part.trim().replace(/(^[/]*|[/]*$)/g, '');
}
})
.filter((part) => part.length)
.join('/');
}