-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
99 lines (88 loc) · 2.86 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
//create a function that takes any url and returns the origin
export function GetOrigin(url: string): string {
if (url.startsWith('*')) {
return '*';
}
if (url.startsWith('/')) {
return '';
}
const a = document.createElement('a');
a.href = url;
return a.origin;
}
export function GetFromType(url: string): string {
const origin = GetOrigin(url);
const cleanedUrl = url.replace(origin, '');
const splittedUrl = cleanedUrl.split('/');
const toDestination = splittedUrl[1];
if (toDestination.startsWith('parent')) {
return 'parent';
} else if (toDestination.startsWith('iframe')) {
return 'iframe';
} else if (toDestination.startsWith('window')) {
return 'window';
}
return 'parent';
}
export function GetToType(url: string): string {
const origin = GetOrigin(url);
const cleanedUrl = url.replace(origin, '');
const splittedUrl = cleanedUrl.split('/');
const toDestination = splittedUrl[1];
if (toDestination.endsWith('parent')) {
return 'parent';
} else if (toDestination.endsWith('iframe')) {
return 'iframe';
} else if (toDestination.endsWith('window')) {
return 'window';
}
return 'iframe';
}
export function GetUUID(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
export function ValidatePath(url: string): boolean {
const origin = GetOrigin(url);
const cleanedUrl = url.replace(origin, '');
if (!cleanedUrl.startsWith('/')) {
return false;
}
const splittedUrl = cleanedUrl.split('/');
if (splittedUrl.length != 3) {
return false;
}
const toDestination = splittedUrl[1];
if (!toDestination.startsWith('parent') && !toDestination.startsWith('iframe') && !toDestination.startsWith('window')) {
return false;
}
if (toDestination.startsWith('parent')) {
if (!toDestination.endsWith('iframe') && !toDestination.endsWith('window')) {
return false;
}
}
if (toDestination.startsWith('iframe') || toDestination.startsWith('window')) {
if (!toDestination.endsWith('parent')) {
return false;
}
}
return true;
}
export function WaitForIframeLoad(iframe: HTMLIFrameElement, timeout: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
const timeoutId = setTimeout(() => {
clearInterval(interval);
reject(new Error('Timeout exceeded'));
}, timeout);
const interval = setInterval(() => {
if (iframe.contentWindow) {
clearInterval(interval);
clearTimeout(timeoutId);
resolve();
}
}, 100);
});
}