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?
Could you take a look at what I have so far. I understand what you are sayig and I tried adding the code to the Common.js, but I'm just not sure what exactly I should put where in terms of the rest of the code.
Fandyllic: What Lunarity meant by his post above is that jQuery is not available in a very special case. In the ordinary case where you put a script in your MediaWiki:Wikia.js or MediaWiki:Common.js, there is no problem. But in the special case where you put a script in a MediaWiki page and then include it with verbatim tags, jQuery will not be available. It has to do with the order in which Resource Loader loads things. Resource loader loads jQuery after all wikitext/HTML content of the page has been parsed. If you're using verbatim tags to load a script, that script is in the HTML content of the page and will be parsed before Resource Loader loads jQuery. So basically, jQuery is loaded, it's just loaded too late.
Ahh... hate those special cases. Okay, so inspecting a wikia page, it looks like JQUery loads from here: http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
Would it be easier to try to load the JQuery in the verbatim stuff or just re-write the code?
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.