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
This commit is contained in:
Jesús Martínez Novo 2017-12-24 16:43:38 +01:00
parent a75bbc4c46
commit 37c6d7d217

View file

@ -2,31 +2,37 @@
/**
* 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.
* failed for any reason, or the user is not logged-in.
*/
function setCookie() {
mw.cookie.set( 'cookiewarning_dismissed', true );
}
// 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();
} );
$( function () {
if ( mw.cookie.get( 'cookiewarning_dismissed' ) ) {
$( '.mw-cookiewarning-container' ).detach();
} else {
// use cookies for anonymous users
setCookie();
}
// always remove the cookiewarning element
$( '.mw-cookiewarning-container' ).detach();
// 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();
ev.preventDefault();
} );
}
} );
}( mediaWiki, jQuery ) );