0

I tried to customise the toolbar using a user script, following the tutorial on MediaWiki.org; however, sometimes when I load it from my common.js, I get this error in my console:

jQuery.Deferred exception: $(...).wikiEditor is not a function TypeError: $(...).wikiEditor is not a function
    at quickMAR_add (https://en.moegirl.org/index.php?title=User:Leranjun/quickMAR.js&action=raw&ctype=text/javascript:52:19)
    at https://en.moegirl.org/index.php?title=User:Leranjun/quickMAR.js&action=raw&ctype=text/javascript:60:12
    at mightThrow (https://en.moegirl.org/load.php?debug=false&lang=en&modules=jquery%2Cmediawiki&only=scripts&skin=vector&version=1o54dq6:49:598)
    at process (https://en.moegirl.org/load.php?debug=false&lang=en&modules=jquery%2Cmediawiki&only=scripts&skin=vector&version=1o54dq6:50:269) undefined

What's even weirder is that it sometimes works and sometimes not… And it always works if I copy/paste the code into the console and execute it manually.

The source code is here, if it's necessary.

Always Helping
  • 14,316
  • 4
  • 13
  • 29
  • You would only get this error if your script runs before the `wikiEditor` jQuery plugin is registered. We would need to see the HTML where you include jQuery, the plugin and your script above – Phil Jun 19 '20 at 02:10
  • @Phil My apologies, but I have no idea what you mean by the HTML and the plugin. Do you mean the editing page? – frankly.not.frank Jun 19 '20 at 02:15
  • @frankly.not.frank can you please add this your html page at the top `` – Always Helping Jun 19 '20 at 02:16
  • @AlwaysHelping Oopsies, forgot to mention that I'm not a sysop and can't make global changes. What's more, other jQuery functions are completely functional, so I'm pretty certain that jQuery is already included. – frankly.not.frank Jun 19 '20 at 02:18

1 Answers1

0

You need to wait for the toolbar to be ready:

mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
    $textarea.wikiEditor(...);
} );

The method described on the documentation page you linked is less elegant but would also work.

Tgr
  • 27,442
  • 12
  • 81
  • 118
  • I'm not too sure where to add this snippet though… Is it before calling the add function? – frankly.not.frank Jun 23 '20 at 10:05
  • You need to wrap your `$(...).wikiEditor(...)` calls like this, to ensure that `$(...).wikiEditor` is loaded by the time you call it. – Tgr Jun 23 '20 at 13:21