mediawiki-extensions-Cookie.../resources/ext.CookieWarning.geolocation/cookiePolicy.js
Florian Schmidt c35b78fae8 Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.

With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.

Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.

Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-10-07 23:51:18 +02:00

53 lines
1.5 KiB
JavaScript

/* global Geo, mediaWiki */
( function ( mw, $ ) {
'use strict';
var geoLocation;
geoLocation = {
/**
* @return string Two-letter country code
*/
getCountryCode: function () {
/**
* safe fallback -- if geolocation fails, display the notice anyway
*/
var countryCode = 'EU';
if ( !$.cookie( 'euCookieWarningCountryCode' ) ) {
// @see http://www.dwuser.com/education/content/web-services-made-practical-where-are-your-visitors-from/
$.get( mw.config.get( 'wgCookieWarningGeoIPServiceURL' ), function ( data ) {
// Get the country code
countryCode = data.country_code;
// Store the result in a cookie (ah, the sweet, sweet irony) to
// avoid hitting the geolocation service unnecessarily
$.cookie( 'euCookieWarningCountryCode', countryCode, {
domain: window.mw.config.get( 'wgCookieDomain' ),
path: '/',
expires: 30
} );
}, 'jsonp' );
} else if ( $.cookie( 'euCookieWarningCountryCode' ) !== null ) {
countryCode = $.cookie( 'euCookieWarningCountryCode' );
}
return countryCode;
},
/**
* Check if the supplied country code is that of a configured region.
*
* @return {boolean}
*/
isInRegion: function () {
return mw.config.get( 'wgCookieWarningForCountryCodes' ).hasOwnProperty( this.getCountryCode() );
}
};
$( function () {
if ( geoLocation.isInRegion() ) {
$( '.mw-cookiewarning-container' ).show();
} else {
$( '.mw-cookiewarning-container' ).detach();
}
} );
}( mediaWiki, jQuery ) );