JAVASCRIPT   88

frontend.js

Guest on 26th July 2021 04:31:47 PM

  1. import Record from './models/record';
  2. import { getLocalStorage, setLocalStorage } from './localStorageAccess';
  3. import _ from './lodash.custom';
  4. import { makeJSONRequest } from './xhr';
  5.  
  6. if (!window.geoip_detect) {
  7.     console.error('Geoip-detect: the JS variable window.geoip_detect is missing - this is needed for the options')
  8. }
  9. const options = window.geoip_detect.options || {};
  10.  
  11. let ajaxPromise = null;
  12.  
  13. function get_info_raw() {
  14.     if (!ajaxPromise) {
  15.         // Do Ajax Request only once per page load
  16.         const url = options.ajaxurl + '?action=geoip_detect2_get_info_from_current_ip'
  17.  
  18.         ajaxPromise = makeJSONRequest(url);
  19.     }
  20.  
  21.     return ajaxPromise;
  22. }
  23.  
  24. async function get_info_cached() {
  25.     let response = false;
  26.  
  27.     // 1) Load Info from cookie cache, if possible
  28.     if (options.cookie_name) {
  29.         response = getLocalStorage(options.cookie_name)
  30.         if (response && response.extra) {
  31.             // This might be an error object - cache it anyway
  32.             return response;
  33.         }
  34.     }
  35.  
  36.     // 2) Get response
  37.     try {
  38.         response = await get_info_raw();
  39.     } catch(err) {
  40.         response = err.responseJSON || err;
  41.     }
  42.  
  43.     // 3) Save info to cookie cache
  44.     if (options.cookie_name) {
  45.         setLocalStorage(options.cookie_name, response, options.cookie_duration_in_days * 24 * 60 * 60)
  46.     }
  47.  
  48.     return response;
  49. }
  50.  
  51.  
  52. export async function get_info() {
  53.     let response = await get_info_cached();
  54.  
  55.     if (typeof(response) !== 'object') {
  56.         console.error('Geoip-detect: Record should be an object, not a ' + typeof(response), response);
  57.         response = { 'extra': { 'error': response || 'Network error, look at the original server response ...' }};
  58.     }
  59.  
  60.     const record = new Record(response, options.default_locales);
  61.     return record;
  62. }
  63.  
  64. async function add_body_classes() {
  65.     const record = await get_info();
  66.  
  67.     if (record.error()) {
  68.         console.error('Geodata Error (could not add CSS-classes to body): ' + record.error());
  69.     }
  70.  
  71.     const css_classes = {
  72.         country:   record.get('country.iso_code'),
  73.         'country-is-in-european-union': record.get('country.is_in_european_union'),
  74.         continent: record.get('continent.code'),
  75.         province:  record.get('most_specific_subdivision.iso_code'),
  76.     };
  77.  
  78.     const body = document.getElementsByTagName('body')[0];
  79.     for(let key of Object.keys(css_classes)) {
  80.         const value = css_classes[key];
  81.         if (value) {
  82.             if (typeof(value) == 'string') {
  83.                 body.classList.add(`geoip-${key}-${value}`);
  84.             } else {
  85.                 body.classList.add(`geoip-${key}`);
  86.             }
  87.         }
  88.     }
  89. }
  90. if (options.do_body_classes) {
  91.     add_body_classes();
  92. }
  93.  
  94. // Extend window object
  95. window.geoip_detect.get_info = get_info;

Raw Paste


Login or Register to edit or fork this paste. It's free.