انتقل إلى المحتوى

مستخدم:Gerges/ads.js: الفرق بين النسختين

من ويكيبيديا، الموسوعة الحرة
تم حذف المحتوى تمت إضافة المحتوى
أنشأ الصفحة ب'function getCountryCode() { var geoIP = $.cookie('GeoIP'); if (!geoIP || !geoIP.includes(':')) { console.error('GeoIP cookie not found or invalid.'); return false; } var parts = geoIP.split(':'); var countryCode = parts[0]; if (countryCode.length === 2) { return countryCode; } else { console.error('Invalid GeoIP format: No valid country code found.'); return false; } } function checkCountry(countryCodes) { if (countryCodes =...'
وسوم: تحرير من المحمول تعديل ويب محمول تعديل المحمول المتقدم
(لا فرق)

نسخة 16:20، 2 أكتوبر 2024

function getCountryCode() {
  var geoIP = $.cookie('GeoIP');

  if (!geoIP || !geoIP.includes(':')) {
    console.error('GeoIP cookie not found or invalid.');
    return false;
  }

  var parts = geoIP.split(':');
  var countryCode = parts[0];

  if (countryCode.length === 2) {
    return countryCode;
  } else {
    console.error('Invalid GeoIP format: No valid country code found.');
    return false;
  }
}
function checkCountry(countryCodes) {
  if (countryCodes === null) {
    return false;
  }
  if (countryCodes === 'all') {
    return true;
  }
  
  let countryCodesArray = countryCodes.split(',').map(code => code.trim());
  
  let userCountryCode = getCountryCode();
  
  return countryCodesArray.includes(userCountryCode);
}

$.when(
  $.getJSON(mw.config.get('wgScript') + '?action=raw&ctype=application/json&maxage=5&title=MediaWiki:إعلانات/بيانات.json'),
  mw.loader.using(['mediawiki.util']),
  $.ready
).then(function(jsonData) {
  let ads = jsonData[0];
  let validAds = [];
  const datestring = (new Date).toISOString();
  const Adkey = 'arabic-ads-last';
  const Adcookie = 'arabicad-dismiss-';
  ads.forEach(ad => {
    if ((ad.start < datestring) && (ad.end > datestring && mw.cookie.get(Adcookie + ad.id) !== '1') && checkCountry(ad.countryCodes)) {
      validAds.push(ad);
    }
  });
  let cad;
  if (validAds.length == 1) {
    cad = validAds[0];
  } else if (validAds.length > 1) {
    let lastAd = Number(mw.cookie.get(Adkey)) || -1;

    if (lastAd > -1) {
      for (let i = 0; i < validAds.length; i++) {
        if (validAds[i].id > lastAd) {
          cad = validAds[i];
          break;
        }
      }
      if (cad === undefined) {
        cad = validAds[0];
      }
    } else {
      cad = validAds[Math.floor(Math.random() * validAds.length)];
    }
  }

  if (cad !== undefined) {
    mw.cookie.set(Adkey, cad.id, { expires: 7 * 86400, path: '/' });
    $.get('/w/api.php?action=parse&format=json&prop=text&page=MediaWiki:إعلانات/' + cad.page, function(getData) {
      $("#siteNotice").append('<div class="arabicads-notice" data-id="' + cad.id + '"><div class="arabicads-notice-close" style="visibility: visible;"><a role="button" title="أغلق"></a></div>' +
        getData.parse.text['*'].replace(/<!--[\s\S]*?-->/g, '') + '</div>');

      $(function() {
        // eslint-disable-next-line no-jquery/no-global-selector
        $('.arabicads-notice-close')
          .find('a')
          .on('click keypress', function(e) {
            if (
              e.type === 'click' ||
              e.type === 'keypress' && e.which === 13
            ) {
              const p = $(this).closest('.arabicads-notice');
              p.hide();
              mw.cookie.set(Adcookie + p.data('id'), '1', { expires: 7 * 86400, path: '/' });
            }
          });
      });

    })
  }
});