MediaWiki:Mobile.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

Any JavaScript here will be loaded for users using the mobile site. It is the equivalent of MediaWiki:Common.js for the mobile version.


/** As with common.js, any code here should be kept to a minimum. */
'use strict';
// [[Category:Wiktionary scripts]] <nowiki>
/*jshint shadow:true, undef:true, latedef:true, unused:true, es3:true */
/*global window, jQuery, mw, importScript, importStylesheet, $ */

/********************* Copied from Common.js as of [[Special:Permalink/81324584]] 17:14, 19 August 2024 ***********************/

/** [[WT:PREFS]] v2.0 **/
try {
	(function() {

		var prefs;
		try {
			prefs = window.localStorage.getItem('AGprefs');
		} catch (e) {
			prefs = jQuery.cookie('AGprefs');
		}

		prefs = prefs && JSON.parse(prefs);

		if (mw.config.get('wgUserGroups').indexOf('autoconfirmed') !== -1)
			return;

		if (mw.config.get('wgUserGroups').indexOf('user') === -1) {
			// XXX: [[Wiktionary:Preferences/V2]] is just a temporary page

			mw.loader.using(['mediawiki.util'], function() {
				mw.util.addPortletLink('p-personal', mw.util.getUrl('Wiktionary:Preferences for users without an account'),
					'Preferences', 'pt-agprefs', 'Personalise Wiktionary (settings are kept per-browser).', '',
					document.getElementById('pt-createaccount'));
			});

			if ((mw.config.get('wgAction') === 'view') && (mw.config.get('wgPageName') === 'Wiktionary:Preferences_for_users_without_an_account')) {
				mw.loader.load('ext.gadget.AGprefs'); // [[MediaWiki:Gadget-AGprefs.js]]
			}
		}

		if (!prefs)
			return;

		mw.loader.state('wiktionary_this_gadget_has_been_disabled_by_the_user', 'missing');
		for (var key in prefs.modules) {
			if (prefs.modules[key]) {
				mw.loader.load([key]);
			} else {
				// unavoidable race condition. to prevent it, every enabled-by-default gadget should have "site" as a dependency
				if (mw.loader.getState(key) !== 'ready') {
					mw.loader.moduleRegistry[key].dependencies.push('wiktionary_this_gadget_has_been_disabled_by_the_user');
					mw.loader.state(key, 'missing');
				} else {
					// XXX
					mw.log.warn(key + " could not be disabled; make sure it has 'site' declared as a dependency");
				}
			}
		}

		for (var key in prefs.sheets) {
			importStylesheet('MediaWiki:Gadget-' + key);
		}

		for (var key in prefs.scripts) {
			importScript('MediaWiki:Gadget-' + key);
		}

		if (mw.config.get('wgUserGroups').indexOf('user') !== -1)
			mw.loader.using([ 'mediawiki.api'], function() {
				var changes = [];
				for (var key in prefs.gadgets)
					changes.push('gadget-' + key + '=' + (prefs.gadgets[key] ? '1' : '0'));

				new mw.Api().postWithToken('options', {
					action: 'options',
					change: changes.join('|')
				}).then(function() {
					jQuery.cookie('AGprefs', null);
					try {
						window.localStorage.removeItem('AGprefs');
					} catch (e) { /* */ }
					mw.notify(
						jQuery('<b>Your <a href="/wiki/Wiktionary:Preferences/V2">per-browser preferences</a> have been migrated</b><br/><br/>' +
							'From now on, you should use your <a href="/wiki/Special:Preferences">user preferences page</a>. ' +
							'Preferences will no longer apply after you log out.')
					);
				});
			});

	})();
} catch (e) {
	mw.log.warn(e);
}

/* ==Page specific extensions== */
/* ===[[Wiktionary:Main Page]]=== */
mw.loader.using("mediawiki.util", function(){
	if (mw.config.get('wgIsMainPage')) {
		// Workaround for [[phab:T335552]].
		document.querySelector(".mw-searchInput").autocapitalize = "off";
	}
});

/********************* End copied from Common.js ***********************/

// Make "edit all" the default by stopping `section=0` from getting added to the link.
var editButton = document.querySelector("#page-actions-edit > a");
if (editButton) {
	var m = new MutationObserver(function() {
		if (editButton.href.slice(-10) === "&section=0") {
			editButton.href = editButton.href.slice(0, -10);
		}
	});
	m.observe(editButton, {attributes: true});
}

// </nowiki>