This is a subpage of Evad37's talk page, where you can send them messages and comments. |
|
Script module importation could be extended to include version control. This would necessitate each script module having an associated json page mapping version numbers to old revision ids. The importScriptModule function, if given a version number as a second parameter, could first import the script module's json page, and add the oldid= parameter with the appropriate value to the url for loading the script. There should be a way of specifying partial version numbers, and/or greater-than/less-than a version number, similar to how npm does it. The exportScriptModule function would also need to know the version number, and store the exported function/object at Window.exportScriptModule[name][version]. Perhaps the json pages could be maintained by bot. - Evad37 [talk] 03:02, 26 January 2019 (UTC)
Regardless of if this is implemented or not, it would be useful to have a collection of such "script modules" (if implemented, they form the basis of the "namespace," and if not, they could still serve its purpose). Accordingly, if anyone has a script that they want to add, please post it here (I'll think of a better system soon) so that we can keep track of them. Thanks, --DannyS712 (talk) 04:44, 26 January 2019 (UTC)
I think this is a good idea, but have a couple of points:
mw.loader.using
, allowing multiple imports to be declared in an array, so that $.when
need not be used?SD0001 (talk) 05:41, 26 January 2019 (UTC)
[allow] multiple imports to be declared in an array–
I find it interesting that you want to create a pseudo-namespace for these script modules, when the review process for them are the same as other global scripts in technical terms. It seems that the purpose here is to prevent users from loading the rest of the MediaWiki namespace into their user scripts. What use cases are you expecting where we want to block a normal user from loading a sitewide script into their own user script? Deryck C. 19:56, 26 January 2019 (UTC)
expecting where we want to block a normal user fromas "expecting where blocked users would be..." or something like that. But, what do you mean by
prevent users from loading the rest of the MediaWiki namespace into their user scripts? I'm confused by what you are referring to... --DannyS712 (talk) 20:29, 26 January 2019 (UTC)
the review process for them are the same as other global scripts in technical terms– the review process is meant to be a very low bar to cross, basically just "not evil", as opposed to gadgets which have more criteria and require a proposal to gather consensus.
It seems that the purpose here is to prevent users from loading the rest of the MediaWiki namespace into their user scripts– The purpose is to only allow script modules to be loaded using Window.importScriptModule. Other types of MediaWiki scripts can be loaded using conventional means, such as how XFDcloser has a
importScript('MediaWiki:Gadget-morebits.js');
line, and later on uses new Morebits.simpleWindow(
...etc... .What use cases are you expecting where we want to block a normal user from loading a sitewide script into their own user script?– When using Window.importScriptModule is going to return a rejected promise, because you tried to load a random script that doesn't use the
Window.exportScriptModule('name
', IIFE
);
syntaxWhy do we want to stop users from loading MediaWiki namespace scripts in general, given that all of them have been reviewed by IAdmins?– We don't, but only the special script modules using the correct syntax can be imported using Window.importScriptModule. Other scripts have to be imported by other means.
Here are some sample script modules:
Converting a regular user script to a script module isn't too hard:
Window.exportScriptModule('moduleName
', (function() {
to the top})());
to the bottom- Evad37 [talk] 00:22, 28 January 2019 (UTC)
What's the problem being solved here? It seems to add a decent amount of complexity...for what gain exactly?
Creating a shared library seems like a decent idea, but I don't see much advantage to creating small individual modules compared to a single grabbag of stuff. I would also suggest looking at what Commons has (see c:Special:Gadgets, the lib* ones), and whether we can leverage that.
HTH, Legoktm (talk) 01:06, 28 January 2019 (UTC)
var fooIsLoaded = Window.foo !== undefined;
$.when(
fooIsLoaded ||
$.getScript('https://en.wikipedia.org/w/index.php?title=User:Example/foo.js&action=raw&ctype=text/javascript')
).then(function() {
// code that uses Window.foo
})
importScriptModule('foo').then(//... foo is ready to be used, and was not reloaded if already loaded
mw.loader.using
for stuff other than ResourceLoader modules. I wonder why mw.loader.using
doesn't work for external scripts, while mw.loader.load
does? That aside, the Commons libraries seem to be great, especially c:MediaWiki:Gadget-libWikiDOM.js. Since they are gadgets, do they get loaded on all pages? Otherwise, looking at a search result, they seem to be unused. SD0001 (talk) 09:10, 28 January 2019 (UTC)
mw.loader.using
. That's what I'd recommend doing instead of inventing a parallel system, especially since it's already planned to use the MediaWiki namespace. Legoktm (talk) 08:12, 29 January 2019 (UTC)
mw.loader.using
"? It seems to always return a rejected promise with an Unknown dependency error (when used with anything other than the modules listed at mw:RL/CM). - Evad37 [talk] 09:00, 29 January 2019 (UTC)
mw.loader.using('ext.gadget.Navigation_popups').done(() => console.log('success!'));
, and see "success!" printed. You can use mw.loader.getState()
to see what the current state is, 'ready' for loaded, 'registered' for not loaded, and null if the module doesn't exist. Legoktm (talk) 17:03, 29 January 2019 (UTC)
ext.gadget.
prefix was needed – that should really be documented on mw:RL/CM, and possibly elsewhere. But in any case, I was trying to load MediaWiki:Gadget-morebits.js – still doesn't work, probably because it doesn't have it's own line on MediaWiki:Gadgets-definition (loading twinkle using mw.loader.using('ext.gadget.Twinkle')
works as expected).mw.loader.using
but for any userscript could still be useful – basically a wrapper for $.getScript
, but taking a wiki page name like (importScript
), or an array of page names (like mw.loader.using
), and keeping track of what's already been loaded (by other scripts) to prevent unnecessary requests being sent. - Evad37 [talk] 00:55, 1 February 2019 (UTC)
These aren't in the right format, but some scripts of mine that would be useful as "script modules" IMO:
--DannyS712 (talk) 07:17, 23 February 2019 (UTC)
var arrayFromResponsePages = function(response) {
return $.map(response.query.pages, function(page) { return page; });
};
var pageFromResponse = function(response) {
return arrayFromResponsePages(response)[0];
};
var makeErrorMsg = function(code, jqxhr) {
var details = '';
if ( code === 'http' && jqxhr.textStatus === 'error' ) {
details = 'HTTP error ' + jqxhr.xhr.status;
} else if ( code === 'http' ) {
details = 'HTTP error: ' + jqxhr.textStatus;
} else if ( code === 'ok-but-empty' ) {
details = 'Error: Got an empty response from the server';
} else {
details = 'API error: ' + code;
}
return details;
};
// import `apiLib` from script module/library
var api = new mw.Api( { ajax: { headers: { 'Api-User-Agent': /* your info here */ } } } );
// ...
api.get( apiLib.query("Page title") )
.then(apiLib.pageWikitextFromResponse)
.then(function(pageWikitext) { /* ... do stuff ... */ });