diff --git a/extension.json b/extension.json index 57d9b2a..67bd038 100644 --- a/extension.json +++ b/extension.json @@ -18,6 +18,9 @@ ], "BeforePageDisplay": [ "CookieWarningHooks::onBeforePageDisplay" + ], + "GetPreferences": [ + "CookieWarningHooks::onGetPreferences" ] }, "config": { @@ -26,13 +29,22 @@ "ResourceModules": { "ext.CookieWarning": { "dependencies": [ + "mediawiki.api", "mediawiki.cookie" ], - "scripts": "resources/ext.CookieWarning/ext.CookieWarning.js" + "scripts": "resources/ext.CookieWarning/ext.CookieWarning.js", + "targets": [ + "mobile", + "desktop" + ] }, "ext.CookieWarning.styles": { "position": "top", - "styles": "resources/ext.CookieWarning/ext.CookieWarning.less" + "styles": "resources/ext.CookieWarning/ext.CookieWarning.less", + "targets": [ + "mobile", + "desktop" + ] } }, "ResourceFileModulePaths": { diff --git a/includes/CookieWarning.hooks.php b/includes/CookieWarning.hooks.php index fe7e649..341e347 100644 --- a/includes/CookieWarning.hooks.php +++ b/includes/CookieWarning.hooks.php @@ -39,13 +39,32 @@ class CookieWarningHooks { } private static function showWarning( IContextSource $context ) { + $user = $context->getUser(); $conf = ConfigFactory::getDefaultInstance()->makeConfig( 'cookiewarning' ); if ( $conf->get( 'CookieWarningEnabled' ) && + !$user->getBoolOption( 'cookiewarning_dismissed', false ) && !$context->getRequest()->getCookie( 'cookiewarning_dismissed' ) ) { return true; } return false; } + + /** + * GetPreferences hook handler + * + * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences + * + * @param User $user + * @param array $defaultPreferences + * @return bool + */ + public static function onGetPreferences( User $user, &$defaultPreferences ) { + $defaultPreferences['cookiewarning_dismissed'] = array( + 'type' => 'api', + 'default' => '0', + ); + return true; + } } diff --git a/resources/ext.CookieWarning/ext.CookieWarning.js b/resources/ext.CookieWarning/ext.CookieWarning.js index 26b4bf0..dc9958c 100644 --- a/resources/ext.CookieWarning/ext.CookieWarning.js +++ b/resources/ext.CookieWarning/ext.CookieWarning.js @@ -1,6 +1,20 @@ ( function ( mw, $ ) { - $( '.mw-cookiewarning-dismiss' ).on( 'click', function ( ev ) { + function setCookie() { mw.cookie.set( 'cookiewarning_dismissed', true ); + } + + $( '.mw-cookiewarning-dismiss' ).on( 'click', function ( ev ) { + if ( !mw.user.isAnon() ) { + new mw.Api().postWithToken( 'options', { + action: 'options', + change: 'cookiewarning_dismissed=1' + } ).fail( function ( code, result ) { + mw.log.warn( 'Failed to save dismissed CookieWarning: ' + code + '\n' + result.error + '. Using cookie now.' ); + setCookie(); + } ); + } else { + setCookie(); + } $( '.mw-cookiewarning-container' ).detach(); ev.preventDefault(); } ); diff --git a/resources/ext.CookieWarning/ext.CookieWarning.less b/resources/ext.CookieWarning/ext.CookieWarning.less index 227ae57..00cc48b 100644 --- a/resources/ext.CookieWarning/ext.CookieWarning.less +++ b/resources/ext.CookieWarning/ext.CookieWarning.less @@ -28,5 +28,13 @@ text-decoration: none !important; color: white !important; margin-right: 0.5em; + white-space: nowrap; + } +} + +.mw-mobile-mode { + .mw-cookiewarning-container { + bottom: 0; + top: inherit; } } \ No newline at end of file