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-09-16 19:18:49 +00:00
|
|
|
( function ( mw, $ ) {
|
|
|
|
'use strict';
|
|
|
|
var geoLocation;
|
|
|
|
|
|
|
|
geoLocation = {
|
|
|
|
/**
|
2018-03-22 20:20:28 +00:00
|
|
|
* @return {string} Two-letter country code
|
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-09-16 19:18:49 +00:00
|
|
|
*/
|
|
|
|
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, {
|
2018-08-27 19:09:25 +00:00
|
|
|
domain: mw.config.get( 'wgCookieDomain' ),
|
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-09-16 19:18:49 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
} );
|
2018-03-22 20:20:28 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|