User:Evad37/Script modules
| This is a draft working towards a proposal for adoption as a Wikipedia policy, guideline, or process. The proposal must not be taken to represent consensus, but is still in development and under discussion, and has not yet reached the process of gathering consensus for adoption. Thus references or links to this page should not describe it as policy, guideline, nor yet even as a proposal. |
| The previous version of this draft is available here |
Script libraries are bits of Javascript code intended to be easily reused by userscripts.
The basics
Script libraries add an object to the Window object. This object contains one or more pieces of code intended to be used by other scripts – typically functions or classes.
Window.libSomethingUseful = {
doThis: function(param) {
// ...
return something;
},
doThat: function(p1, p2) {
// ...
return somethingElse;
}
}
The name used should be quite unique, and unlikely to be unintentionally overridden by other scripts.
To minimise the number of network requests, related functionality should all be contained within a single module, rather than several small modules. To minimise the size of network requests, unrelated functionality should be split between separate modules, rather than one huge module.
Dependencies
Script libraries should, if possible, avoid having dependencies themselves. This allows scripts to easily use them:
mw.loader.getScript( /* script url */ ).then(function() { // the library is now available on the Window object // ... } );
If necessary, then there are two options for handling dependencies:
- Specify dependencies in the documentation, and require scripts loading the library to be responsible for loading dependencies:
mw.loader.using( /* dependencies */ ).then(function( mw.loader.getScript( /* script url */ ).then(function() { // the library is now available on the Window object // ... } ); } );
- Load dependencies within the library itself, and attach a Promise/Deferred object to the window. Script using the library will need to access it using something like
mw.loader.getScript( /* script url */ ).then(function() { // A Promise is now available on the window object. $.when( /* library Promise */ ).then(function(libraryName) { // script gets to decide what to call the library // the library is now available, as a variable scoped to this function // ... } ); } );
Test cases
Should be required. Requires a little bit of effort to set up, but invaluable for making changes/updates much less likely to break things. Which could break a lot of things for a lot of users, if the library becomes widely used. Examples using QUnit: User:Evad37/rater/test.js, User:Evad37/Covery/sandbox.js
Pseudo-namespace?
Should script modules be stored in a pseudo-namespace like MediaWiki:Script module/? This means that only code approved by Interface Admins can be exported to other scripts, who could require non-minor updates to pass a WP:Code review.
Module authors wanting to add or update a script module would use an edit request, or post a request at the Interface administrators' noticeboard.
- Advantages: Less likely to break things; better security
- Disadvantages: More bureaucracy; likely take longer for bugs to be fixed; harder for non-intAdmin authors to maintain their stuff
Library ideas
- API helper. Make working with returned values easier – extracting response page or pages from the response data structure; format an error message from the response parameters; etc. Stuff I'm doing in XFDcloser which could be used more generally.
- OOUI helper. Multibutton confirm window like XFDcloser; open a process dialog with only a few lines of code, rather than the current method of having to create a whole new class per mw:OOUI/Windows/Process Dialogs; custom widgets I've made for various scripts
- Wikitext parsing. Stuff that can actually be a bit tricky, like parsing dates, signatures, and especially templates.
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.