-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudflare-worker.js
63 lines (58 loc) · 2.12 KB
/
cloudflare-worker.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
var consentJs = "export function renderCookieChoice() {\n const element = document.createElement(\"form\");\n const banner = `\n <div class=\"cookie-choice-description\">\n <p class=\"cookie-choice-p\">\n To help personalize content, tailor and measure ads, and provide a safer experience, we use cookies. By clicking or navigating the site, you agree to allow our collection of information through cookies. To learn more, read our <a href=\"PRIVACY_POLICY_LINK\" target=\"_blank\" rel=\"noopener\">Privacy Policy</a>.\n </p>\n </div>\n <div class=\"cookie-choice-action\">\n <button class=\"cookie-choice-button\" class=\"cookie-choice-accept\" type=\"submit\">Accept</button>\n <div>\n `;\n element.id = \"cookie-choice\";\n element.innerHTML = banner;\n element.onsubmit = function(e) {\n e.preventDefault();\n const event = new CustomEvent(\"cookieChoice:accept\", {\n bubbles: true\n });\n element.dispatchEvent(event);\n element.remove();\n };\n document.body.prepend(element);\n return element;\n}\n\nexport default renderCookieChoice;\n";
var consentNoopJs = "export function renderCookieChoice() {\n return;\n}\n\nexport default renderCookieChoice;\n";
const COOKIE_CONSENT_COUNTRIES = [
"AT",
"BE",
"BG",
"CY",
"CZ",
"DE",
"DK",
"DK",
"EE",
"EL",
"ES",
"EU",
"FI",
"FR",
"FR",
"GB",
"GR",
"HR",
"HU",
"IE",
"IT",
"IT",
"LT",
"LU",
"LV",
"MT",
"NL",
"PL",
"PT",
"SE",
"SK",
"UK"
];
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const { searchParams } = new URL(request.url);
const country = request.headers.get("cf-ipcountry");
const script =
COOKIE_CONSENT_COUNTRIES.includes(country) ||
searchParams.get("force") === "true"
? consentJs.replace(
"PRIVACY_POLICY_LINK",
searchParams.get("privacy-policy-link")
)
: consentNoopJs;
return new Response(script, {
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "text/javascript"
}
});
}