-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
140 lines (115 loc) · 3.36 KB
/
script.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
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
import { renderSimpleTemplate } from "./templates/weary-wolverine/template";
import { renderTemplateCard } from "./templates/lazy-ladybird/template";
function fetchAdverts(uniqueId, currentUrl) {
const apiEndpoint = "https://api.reallygreatads.com/v1/placement";
const payload = {
uniqueId: uniqueId,
url: currentUrl,
};
return fetch(apiEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}).then((response) => {
if (response.ok) {
return response.json();
}
throw new Error("Failed to fetch adverts");
});
}
function renderLoadingSpinner(containerId) {
const container = document.getElementById(containerId);
if (!container) return;
const style = document.createElement("style");
style.textContent = `
.rga_loading-spinner {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2rem;
}
.rga_loading-spinner .rga_spinner {
width: 40px;
height: 40px;
border: 4px solid rgba(0, 123, 255, 0.3);
border-top-color: #007bff;
border-radius: 50%;
animation: rga_spin 1s linear infinite;
}
.rga_loading-spinner p {
margin-top: 1rem;
font-size: 1rem;
color: #333;
}
@keyframes rga_spin {
to {
transform: rotate(360deg);
}
}
`;
document.head.appendChild(style);
container.innerHTML = `
<div class="rga_loading-spinner">
<div class="rga_spinner"></div>
<p>Loading...</p>
</div>
`;
}
function removeLoadingSpinner(containerId) {
const container = document.getElementById(containerId);
if (!container) return;
const spinner = container.querySelector(".loading-spinner");
if (spinner) {
container.removeChild(spinner);
}
}
window.initAdverts = function (uniqueId, containerId, isDevMode = false) {
const currentUrl = window.location.href;
// Display loading indicator...
renderLoadingSpinner(containerId);
fetchAdverts(uniqueId, currentUrl)
.then((data) => {
if (data.template) {
let adHtml;
let src = `https://cdn.reallygreatads.com/${data.template}.min.css`;
// If we're in development node, then we can use the local template
if (isDevMode) src = `/templates/${data.template}/styles.css`;
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = src;
document.head.appendChild(link);
// Load the html now
const container = document.getElementById(containerId);
switch (data.template) {
case "lazy-ladybird":
adHtml = renderTemplateCard({
adverts: data.adverts,
});
break;
case "weary-wolverine":
adHtml = renderSimpleTemplate({
adverts: data.adverts,
});
break;
}
if (container) {
container.innerHTML = adHtml;
} else {
if (!adHtml) {
console.error("Unable to find layout for this advert");
} else {
console.error("Container not found");
}
}
} else {
console.log("No template was found");
}
removeLoadingSpinner();
})
.catch((error) => {
console.error("Error fetching adverts:", error);
});
};