Advertisement
swte

Automatikus süti elfogadás

May 22nd, 2024
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. addEventListener('fetch', event => {
  2.       event.respondWith(handleRequest(event.request))
  3. })
  4.  
  5. async function handleRequest(request) {
  6.       // Ország lekérdezése
  7.       const country = request.cf.country;
  8.      
  9.       // Az EU országai
  10.       const EU_Countries = ['AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK'];
  11.      
  12.       // Az eredeti válasz
  13.       let response = await fetch(request);
  14.      
  15.       // Ellenőrizzük, hogy az ország kódja szerepel-e az EU országok listáján
  16.       if (!EU_Countries.includes(country)) {
  17.             // Süti beállítása, ha a látogató nem EU-ból érkezik
  18.             const cookieValue = JSON.stringify({
  19.                   "consents": {
  20.                         "essential": ["borlabs-cookie", "first-promoter", "c-paddle"],
  21.                         "external-media": ["facebook", "maps", "instagram", "open-street-map", "soundcloud", "vimeo", "x-alias-twitter", "youtube"]
  22.                   }
  23.             });
  24.            
  25.             // A süti érvényességi idejének kiszámítása (1 hónap)
  26.             const expirationDate = new Date();
  27.             expirationDate.setMonth(expirationDate.getMonth() + 1);
  28.             const cookieHeader = `borlabs-cookie=${encodeURIComponent(cookieValue)}; Path=/; Expires=${expirationDate.toGMTString()}; SameSite=Lax`;
  29.            
  30.             // Új válasz létrehozása sütivel
  31.             response = new Response(response.body, response);
  32.             response.headers.append('Set-Cookie', cookieHeader);
  33.       }
  34.      
  35.       return response;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement