-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathloaders.js
75 lines (63 loc) · 1.56 KB
/
loaders.js
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
// Function to create and insert a script tag
function createScriptTag(options) {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
if (options.src) {
script.src = options.src;
script.onnload = resolve;
script.onerror = reject;
} else {
resolve();
}
if (options.inline) {
script.textContent = options.inline;
}
if (options.async) {
script.async = true;
}
if (options.defer) {
script.defer = true;
}
if (options.crossOrigin) {
script.crossOrigin = options.crossOrigin;
}
if (options.attributes) {
for (const [key, value] of Object.entries(options.attributes)) {
script.setAttribute(key, value);
}
}
document.head.appendChild(script);
if (!options.src) {
// If it's an inline script, resolve immediately after appending
resolve();
}
});
}
// Function to create and insert ad display div
function createAdDisplayDiv(
id,
className = null,
container = null,
style = null,
) {
const div = document.createElement("div");
div.id = id;
if (className != null) {
div.className = className;
}
if (style != null) {
div.setAttribute("style", style);
}
const script = document.createElement("script");
script.textContent = `
googletag.cmd.push(function () {
googletag.display('${id}');
});
`;
div.appendChild(script);
if (container == null) {
document.body.appendChild(div);
} else {
document.querySelector(container).appendChild(div);
}
}