mediawiki-extensions-Cookie.../resources/ext.CookieWarning/ext.CookieWarning.js
Jesús Martínez Novo 37c6d7d217 Check also the dismissed cookie on client-side
If geolocation is done at client-side, it was displaying the warning without
checking the dismissed cookie. Now the main module will remove it if the cookie
exists, and the geolocation service won't display it again.

The idea of doing geolocation at client side rather than server side is to
allow more pages cached at varnish/squid, but it still required to let the
dismissed cookie break the cache. With this change, the dismissed cookie can
be blocked at the varnish/squid level, all pages will contain the cookie
warning notice, but it will be left hidden by the client-side script instead
of displaying it.

In addition, the click handler for .mw-cookiewarning-dismiss has been put
inside a DOMready handler to prevent this code fire before the element becomes
available.

Bug: T183657
Change-Id: I5f7505dbd650867155485d17fff58fb92800581b
2018-04-12 22:46:16 +02:00

39 lines
1.3 KiB
JavaScript

( function ( mw, $ ) {
/**
* Sets the cookie, that the cookiewarning is dismissed. Called,
* when the api query to save this information in the user preferences,
* failed for any reason, or the user is not logged-in.
*/
function setCookie() {
mw.cookie.set( 'cookiewarning_dismissed', true );
}
$( function () {
if ( mw.cookie.get( 'cookiewarning_dismissed' ) ) {
$( '.mw-cookiewarning-container' ).detach();
} else {
// Click handler for the "Ok" element in the cookiewarning information bar
$( '.mw-cookiewarning-dismiss' ).on( 'click', function ( ev ) {
// an anonymous user doesn't have preferences, so don't try to save this in
// the user preferences.
if ( !mw.user.isAnon() ) {
// try to save, that the cookiewarning was disabled, in the user preferences
new mw.Api().saveOption( 'cookiewarning_dismissed', '1' )
.fail( function ( code, result ) {
// if it fails, fall back to the cookie
mw.log.warn( 'Failed to save dismissed CookieWarning: ' + code + '\n' + result.error + '. Using cookie now.' );
setCookie();
} );
} else {
// use cookies for anonymous users
setCookie();
}
// always remove the cookiewarning element
$( '.mw-cookiewarning-container' ).detach();
ev.preventDefault();
} );
}
} );
}( mediaWiki, jQuery ) );