User:Saftzie/app.usrchk.js

ShoutWiki — express yourself and be heard!
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/**
 * Check localStorage and cookie function in the browser
 */
(function (mw, $) {
	'use strict';

	var
		PREFIX = 'js-usrchk',
		ta = $('<textarea rows="8" readonly="readonly" style="min-width: 250px;"></textarea>'),
		s;

	if ($('#' + PREFIX).length === 0) {
		return; // nothing to do
	}
	$('#' + PREFIX).empty().append(ta);
	ta = ta[0];

	if (typeof Storage === undefined) {
		ta.value += 'localStorage is not available.\n';
	} else {
		ta.value += 'localStorage is available.\n';
		try {
			s = 'Failed to set localStorage';
			localStorage.setItem(PREFIX, PREFIX);
			ta.value += 'Set localStorage.\n';
			s = 'Failed to read localStorage';
			if (localStorage.getItem(PREFIX) === PREFIX) {
				ta.value += 'localStorage set/read correctly.\n';
			} else {
				ta.value += 'localStorage did not set/read correctly.\n';
			}
			s = 'Failed to delete localStorage';
			localStorage.removeItem(PREFIX);
			if (localStorage.getItem(PREFIX) === null) {
				ta.value += 'localStorage deleted.\n';
			} else {
				ta.value += 'localStorage did not delete.\n';
			}
		} catch (err) {
			ta.value += s + '\nerr.message = ' + err.message;
		}
	}

	ta.value += 'Loading jquery.cookie\n';
	mw.loader.using( 'jquery.cookie', function ( ) {
		try {
			s = 'Failed to set cookie.';
			$.cookie(PREFIX, PREFIX, { expires: 7, path: '/' });
			ta.value += 'Set cookie.\n';
			s = 'Failed to read cookie.';
			if ($.cookie(PREFIX) === PREFIX) {
				ta.value += 'Cookie set/read correctly.\n';
			} else {
				ta.value += 'Cookie did not set/read correctly.\n';
			}
			s = 'Failed to delete cookie.';
			$.cookie(PREFIX, null, { path: '/' }); // path must match
			if ($.cookie(PREFIX) === null) {
				ta.value += 'Cookie deleted.\n';
			} else {
				ta.value += 'Cookie did not delete.\n';
			}
		} catch (err) {
			ta.value += s + '\nerr.message = ' + err.message;
		}
	} );
}(mediaWiki, jQuery));