විකිපීඩියා:User scripts/TechniquesThis 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 pageThis 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 pagesdocument.getElementById("wikiPreview")
Edit pagesdocument.getElementById("editform") This will be null when not editing a page. History pagesmw.config.get('wgAction') === 'history' Special pagesmw.config.get('wgCanonicalNamespace') === 'Special' Pages with historydocument.getElementById('ca-history')
Editable pagesdocument.getElementById('ca-edit') Be advised that this also returns the edit tab if you're currently editing the page.
Getting various parts of a pageGetting the page title and namespace
Getting the various toolbars (personal, tabs, sidebar)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!
Inserting contentdocument.getElementById("content").insertBefore(document.createTextNode("abcdef"), document.getElementsByTagName("h1")[0])
Pressing buttonsdocument.editform.wpDiff.click()
Altering existing interface linksTo 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 StructurejQuery 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 wikipediamw.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$.getScript('http://example.org/foo.js', function () {
// Foo.js is loaded!
} )
Automatic editsOn 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. JSONParsing 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 scriptScripts 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 wikiAlthough not commonly used, CORS is enabled between all Wikimedia wikis. For an example of cross-wiki editing, see here. Timezone formattingThe selected timezone of a user is available via |
Portal di Ensiklopedia Dunia