* Roan Kattouw roan.kattouw@gmail.com [Fri, 5 Nov 2010 14:43:57 +0100]:
You totally can. It's not recommended, and it'll be less performant, but you can use it and it'll work. Your script tag will end up near the bottom of the <body> though, so you can't use inline JS to call functions defined in that script.
I am viewing generated HTML and don't see my script anywhere, neither in top nor bottom. It is a special page and I've used $wgOut->addScript() in "overloaded" SpecialPage::execute(). Works just fine in 1.15.x, while in relatively fresh trunk this $wgOut->addScript() is completely ignored - like it wasn't there. The "old" code is in repo Extension:WikiSync. But, $wgOut->addLink() in SpecialPage::execute() works just fine, css is being added, scripts are not! I've tried to switch to ResourceLoader, in case such class exists; however cannot get it to work.
$wgHooks['ResourceLoaderRegisterModules'][] = 'WikiSyncSetup::registerModules'; $wgHooks['MakeGlobalVariablesScript'][] = 'WikiSyncSetup::MakeGlobalVariablesScript'; ... I am trying MakeGlobalVariablesScript to call ResourceLoader::addModules() because when it was in SpecialPage::execute(), addModules() was performed before registerModules (which is probably wrong). I've also tried OutputPageParserOutput, but that is not being executed in SpecialPage (which is probably right, because some Special pages can live without parser at all).. ... static function registerModules( $resourceLoader ) { global $wgExtensionAssetsPath; $localpath = dirname( __FILE__ ); $remotepath = "$wgExtensionAssetsPath/WikiSync"; $resourceLoader->register( array( 'ext.wikisync' => new ResourceLoaderFileModule( array( 'scripts' => 'WikiSync.js', 'styles' => 'WikiSync.css', 'messages' => self::$jsMessages ), $localpath, $remotepath ) ) ); return true; }
static function MakeGlobalVariablesScript( &$vars ) { global $wgOut, $wgContLang; self::headScripts( $wgOut, $wgContLang->isRTL() ); return true; }
/* * include stylesheets and scripts; set javascript variables * @param $outputPage - an instance of OutputPage * @param $isRTL - whether the current language is RTL */ static function headScripts( &$outputPage, $isRTL ) { global $wgJsMimeType; if ( class_exists( 'ResourceLoader' ) ) { # $outputPage->addModules( 'jquery' ); $outputPage->addModules( 'ext.wikisync' ); return; } $outputPage->addLink( array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => self::$ScriptPath . '/WikiSync.css?' . self::$version ) ); if ( $isRTL ) { $outputPage->addLink( array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => self::$ScriptPath . '/WikiSync_rtl.css?' . self::$version ) ); } $outputPage->addScript( '<script type="' . $wgJsMimeType . '" src="' . self::$ScriptPath . '/WikiSync.js?' . self::$version . '"></script> <script type="' . $wgJsMimeType . '" src="' . self::$ScriptPath . '/WikiSync_Utils.js?' . self::$version . '"></script> <script type="' . $wgJsMimeType . '">WikiSyncUtils.addEvent(window,"load",WikiSync.onloadHandler);</script> <script type="' . $wgJsMimeType . '"> WikiSync.setLocalMessages( ' . self::getJsObject( 'wsLocalMessages', self::$jsMessages ) . ');</script>' . "\n" ); }
As long as they're not using inline JS, they won't break.
Unfortunately, I've used a bit of inline JS. How do I add window.onload? Inside "main flow" of WikiSync.js ? I'll try that however currently I cannot get registerModule() and addModules() to work. Probably it's something simple, sorry. Dmitriy