%PDF- %PDF-
| Direktori : /www/varak.net/wiki.varak.net/extensions/CookiePolicy/scripts/ |
| Current File : /www/varak.net/wiki.varak.net/extensions/CookiePolicy/scripts/cookiePolicy.js |
/* global Geo, mediaWiki */
( function ( mw, $ ) {
'use strict';
/**
* Initialize JS for whether or not to show a banner notifying European Union
* users that we use cookies on our site, per EU law.
* Users can dismiss the notification and will not be shown again, unless
* they clear their cookies.
*/
function initCookieNotification() {
if ( shouldShowBanner() ) {
showBanner();
}
}
/**
* @return string Two-letter country code
*/
function getCountryCode() {
/**
* safe fallback -- if geolocation fails, display the notice anyway
*/
var countryCode = 'EU';
if ( !$.cookie( 'euCookiePolicyCountryCode' ) ) {
// @see http://www.dwuser.com/education/content/web-services-made-practical-where-are-your-visitors-from/
$.get( '//' + mw.config.get( 'wgCookiePolicyGeoIPServiceURL' ) + '/json/', 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( 'euCookiePolicyCountryCode', countryCode, {
domain: window.mw.config.get( 'wgCookieDomain' ),
path: '/',
expires: 30
} );
}, 'jsonp' );
} else if ( $.cookie( 'euCookiePolicyCountryCode' ) !== null ) {
countryCode = $.cookie( 'euCookiePolicyCountryCode' );
}
return countryCode;
}
/**
* Check if user is from the EU and hasn't seen the banner yet
* @return {boolean}
*/
function shouldShowBanner() {
var countryCode = getCountryCode();
var fromEU = isEuropeanCountry( countryCode );
return ( fromEU && !hasSeenBanner() );
}
/**
* Check if the supplied country code is that of an European state.
*
* List of countries pulled from http://dev.maxmind.com/geoip/legacy/codes/eu_country_list/
* on 19 August 2015.
*
* @return {boolean}
*/
function isEuropeanCountry( countryCode ) {
return $.inArray( countryCode, [
'EU', // Europe
'AD', // Andorra
'AL', // Albania
'AT', // Austria
'BA', // Bosnia and Herzegovina
'BE', // Belgium
'BG', // Bulgaria
'BY', // Belarus
'CH', // Switzerland
'CS', // Serbia and Montenegro
'CZ', // Czech Republic
'DE', // Germany
'DK', // Denmark
'EE', // Estonia
'ES', // Spain
'FI', // Finland
'FO', // Faroe Islands
'FR', // France
'FX', // France, Metropolitan
'GB', // United Kingdom
'GI', // Gibraltar
'GR', // Greece
'HR', // Croatia
'HU', // Hungary
'IE', // Ireland
'IS', // Iceland
'IT', // Italy
'LI', // Liechtenstein
'LT', // Lithuania
'LU', // Luxembourg
'LV', // Latvia
'MC', // Monaco
'MD', // Moldova, Republic of
'MK', // Macedonia
'MT', // Malta
'NL', // Netherlands
'NO', // Norway
'PL', // Poland
'PT', // Portugal
'RO', // Romania
'SE', // Sweden
'SI', // Slovenia
'SJ', // Svalbard and Jan Mayen
'SK', // Slovakia
'SM', // San Marino
'UA', // Ukraine
'VA' // Holy See (Vatican City State)
] ) > -1;
}
/**
* Display the cookie policy message to the user
*/
function showBanner() {
var message = formatMessage();
mw.notify( $.parseHTML( message ), { autoHide: false, tag: 'cookiePolicy' } );
// bind a click handler on the notification and only *after* the user
// clicks on the notification, set the cookie
$( document ).one( 'click', '.mw-notification-tag-cookiePolicy', function() {
setCookie();
} );
}
/**
* This is just nasty; I'm so sorry. --ashley, 20 August 2015
*/
function formatMessage() {
var message = mw.message( 'cookie-policy-notification-message' ),
policyURL = mw.message( 'cookie-policy-link' );
var readMoreMsg = '<a target="_blank" href="' + policyURL + '">' +
mw.message( 'cookie-policy-read-more' ) + '</a>';
message = String( message );
message = message + '<br />' + readMoreMsg;
return message;
}
/**
* Check if the user has seen the cookie notification banner before.
* @return {boolean}
*/
function hasSeenBanner() {
return !!$.cookie( 'euCookiePolicy' );
}
/**
* Set a cookie so we know not to show the policy banner again.
*/
function setCookie() {
var date = new Date( '31 December 2037' );
$.cookie( 'euCookiePolicy', '1', {
domain: window.mw.config.get( 'wgCookieDomain' ),
path: '/',
// original code used 'never' but $.cookie doesn't like that
// also see http://stackoverflow.com/questions/9466829/jquery-cookie-persistence
expires: date
} );
}
$( function () {
initCookieNotification();
} );
}( mediaWiki, jQuery ) );