-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanner.html
117 lines (102 loc) · 2.94 KB
/
banner.html
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
<!-- Banner JavaScript by SSB April 2024 -->
<script type="text/javascript" charset="UTF-8" nonce="">
var config;
function getCookie(name) {
const value = " " + document.cookie;
console.log("value", `==${value}==`);
const parts = value.split(" " + name + "=");
return parts.length < 2 ? undefined : parts.pop().split(";").shift();
}
function setCookie(name, value, expiryDays, domain, path, secure) {
const exdate = new Date();
exdate.setHours(
exdate.getHours() +
(typeof expiryDays !== "number" ? 365 : expiryDays) * 24
);
document.cookie =
name +
"=" +
value +
";expires=" +
exdate.toUTCString() +
";path=" +
(path || "/") +
(domain ? ";domain=" + domain : "") +
(secure ? ";secure" : "");
}
function read_config() {
fetch('./banner.json')
.then(response => {
if (!response.ok) {
throw new Error("HTTP error " + response.status);
}
return response.json();
})
.then(json => {
this.config = json;
console.log(this.config);
setup_banner();
})
.catch(function () {
this.dataError = true;
})
}
//Define type colours here
function bannerColour(type) {
var colour;
switch (type) {
case "alert":
colour = "#f99";
break;
case "warning":
colour = "#ff9";
break;
case "information":
colour = "#9f9";
break;
default:
colour = "#77f";
break;
}
return colour;
}
function setup_banner() {
const $cookiesBanner = $(".mood-banner");
const cookieName = "moodBanner";
const hasCookie = getCookie(cookieName);
var ban;
var $cookiesBannerButton;
for (i = 0; i < config.length; i++) {
if (!ban &&
new Date().getTime() > new Date(config[i].start).getTime() &&
new Date().getTime() < new Date(config[i].end).getTime()) {
console.info("time is now for " + config[i].text);
ban = config[i];
}
}
if (!hasCookie && ban) {
console.info("We need to show a banner, and a cookie was not found");
$cookiesBanner.html("<b>" + ban.title + "</b> " + ban.text);
$cookiesBanner.css("background-color", bannerColour(ban.type));
$cookiesBanner.append("<button>" + ban.button + "</button>");
$cookiesBannerButton = $(".mood-banner button");
$cookiesBanner.removeClass("hidden");
}
if ($cookiesBannerButton)
$cookiesBannerButton.click(() => {
setCookie(cookieName, "closed");
$cookiesBanner.remove();
});
}
function add_banner() {
if ($("body div.mood-banner").length == 0) {
$("body").append("<div class=\"mood-banner hidden\"> By clicking \"OK\", you agree to the storing of cookies on your device to " +
"enhance site navigation, analyze site usage, and improve marketing.<button>Accept</button></div>");
}
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function () {
console.log("ready!");
add_banner();
read_config();
});
</script>