<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.tachyony.co.uk/w/index.php?action=history&amp;feed=atom&amp;title=Wikipedia%3AUser_scripts%2FTechniques</id>
	<title>Wikipedia:User scripts/Techniques - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.tachyony.co.uk/w/index.php?action=history&amp;feed=atom&amp;title=Wikipedia%3AUser_scripts%2FTechniques"/>
	<link rel="alternate" type="text/html" href="https://wiki.tachyony.co.uk/w/index.php?title=Wikipedia:User_scripts/Techniques&amp;action=history"/>
	<updated>2026-05-15T19:05:06Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.5</generator>
	<entry>
		<id>https://wiki.tachyony.co.uk/w/index.php?title=Wikipedia:User_scripts/Techniques&amp;diff=1882&amp;oldid=prev</id>
		<title>Tachyony: Imported page</title>
		<link rel="alternate" type="text/html" href="https://wiki.tachyony.co.uk/w/index.php?title=Wikipedia:User_scripts/Techniques&amp;diff=1882&amp;oldid=prev"/>
		<updated>2021-12-06T12:44:37Z</updated>

		<summary type="html">&lt;p&gt;Imported page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Wikipedia:User scripts/Navbox}}&lt;br /&gt;
{{Shortcut|WP:US/T}}&lt;br /&gt;
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 &amp;#039;&amp;#039;&amp;#039;strongly&amp;#039;&amp;#039;&amp;#039; 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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Identifying the type of page==&lt;br /&gt;
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.&lt;br /&gt;
===Preview pages===&lt;br /&gt;
 document.getElementById(&amp;quot;wikiPreview&amp;quot;)&lt;br /&gt;
*From: [http://en.wikipedia.org/w/index.php?title=Wikipedia:WikiProject_User_scripts/Scripts/Add_Edit_Top_Link&amp;amp;oldid=22052848 Add Edit Top Link]&lt;br /&gt;
&lt;br /&gt;
===Edit pages===&lt;br /&gt;
 document.getElementById(&amp;quot;editform&amp;quot;)&lt;br /&gt;
This will be null when not editing a page.&lt;br /&gt;
&lt;br /&gt;
===History pages===&lt;br /&gt;
 mw.config.get(&amp;#039;wgAction&amp;#039;) === &amp;#039;history&amp;#039;&lt;br /&gt;
&lt;br /&gt;
===Special pages===&lt;br /&gt;
 mw.config.get(&amp;#039;wgCanonicalNamespace&amp;#039;) === &amp;#039;Special&amp;#039;&lt;br /&gt;
&lt;br /&gt;
===Pages with history===&lt;br /&gt;
 document.getElementById(&amp;#039;ca-history&amp;#039;)&lt;br /&gt;
*From: [http://en.wikipedia.org/w/index.php?title=Wikipedia:WikiProject_User_scripts/Scripts/Add_purge_to_tabs&amp;amp;oldid=113178096 Add purge to tabs]&lt;br /&gt;
&lt;br /&gt;
===Editable pages===&lt;br /&gt;
 document.getElementById(&amp;#039;ca-edit&amp;#039;)&lt;br /&gt;
Be advised that this also returns the edit tab if you&amp;#039;re currently editing the page.&lt;br /&gt;
*From: [http://en.wikipedia.org/w/index.php?title=Wikipedia:WikiProject_User_scripts/Scripts/Add_edit_section_0&amp;amp;oldid=21025437 Add edit section 0]&lt;br /&gt;
&lt;br /&gt;
==Getting various parts of a page==&lt;br /&gt;
===Getting the page title and namespace===&lt;br /&gt;
*&amp;lt;code&amp;gt;mw.config.get(&amp;#039;wgCanonicalNamespace&amp;#039;)&amp;lt;/code&amp;gt; contains the name of the namespace, e.g. &amp;quot;&amp;quot; for article space&lt;br /&gt;
*&amp;lt;code&amp;gt;mw.config.get(&amp;#039;wgCanonicalSpecialPageName&amp;#039;)&amp;lt;/code&amp;gt; is normally false but for special pages it contains the non-localized name.&lt;br /&gt;
*&amp;lt;code&amp;gt;mw.config.get(&amp;#039;wgNamespaceNumber&amp;#039;)&amp;lt;/code&amp;gt; is 0 for the main namespace, -1 for special pages, and other values for other namespaces.&lt;br /&gt;
*&amp;lt;code&amp;gt;mw.config.get(&amp;#039;wgPageName&amp;#039;)&amp;lt;/code&amp;gt; is the article name as it is shown on the url, e.g. &amp;quot;Wikipedia:User_scripts/Techniques&amp;quot;. It includes the namespace identifier.&lt;br /&gt;
*&amp;lt;code&amp;gt;mw.config.get(&amp;#039;wgTitle&amp;#039;)&amp;lt;/code&amp;gt; is the title of the article, without namespace identifier or escaping, e.g. &amp;quot;User scripts/Techniques&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Getting the various toolbars (personal, tabs, sidebar)===&lt;br /&gt;
 var tabs = document.getElementById(&amp;#039;&amp;#039;&amp;#039;BAR NAME&amp;#039;&amp;#039;&amp;#039;).getElementsByTagName(&amp;#039;ul&amp;#039;)[0];&lt;br /&gt;
*Where &amp;#039;&amp;#039;&amp;#039;BAR NAME&amp;#039;&amp;#039;&amp;#039; is one of the following strings:&lt;br /&gt;
*&amp;#039;p-cactions&amp;#039; - the tabs at the top of the page (with the article, discussion, edit, history, move, and watch links)&lt;br /&gt;
*&amp;#039;p-personal&amp;#039; - the personal toolbar (i.e. the one at the top, with a link to user page, user talk, prefs, watchlist, contribs, log out)&lt;br /&gt;
*&amp;#039;p-navigation&amp;#039; - the navigation toolbar (i.e. Main page, Featured Content, etc.)&lt;br /&gt;
*&amp;#039;p-interaction&amp;#039; - the interaction toolbar, below the navigation toolbar&lt;br /&gt;
*&amp;#039;p-tb&amp;#039; - the toolbox (What links here, Related changes etc.)&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;TODO&amp;#039;&amp;#039;&amp;#039;: Someone please test the search and toolbox ones, and see if they work the same. Thanks!&lt;br /&gt;
:The search box is &amp;#039;p-search&amp;#039; but there&amp;#039;s no &amp;amp;lt;ul&amp;amp;gt; element in it. &amp;lt;span class=&amp;quot;ipa&amp;quot;&amp;gt;[[User:Alphax|[ælfəks]]]&amp;lt;/span&amp;gt; 10:38, 24 June 2006 (UTC)&lt;br /&gt;
::The search box can be retrieved by simply replacing the &amp;lt;code&amp;gt;&amp;#039;ul&amp;#039;&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;getElementsByTagName(&amp;#039;ul&amp;#039;)&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;&amp;#039;div&amp;#039;&amp;lt;/code&amp;gt;, as all the toolboxes&amp;#039; ids are in div tags. [[User:Extremecircuitz|&amp;lt;font color=&amp;quot;#009900&amp;quot;&amp;gt;Extreme&amp;lt;/font&amp;gt;&amp;lt;font color=&amp;quot;#330066&amp;quot;&amp;gt;circuitz&amp;lt;/font&amp;gt;]] ([[User_talk:Extremecircuitz|Talk]] | [[User:Extremecircuitz/more_userboxes|Userboxes page]]) 20:17, 21 October 2007 (UTC)&lt;br /&gt;
&lt;br /&gt;
==Inserting content==&lt;br /&gt;
 document.getElementById(&amp;quot;content&amp;quot;).insertBefore(document.createTextNode(&amp;quot;abcdef&amp;quot;), document.getElementsByTagName(&amp;quot;h1&amp;quot;)[0])&lt;br /&gt;
*On a page with a h1 heading, this works in Firefox 1.0.4 on OSX, but fails on some other browsers.  Anyone know how or why? [[User:JesseW|JesseW]] 20:58, 29 August 2005 (UTC)&lt;br /&gt;
:*No, but does it help if you delay execution until the page has loaded? [[User:Lupin|&amp;#039;&amp;#039;&amp;#039;Lupin&amp;#039;&amp;#039;&amp;#039;]]|[[User_talk:Lupin|talk]]|[[Wikipedia:Tools/Navigation popups|popups]] 12:14, 4 October 2005 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Pressing buttons ==&lt;br /&gt;
&lt;br /&gt;
 document.editform.wpDiff.click()&lt;br /&gt;
&lt;br /&gt;
* Presses the diff button.&lt;br /&gt;
&lt;br /&gt;
==Altering existing interface links==&lt;br /&gt;
To change the url, name, or any other aspect of existing tab buttons, personal bar links, or other links, use the following: (where &amp;#039;&amp;#039;&amp;#039;id&amp;#039;&amp;#039;&amp;#039; is the id of the link to be changed, e.g. &amp;quot;pt-preferences&amp;quot;, &amp;quot;ca-edit&amp;quot;, &amp;quot;n-portal&amp;quot; or &amp;quot;t-whatlinkshere&amp;quot;; &amp;#039;&amp;#039;&amp;#039;url&amp;#039;&amp;#039;&amp;#039; is the new URL, and &amp;#039;&amp;#039;&amp;#039;name&amp;#039;&amp;#039;&amp;#039; is the new displayed name for the link, e.g. &amp;quot;my preferences&amp;quot;, &amp;quot;edit this page&amp;quot;, &amp;quot;Community Portal&amp;quot;, or &amp;quot;What links here&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
 document.getElementById(&amp;#039;&amp;#039;&amp;#039;id&amp;#039;&amp;#039;&amp;#039;).childNodes[0].href=&amp;#039;&amp;#039;&amp;#039;url&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 q=document.getElementById(&amp;#039;&amp;#039;&amp;#039;id&amp;#039;&amp;#039;&amp;#039;).firstChild; q.removeChild(q.firstChild); q.appendChild(document.createTextNode(&amp;#039;&amp;#039;&amp;#039;name&amp;#039;&amp;#039;&amp;#039;)) &lt;br /&gt;
&lt;br /&gt;
==Onload Structure==&lt;br /&gt;
jQuery can attach functions to the &amp;lt;tt&amp;gt;onLoad&amp;lt;/tt&amp;gt; event:&lt;br /&gt;
&lt;br /&gt;
 $( myFunction );&lt;br /&gt;
&lt;br /&gt;
Functions can also be written inline as&lt;br /&gt;
 $( function() {&lt;br /&gt;
     // Code here&lt;br /&gt;
 } );&lt;br /&gt;
&lt;br /&gt;
Do not assign &amp;lt;tt&amp;gt;window.onload&amp;lt;/tt&amp;gt; to a function directly, as this overwrites any other &amp;lt;tt&amp;gt;onLoad&amp;lt;/tt&amp;gt; functions that may have been previously set.&lt;br /&gt;
&lt;br /&gt;
== Include an external js-file on wikipedia ==&lt;br /&gt;
[[mw:RL/DM#mediaWiki.loader|mw.loader.load]] is a loader method to load external javascript or css:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
mw.loader.load( &amp;#039;http://meta.wikimedia.org/w/index.php?title=MediaWiki:Wikiminiatlas.js&amp;amp;action=raw&amp;amp;ctype=text/javascript&amp;#039;, &amp;#039;text/javascript&amp;#039; );&lt;br /&gt;
mw.loader.load( &amp;#039;http://example.org/mystyles.css&amp;#039;, &amp;#039;text/css&amp;#039; );&lt;br /&gt;
mw.loader.load( &amp;#039;http://example.org/mystyles.js&amp;#039;, &amp;#039;text/javascript&amp;#039; );&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== AJAX ==&lt;br /&gt;
: &amp;#039;&amp;#039;See [[Wikipedia:WikiProject User scripts/Guide#Ajax]]&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
$.getScript(&amp;#039;http://example.org/foo.js&amp;#039;, function () {&lt;br /&gt;
 // Foo.js is loaded!&lt;br /&gt;
} )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Automatic edits==&lt;br /&gt;
On classic edit pages you can find the textbox with the wikitext like this:&lt;br /&gt;
 var t = document.editform.wpTextbox1;&lt;br /&gt;
Then use the methods of the [https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/jQuery.plugin.textSelection 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.&lt;br /&gt;
&lt;br /&gt;
==JSON==&lt;br /&gt;
Parsing JSON text, as delivered by e.g. the MediaWiki API is done automatically when using jQuery utilities:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
jQuery.getJSON(&lt;br /&gt;
  mw.util.wikiScript( &amp;#039;api&amp;#039; ), {&lt;br /&gt;
    &amp;#039;format&amp;#039;: &amp;#039;json&amp;#039;,&lt;br /&gt;
    &amp;#039;action&amp;#039;: &amp;#039;query&amp;#039;,&lt;br /&gt;
    &amp;#039;meta&amp;#039;: &amp;#039;userinfo&amp;#039;&lt;br /&gt;
  }, function ( data ) {&lt;br /&gt;
    // data.query.userinfo&lt;br /&gt;
  }&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Update a script ==&lt;br /&gt;
&lt;br /&gt;
Scripts on a user&amp;#039;s computer are updated to the most recent version by bypassing (clearing) the browser cache - the user has to push &amp;#039;&amp;#039;Shift-Reload&amp;#039;&amp;#039; (Mozilla) or &amp;#039;&amp;#039;Shift-F5&amp;#039;&amp;#039; (MS-IE). A JavaScript can do the same by calling:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
window.location.reload(true);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
This &amp;#039;&amp;#039;forced reload&amp;#039;&amp;#039; (&amp;quot;forceGet&amp;quot;) 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.&lt;br /&gt;
&lt;br /&gt;
For users that have a lot of scripts installed, reloading them all may take up a lot of time. See [[User:Gerbrant/mng/decache.js|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.&lt;br /&gt;
&lt;br /&gt;
== Edit a page on another Wikimedia wiki ==&lt;br /&gt;
&lt;br /&gt;
Although not commonly used, CORS is enabled between all Wikimedia wikis. For an example of cross-wiki editing, see [[mw:User:PiRSquared17/CORS_editing_example.js|here]].&lt;br /&gt;
&lt;br /&gt;
== Timezone formatting ==&lt;br /&gt;
&lt;br /&gt;
The selected timezone of a user is available via &amp;lt;code&amp;gt;mw.user.options.get(&amp;#039;timecorrection&amp;#039;)&amp;lt;/code&amp;gt;, which will return something like &amp;lt;code&amp;gt;&amp;quot;ZoneInfo|180|Africa/Addis_Ababa&amp;quot;&amp;lt;/code&amp;gt;, where 180 is the number of minutes to add to UTC to obtain a time in the user&amp;#039;s preferred time zone. The number might be negative.&lt;br /&gt;
&lt;br /&gt;
{{Wikipedia technical help|collapsed}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Wikipedia scripts]]&lt;/div&gt;</summary>
		<author><name>Tachyony</name></author>
	</entry>
</feed>