Help:Writing a gadget

Revision as of 08:57, 12 September 2023 by Rob Kam (talk | contribs) (Rob Kam moved page Writing a gadget to Help:Writing a gadget without leaving a redirect)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

How to add utility gadgets for all users of the wiki. These gadgets will be hidden from the Preferences page. The finished gadgets can be found from the Special:Gadgets page.

1. Add the following line to MediaWiki:Gadgets-definition:

* mygadget[ResourceLoader|hidden|default]|mygadget.js|mygadget.css

2. Enter some description in MediaWiki:Gadget-mygadget

My gadget template, do not edit.

3. Add the following JavaScript code to MediaWiki:Gadget-mygadget.js:

/**
 * mygadget Gadget JavaScript
 * Authors: 
 * Description: This gadget replaces the content within elements with the class "mygadget"
 *              with the text "Hello, World!" and appends it below the existing content.
 */

$(document).ready(function() {
    // Find all elements with the class "mygadget" and append a line break and "Hello, World!" after the existing content.
    $('.mygadget').each(function() {
        var helloWorld = $(this).find('<!-- BEGIN-HELLO-WORLD -->');
        helloWorld.after('<br />Hello, World!');
    });
});

4. Edit MediaWiki:Gadget-mygadget.css:


5. Create Template:mygadget with the following content:

<div class="mygadget">
This is some text.<br />
<!-- BEGIN-HELLO-WORLD -->
Hello, World!
</div>

6. Use the following code to insert your gadget on a page:

Some content above.
{{mygadget}}
Some content below.