Topic on Project:Support desk

ResourceLoader: Add Javascript for logged in users

7
Subfader (talkcontribs)

I have a few KB worth JS which I add in the skin when the user is logged in.

I'd like to add that code to MediaWiki:Common.js. Can I use ResourceLoader so it is only applied to logged in users?

Florianschmidtwelzow (talkcontribs)

Hello,

sure. Just create a new ResourceLoader module and add a function to the hook BeforePageDisplay and add the module only, if the user is logged in, example:

$wgResourceModules['zzz.custom.js'] = array(
	'scripts' => array(
		// path to the script
	),
	'dependencies' => array(
		// if you have any
	),
	'localBasePath' => __DIR__,
	'remoteExtPath' => '/'
);
$wgHooks['BeforePageDisplay'][] = 'onBeforePageDisplay';

public static function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
	$user = $out->getUser();

	if ( $user->isLoggedIn() ) {
		$out->addModules( 'zzz.custom.js' );
	}
}
Legoktm (talkcontribs)

You can use MediaWiki:Group-user.js, which will apply to logged in users only.

Florianschmidtwelzow (talkcontribs)

Will be this loaded for every logged in user, or just for those, who are in the group users?

88.130.120.5 (talkcontribs)

All users, who are logged in, are in the group user. ;-)

Florianschmidtwelzow (talkcontribs)
  • facepalm* Ah, oops, my fault :P Thanks!
Subfader (talkcontribs)
Reply to "ResourceLoader: Add Javascript for logged in users"