Wikipedia talk:User scripts

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

User scripts clash, dark mode not working, disruption of toolbars, editing interfaces[edit]

I have many user scripts installed in my common.js page, which seem to be clashing with each other. At the top of every page, some of the disrupting elements seem to be parts of RedWarn, but they are still appearing again and again even though I have uninstalled RedWarn from my common.js.


I don't know what's causing Volker E. (WMF)'s dark-mode.js to dysfunction. For some reason, the dark mode's toggle button appears 4 times on the top of every page, and one of the button says "light mode", while the rest say "dark mode".

Also, in desktop mode, the source editor initially does not show any article content, instead just displays "the below text may contain spoilers" with a [show] button at the right side.

What could have caused RedWarn's parts to appear again?

Can anyone help me to fix the dark mode at least? Other clashing scripts are more annoying, but I really need dark mode for visual contrast. — CrafterNova [ TALK ] [ CONT ] 12:56, 22 January 2024 (UTC)[reply]

You're importing the script twice (one with importScript, the other mw.loader.load) and then forking the script on top of that. I suggest you remove all that (lines 65–89), and enable the dark mode toggle gadget in Preferences if you haven't done so. Nardog (talk) 13:08, 22 January 2024 (UTC)[reply]
@Nardog: After doing that, dark mode works when a page is loading, but reverts to light mode after the page has loaded. — CrafterNova [ TALK ] [ CONT ] 16:18, 22 January 2024 (UTC)[reply]
Maybe because you're also loading and forking it in your global.js? You really need to stop putting things you don't understand in your .js, they can steal information from you or make any edit on your behalf, hence all the warnings. Nardog (talk) 00:09, 23 January 2024 (UTC)[reply]
The spoilers thing is coming from User:Anomie/hidespoilers, which is a script seemingly written as a joke or at best for demonstration way back in 2009 and currently imported by no one but you. I wonder how the hell you found it. Nardog (talk) 13:19, 22 January 2024 (UTC)[reply]
Weird, I don't remember installing that one. — CrafterNova [ TALK ] [ CONT ] 14:31, 22 January 2024 (UTC)[reply]
@CrafterNova: Two years ago. --Redrose64 🌹 (talk) 17:38, 22 January 2024 (UTC)[reply]

Preview edits by a userscript[edit]

A userscript can automatically generate and publish an edit using mw.Api.postWithEditToken(...). This is possible to do from any Wikipedia page to any remote Wikipedia page.

Portlet links which use WP:TemplateScript allow to generate and preview an edit for a page, currently opened in the wikitext editor.

Is there any way for a userscript to show a preview for an automatic edit for a remote page? My particular use case is generating an edit to another user's talk page from their contributions page. That is, a portlet link on a page like Special:Contributions/Example should load a preview with an edit of User talk:Example. —⁠andrybak (talk) 15:40, 10 February 2024 (UTC)[reply]

Use parse API. Here's how Twinkle does it. If you want to be thorough, you can use MW's native live preview API. Nardog (talk) 19:49, 10 February 2024 (UTC)[reply]
Nardog, thank you for the pointer, I'll look into it. Is there anything that allows opening the native editor on the remote page? Similar to how preload templates work? —⁠andrybak (talk) 19:56, 10 February 2024 (UTC)[reply]
You mean you want to open the remote page with certain wikitext instead of the current content? If so you can attach a form element and submit it, e.g.
$('<form>').attr({
	method: 'post',
	action: mw.util.getUrl('Wikipedia:Sandbox', { action: 'edit' }),
	enctype: 'multipart/form-data'
}).append(
	$('<input>').attr({
		name: 'wpTextbox1',
		type: 'hidden'
	}).val('foo')
).appendTo(document.body).trigger('submit').remove();
(There doesn't seem to be a way to suppress the preview.) Nardog (talk) 20:50, 10 February 2024 (UTC)[reply]
Come to think of it, a simpler way is to just store the text in e.g. sessionStorage, open the page the normal way, and modify the textbox (use jquery.textSelection). Nardog (talk) 20:59, 10 February 2024 (UTC)[reply]
Re: There doesn't seem to be a way to suppress the preview. – here are tweaked fields of the form to mimic "Show changes" instead of "Show preview":
/* the data of the edit */
$('<input>').attr({
	name: 'wpTextbox1',
	type: 'hidden'
}).val(newWikitext),
$('<input>').attr({
	name: 'wpSummary',
	type: 'hidden'
}).val(editSummary),

/* show as a diff */
$('<input>').attr({
	name: 'wpDiff',
	type: 'hidden'
}).val(true),

/* wpUltimateParam is needed to avoid the scary error
 * "Some parts of the edit form did not reach the server" */
$('<input>').attr({
	name: 'wpUltimateParam',
	type: 'hidden'
}).val(1)
Just in case anyone else will find it useful. —⁠andrybak (talk) 21:46, 10 February 2024 (UTC)[reply]

CSS items[edit]

@Nardog I mean, that's what User:BrandonXLF/GreenRedirects is. I don't see why it'd be a worse idea than JS, which can actually compromise one's browser. Aaron Liu (talk) 02:54, 21 February 2024 (UTC)[reply]

It's not. common.css and global.css are loaded automatically for the owners and they can put or remove whatever they want there at any time. If you intend some styles to be imported by other users, you should host it as a separate page. Nardog (talk) 03:01, 21 February 2024 (UTC)[reply]
Also, importing an external CSS via JS causes FOUCs. Using @import doesn't, but can delay first paint. Nardog (talk) 06:12, 21 February 2024 (UTC)[reply]
Speaking about that... my @import doesn't seem to be working right now :p Aaron Liu (talk) 16:07, 21 February 2024 (UTC) Fixed that now! Aaron Liu (talk) 03:05, 22 February 2024 (UTC)[reply]

Highlight username[edit]

Hey, do we have a script which can highlight your own username in a page? I remember seeing something similar a while ago but can't find that now. Can sormeone link it please? Thanks. — The Herald (Benison) (talk) 05:35, 25 February 2024 (UTC)[reply]

@The Herald: Not sure if there's a script, but a userscript wouldn't be super necessary; you could just put this in Special:MyPage/common.css:
a[href$="User:The_Herald"] { background-color:#FFFF00; }
Change the username in quotes to change which page gets highlighted, and change the "FFFF00" to any other hex color code to change the color of the highlight.
EDIT:After rereading, I think I misunderstood your ask; the above will highlight any links to your user page (as in your signature or any pings to you), but not any mere use of your username. That *would* require a script. Writ Keeper  13:45, 25 February 2024 (UTC)[reply]
I think I'll give it a ago. I just want my name to get highlighted, especially in AfD discussions, just to avoid double !votes or avoid WP:INVOLVED when I'm closing discussions. Hope it will work for that. Thanks. The Herald (Benison) (talk) 14:06, 25 February 2024 (UTC)[reply]
Update: It works just how I wanted. Thanks a lot. The Herald (Benison) (talk) 14:09, 25 February 2024 (UTC)[reply]