restructured inject.js
This commit is contained in:
100
client/src/modules/utils.ts
Normal file
100
client/src/modules/utils.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
export default class Utils {
|
||||
|
||||
static waitForElement(selector: any, callback: Function, type: string = "selector", checkFrequencyInMs: number = 1000, timeoutInMs: number = 10000) {
|
||||
let startTimeInMs: number = Date.now();
|
||||
(function loopSearch(): void {
|
||||
let value: boolean;
|
||||
console.log(selector);
|
||||
|
||||
if (type === "selector")
|
||||
value = Utils.querySelector(selector);
|
||||
else if (type === "function")
|
||||
value = Utils.isFunction(selector);
|
||||
else
|
||||
value = false;
|
||||
|
||||
if (value) {
|
||||
console.log("defined");
|
||||
callback();
|
||||
}
|
||||
else {
|
||||
setTimeout(function (): void {
|
||||
console.log("repeating");
|
||||
if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs)
|
||||
return;
|
||||
loopSearch();
|
||||
}, checkFrequencyInMs);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
static waitForSeatmap(callback: Function, checkFrequencyInMs: number = 100, timeoutInMs: number = 10000) {
|
||||
const seatCharts: any = (<any>window).jQuery("#containerSeatmap").seatCharts;
|
||||
let startTimeInMs: number = Date.now();
|
||||
|
||||
if (Utils.isFunction(seatCharts)) {
|
||||
console.log("defined");
|
||||
callback();
|
||||
}
|
||||
else {
|
||||
setTimeout(function (): void {
|
||||
console.log("repeating");
|
||||
if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs)
|
||||
return;
|
||||
Utils.waitForSeatmap(callback);
|
||||
}, checkFrequencyInMs);
|
||||
}
|
||||
}
|
||||
|
||||
static querySelector(selector: string): boolean {
|
||||
if (document.querySelector(selector) != null)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
static isFunction(selector: string): boolean {
|
||||
if (typeof selector === "function")
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
static async delay(ms: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
static inject(content: string, type: string = "js", loc: string = "head") {
|
||||
var script: HTMLLinkElement | HTMLScriptElement;
|
||||
|
||||
if (type === "js") {
|
||||
script = document.createElement('script');
|
||||
script.type = 'text/javascript';
|
||||
script.src = content;
|
||||
}
|
||||
else if (type === "css") {
|
||||
script = document.createElement('link');
|
||||
script.type = 'text/css';
|
||||
script.rel = "stylesheet"
|
||||
script.href = content;
|
||||
}
|
||||
else if (type === "script") {
|
||||
script = document.createElement('script');
|
||||
script.type = 'text/javascript';
|
||||
script.innerHTML = content;
|
||||
}
|
||||
else
|
||||
return;
|
||||
|
||||
|
||||
if (loc === "body")
|
||||
document.body.appendChild(script);
|
||||
else if (loc === "head")
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
|
||||
static getDayName(date: Date, locale: string) {
|
||||
return date.toLocaleDateString(locale, { weekday: 'long' });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user