Currently I am working on a script, and I would like to have a new module on the sidebar where I can put the button to run the script.
How do you do that?
Also would I be able to do that for a wiki wide script or just a personal script?
Currently I am working on a script, and I would like to have a new module on the sidebar where I can put the button to run the script.
How do you do that?
Also would I be able to do that for a wiki wide script or just a personal script?
This is a bit off topic from what Rocketslime is asking about in this thread, but yes, it would be easy to manually load jQuery in the verbatim stuff (for example, w:c:terraria:MediaWiki:Snowstorm.js which is verbatimed on the main page of the wiki). But this is not exactly the most intuitive solution, and while it will allow jQuery to work, it does not solve the second issue that Lunarity mentioned about wg* variables being unavailable.
Actually, jQuery overrides itself in code order. The second copy will replace the first. There is not a major problem with this but there are subtle ones like $.fn
extensions getting lost, and memory usage inflation due to both copies of jQuery existing in memory even if only the latest loaded one is visible (event handlers keep the invisible copy from being GCed, the second copy of jQuery will also remember the first one internally).
The scripts loading in the footer is a performance optimisation due to the way browsers process script tags. The defer
and async
attributes sort of fix it, but they're buggy. Loading conditionally via Common.js provides the normal environment you expect and is a lot easier than trying to wallpaper over the problems unless you really, really need the code to run immediately when the browser reaches that point on the page. That sort of situation is rare, since most JS uses $(document).ready()
which fires when the page is entirely parsed (footer) anyway.
As I said, the simplest thing to do is put:
jQuery(function($) { if ($('button.ben').length) { importArticles({ type: 'script', article: 'User:Rocketslime 1 1/Ben 2.0.js' }); } });
in Common.js. Then put:
<button class="ben" onclick="run()">Run Script</button>
in your verbatim page. [Although I would bind the event from JS instead of hardcoding it directly into the HTML]
You really should not write your own verbatim
tag JavaScript code unless you are very sure you know what you are doing and why you need to do it that way. Verbatim script tags act as a roadblock that stops the browser dead in its tracks whilst the JS runs and will only resume processing the page after the JS completes.
A more general solution to this sort of problem is to put this in Common.js:
if ($.isArray(window.pageNeededScripts)) { importArticles({ type:'script', articles: window.pageNeededScripts }); }
Then create verbatim pages that have:
<script type="text/javascript"> window.pageNeededScripts = window.pageNeededScripts || []; window.pageNeededScripts.push('MediaWiki:Common.js/somepage.js'); </script>
[Caveat: Verbatim tags HTML encode the content, so symbols like ampersand get changed to '&
' which breaks things if you have those sorts of characters in your page name.]
Hmm... I made that changes that you said, but it still doesn't seem to be working.
The first problem is that you pasted the wrong code in Common.js. It should be button.ben
not .ben button
. You made the same mistake in the actual code itself, change that in there as well and it should work. [Also, remove onclick="run()"
from the button in the verbatim page.
Ok, I fixed the code and it worked. However only the button in the page's content section was able to run the script, and the one in the sidebar couldn't.
I tried adding the class "ben" to the button in the sidebar, but that looks like it has made the code not work. Do you know what is causing the problem?
I added the class because that should have gotten it to run the code when clicked.
The code is here: http://rocketslime11-test.wikia.com/wiki/User:Rocketslime_1_1/Ben_2.0.js