Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  



























Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Identifying the type of page  



1.1  Preview pages  





1.2  Edit pages  





1.3  History pages  





1.4  Special pages  





1.5  Pages with history  





1.6  Editable pages  







2 Getting various parts of a page  
2 comments  


2.1  Getting the page title and namespace  





2.2  Getting the various toolbars (personal, tabs, sidebar)  







3 Inserting content  
2 comments  




4 Pressing buttons  





5 Altering existing interface links  





6 Onload Structure  





7 Include an external js-file on wikipedia  





8 AJAX  





9 Automatic edits  





10 JSON  





11 Update a script  





12 Edit a page on another Wikimedia wiki  





13 Timezone formatting  














Wikipedia:User scripts/Techniques







 

Edit links
 









Project page
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 


















From Wikipedia, the free encyclopedia
 

< Wikipedia:User scripts

This page will collect various techniques for achieving common tasks needed in writing user scripts. Discussion about limitations, relative portability, and speed of the various alternatives is strongly encouraged. There is a lot of duplication and non-optimal efforts out there, and this will hopefully encourage us to write tighter, more correct code, both easier and faster.

An advanced API for interacting with Wikipedia content is being developed, large parts of which are already operational. The various possibilities are described at mw:API. The idea is to send an AJAX request (see below) to the API containing a query. The result of this query can be returned in several formats, of which JSON is perhaps the most useful, see below.

Identifying the type of page[edit]

This refers to techniques for identifying the current namespace of the current page, whether or not it is an edit page, a preview page, a Special page, etc.

Preview pages[edit]

document.getElementById("wikiPreview")

Edit pages[edit]

document.getElementById("editform")

This will be null when not editing a page.

History pages[edit]

mw.config.get('wgAction') === 'history'

Special pages[edit]

mw.config.get('wgCanonicalNamespace') === 'Special'

Pages with history[edit]

document.getElementById('ca-history')

Editable pages[edit]

document.getElementById('ca-edit')

Be advised that this also returns the edit tab if you're currently editing the page.

Getting various parts of a page[edit]

Getting the page title and namespace[edit]

Getting the various toolbars (personal, tabs, sidebar)[edit]

var tabs = document.getElementById(BAR NAME).getElementsByTagName('ul')[0];

TODO: Someone please test the search and toolbox ones, and see if they work the same. Thanks!

The search box is 'p-search' but there's no <ul> element in it. [ælfəks] 10:38, 24 June 2006 (UTC)[reply]
The search box can be retrieved by simply replacing the 'ul'ingetElementsByTagName('ul') with 'div', as all the toolboxes' ids are in div tags. Extremecircuitz (Talk | Userboxes page) 20:17, 21 October 2007 (UTC)[reply]

Inserting content[edit]

document.getElementById("content").insertBefore(document.createTextNode("abcdef"), document.getElementsByTagName("h1")[0])
  • No, but does it help if you delay execution until the page has loaded? Lupin|talk|popups 12:14, 4 October 2005 (UTC)[reply]

Pressing buttons[edit]

document.editform.wpDiff.click()

Altering existing interface links[edit]

To change the url, name, or any other aspect of existing tab buttons, personal bar links, or other links, use the following: (where id is the id of the link to be changed, e.g. "pt-preferences", "ca-edit", "n-portal" or "t-whatlinkshere"; url is the new URL, and name is the new displayed name for the link, e.g. "my preferences", "edit this page", "Community Portal", or "What links here")

document.getElementById(id).childNodes[0].href=url
q=document.getElementById(id).firstChild; q.removeChild(q.firstChild); q.appendChild(document.createTextNode(name)) 

Onload Structure[edit]

jQuery can attach functions to the onLoad event:

$( myFunction );

Functions can also be written inline as

$( function() {
    // Code here
} );

Do not assign window.onload to a function directly, as this overwrites any other onLoad functions that may have been previously set.

Include an external js-file on wikipedia[edit]

mw.loader.load is a loader method to load external javascript or css:

mw.loader.load( 'http://meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&action=raw&ctype=text/javascript', 'text/javascript' );
mw.loader.load( 'http://example.org/mystyles.css', 'text/css' );
mw.loader.load( 'http://example.org/mystyles.js', 'text/javascript' );

AJAX[edit]

See Wikipedia:WikiProject User scripts/Guide#Ajax
$.getScript('http://example.org/foo.js', function () {
 // Foo.js is loaded!
} )

Automatic edits[edit]

On classic edit pages you can find the textbox with the wikitext like this:

var t = document.editform.wpTextbox1;

Then use the methods of the textSelection plugin to interact with the textarea or edit summary. This module makes sure that your modification works in combination with other modules that want to manipulate the value of the textarea, like syntax highlighting modules.

JSON[edit]

Parsing JSON text, as delivered by e.g. the MediaWiki API is done automatically when using jQuery utilities:

jQuery.getJSON(
  mw.util.wikiScript( 'api' ), {
    'format': 'json',
    'action': 'query',
    'meta': 'userinfo'
  }, function ( data ) {
    // data.query.userinfo
  }
);

Update a script[edit]

Scripts on a user's computer are updated to the most recent version by bypassing (clearing) the browser cache - the user has to push Shift-Reload (Mozilla) or Shift-F5 (MS-IE). A JavaScript can do the same by calling:

window.location.reload(true);

This forced reload ("forceGet") immediately reloads the current page including all images, scripts, and stylesheets. This should not be done from edit or preview pages as the edits might get lost.

For users that have a lot of scripts installed, reloading them all may take up a lot of time. See Gerbrant.mng.decache and its talk page for example code on how you can let JavaScript remove arbitrary files from your browser cache using an external application.

Edit a page on another Wikimedia wiki[edit]

Although not commonly used, CORS is enabled between all Wikimedia wikis. For an example of cross-wiki editing, see here.

Timezone formatting[edit]

The selected timezone of a user is available via mw.user.options.get('timecorrection'), which will return something like "ZoneInfo|180|Africa/Addis_Ababa", where 180 is the number of minutes to add to UTC to obtain a time in the user's preferred time zone. The number might be negative.


Retrieved from "https://en.wikipedia.org/w/index.php?title=Wikipedia:User_scripts/Techniques&oldid=1212112666"

Category: 
Wikipedia scripts
 



This page was last edited on 6 March 2024, at 06:30 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki