This page is within the scope of WikiProject JavaScript, a collaborative effort to improve the coverage of articles related to JavaScript, and to the development of user scripts for use on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.JavaScriptWikipedia:WikiProject JavaScriptTemplate:WikiProject JavaScriptJavaScript
What scripts are you working on or maintaining, or would like to build?
Feel free to share the details of your user script projects, your problems, your ideas, and your JavaScript-related technological fantasies on this page. :) The Transhumanist01:10, 14 April 2017 (UTC)[reply]
I'm looking to build a userscript the interacts with a template call, and I was hoping that there's another one out there that I can see how this functionality works. Specifically, I want the userscript to do the following:
Scan a page on load for a particular template call.
Insert a button for openning up a javascript window if the template is found, preferably at the template's location on the page.
Load template parameters into fields in the new javascript window.
Does anyone know of a userscript that does something like this, or a set of userscripts that together do all of these functions? Am I completely crazy thinking this is even possible? It's been about 20 years since I last programmed anything in javascript, so I'm very much looking for a place to start connecting the dots again. Thanks for any help you have. VanIsaacWScont18:28, 24 June 2020 (UTC)[reply]
For looking for a template call within wikitext, and finding the parameters and their values, you can use the parseTemplates function of MediaWiki:Gadget-libExtraUtil.js (which is a gadget, so can be loading using ResourceLoader's mw.loader.using). Actually finding the parsed html of the template within the page's html is going to be more tricky, and may not be possible, e.g. if the template output is indistinguishable from regular wikitext, such as {{ndash}} or other formatting templates. - Evad37[talk]03:24, 25 June 2020 (UTC)[reply]
I've wanted a user script that finds and highlights occurrences of a given template for a while – extending such a user script to meet your specification wouldn't be that hard. Both scripts are completely feasible with Parsoid. However, from my past experience, I know they'll probably be pretty annoying/difficult to write, so I haven't written anything yet. I'd be happy to give advice or an outline of how to proceed to anyone who wants it, though. Enterprisey (talk!) 06:42, 25 June 2020 (UTC)[reply]
It's certainly fine to have this script parse wikitext only while editing if that makes things easier. But since the script is only useful interacting with a few specific templates, it's completely reasonable to just program the templates with custom HTML classes for the script data. I would assume that would vastly simplify extracting those values. VanIsaacWScont18:04, 25 June 2020 (UTC)[reply]
Anyone know how I can authenticate a request from a user script to a Toolforge tool? I'm not sure OAuth would work, since I figure that uses cookies (but I would be happy to be wrong about that). I could always have the user script make an edit or a log entry and pass the diff/log entry ID with the request, but that seems spammy. Enterprisey (talk!) 07:12, 3 July 2020 (UTC)[reply]
Evad37, so I was thinking that I would send a request from the user script to the tool, and the tool would then query rest.php/oauth2/resource/profile to see if the user is logged in. That's not the correct use of OAuth, I think (because I would be sending a token associated with the user to /profile, so the answer (barring token expiration) would always be "yes, the user's logged in"). I'm not sure how else to validate that a user is logged in without somehow making a request from the tool that carries the en.wp session cookies. Enterprisey (talk!) 08:30, 3 July 2020 (UTC)[reply]
A tool for watchlisting a section (a user can subscribe/unsubscribe to a section). I don't want to store subscription lists onwiki because sections can move, be merged, etc, which would require a bot to update them. Also, just as watchlists aren't public, section watchlists shouldn't be, either. Thus, I want the userscript to make authenticated requests to a tool that stores the subscription lists in a database. Enterprisey (talk!) 00:15, 4 July 2020 (UTC)[reply]
Enterprisey, do you mean editing on behalf of a user or just authenticating that the user is that user? For the latter, I've already had some ideas about this for RW - so the script would generate a random token on the user side (the private token), have that be passed to toolforge which will store the token and also hand back a public token string which will then be used to make a public edit on Wikipedia somewhere, be it a config file or authentication page (similar to the way WP:DISCORD authenticates users). Then, the script will give the private token and the revision ID which will then link the user who made that edit to the owner of both the public and private token. From then on, the requests would be authenticated by including the private token in the request which is only known to the server and the client - the private token would be stored in a cookie or document storage so it's never made public, and when it expires the process will just run again - but this isn't the most secure solution, but I'd say its more ideal over handing auth cookies. Doing this means that authentication would be far less often. Ed6767talk!18:09, 3 July 2020 (UTC)[reply]
Looks good, and thanks for the infographic! (Very high-effort!) I will probably end up trying this if I can't find an existing solution. Enterprisey (talk!) 01:31, 4 July 2020 (UTC)[reply]