tl;dr: What's the right way for a tag extension to execute JavaScript provided by the user? (On a private wiki without Internet access.)
Details: I run a private wiki for developers (not accessible from the Internet) that lets any wiki page author run JavaScript on a page by adding a tag:
<javascript> alert("hi"); </javascript>
(We understand the security implications, which is why the wiki isn't accessible by the world.) When we upgraded to MediaWiki 1.26 (from 1.24), a problem occurred: the <javascript> tag stopped recognizing the "mediawiki" and "mw" objects, but otherwise works. The following code reports an undefined variable "mw":
<javascript> mw.loader.using(....) </javascript>
I assume this is because the <javascript> extension builds a <script> tag as a string and uses the SkinAfterBottomScripts hook to add it to the page, rather than using ResourceLoader. However, I cannot figure out how to use ResourceLoader to add JavaScript provided on the wiki page like my small examples above. We can't use the array $wgResourceModules[$name]['scripts'] because the JavaScript isn't in a static file.
So... what's the right method for injecting author-supplied JavaScript in this manner?
I've already tried using ResourceLoader to add 'mediawiki' to $wgResourceModules[$name]['dependencies']. It didn't work, complaining that 'mediawiki' was not a known dependency. I also read https://www.mediawiki.org/wiki/ResourceLoader/Migration_guide_for_extension_... but did not find an answer.
Thanks for any advice! DanB
On 12/29/2015 03:59 PM, Daniel Barrett wrote:
I run a private wiki for developers (not accessible from the Internet) that lets any wiki page author run JavaScript on a page by adding a tag:
<javascript> alert("hi"); </javascript>
(We understand the security implications, which is why the wiki isn't accessible by the world.) When we upgraded to MediaWiki 1.26 (from 1.24), a problem occurred: the <javascript> tag stopped recognizing the "mediawiki" and "mw" objects, but otherwise works. The following code reports an undefined variable "mw":
<javascript> mw.loader.using(....) </javascript>
I assume this is because the <javascript> extension builds a <script> tag as a string and uses the SkinAfterBottomScripts hook to add it to the page, rather than using ResourceLoader. However, I cannot figure out how to use ResourceLoader to add JavaScript provided on the wiki page like my small examples above. We can't use the array $wgResourceModules[$name]['scripts'] because the JavaScript isn't in a static file.
Since MediaWiki 1.26, all ResourceLoader modules are loaded asynchronously, so inline scripts now need to account for the possibility they may be executed before the jquery and mediawiki modules have loaded.
If you use ResourceLoader::makeInlineScript() to build the HTML script element, your JavaScript code will be automatically wrapped in an anonymous function and queued for execution once ResourceLoader has started up.
Kevin Israel writes:
Since MediaWiki 1.26, all ResourceLoader modules are loaded asynchronously, so inline scripts now need to account for the possibility they may be executed before the jquery and mediawiki modules have loaded.
If you use ResourceLoader::makeInlineScript() to build the HTML script element, your JavaScript code will be automatically wrapped in an anonymous function and queued for execution once ResourceLoader has started up.
Thank you! I have documented your advice at https://www.mediawiki.org/wiki/ResourceLoader/Migration_guide_%28users%29#In....
DanB
Hmm… actually, looking at the ResourceLoader code, the makeInlineScript() function does not actually queue the script to execute. It only returns a properly wrapped, anonymized function as a string. I still needed to call OutputPage->addScript() to make it execute.
DanB
From: Wikitech-l [mailto:wikitech-l-bounces@lists.wikimedia.org] On Behalf Of Daniel Barrett Sent: Wednesday, December 30, 2015 11:03 AM To: Wikimedia developers Subject: Re: [Wikitech-l] The right way to inject user-supplied JavaScript?
Kevin Israel writes:
Since MediaWiki 1.26, all ResourceLoader modules are loaded asynchronously, so inline scripts now need to account for the possibility they may be executed before the jquery and mediawiki modules have loaded.
If you use ResourceLoader::makeInlineScript() to build the HTML script element, your JavaScript code will be automatically wrapped in an anonymous function and queued for execution once ResourceLoader has started up.
Thank you! I have documented your advice at https://www.mediawiki.org/wiki/ResourceLoader/Migration_guide_%28users%29#In....
DanB _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
Hi,
On 12/29/2015 12:59 PM, Daniel Barrett wrote:
tl;dr: What's the right way for a tag extension to execute JavaScript provided by the user? (On a private wiki without Internet access.)
Details: I run a private wiki for developers (not accessible from the Internet) that lets any wiki page author run JavaScript on a page by adding a tag:
<javascript> alert("hi"); </javascript>
(We understand the security implications, which is why the wiki isn't accessible by the world.) When we upgraded to MediaWiki 1.26 (from 1.24), a problem occurred: the <javascript> tag stopped recognizing the "mediawiki" and "mw" objects, but otherwise works. The following code reports an undefined variable "mw":
<javascript> mw.loader.using(....) </javascript>
I assume this is because the <javascript> extension builds a <script> tag as a string and uses the SkinAfterBottomScripts hook to add it to the page, rather than using ResourceLoader. However, I cannot figure out how to use ResourceLoader to add JavaScript provided on the wiki page like my small examples above. We can't use the array $wgResourceModules[$name]['scripts'] because the JavaScript isn't in a static file.
You should use ResourceLoader::makeInlineScript(), which will wrap it in a closure and adds it to the window.RLQ global to be executed once mediawiki.js and stuff are set up.
-- Legoktm
On 29 dec. 2015, at 21:59, Daniel Barrett danb@cimpress.com wrote: I also read https://www.mediawiki.org/wiki/ResourceLoader/Migration_guide_for_extension_... but did not find an answer.
Hmm, we should really update parts of those guides btw. They not yet take 1.26 into account really.
DJ
wikitech-l@lists.wikimedia.org