Wikipedia:WikiProject User scripts/Techniques
From Wikipedia, the free encyclopedia
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.
Contents |
[edit] Identifying the type of page
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.
[edit] Preview pages
document.getElementById("wikiPreview")
- From: Add Edit Top Link
[edit] Edit pages
document.getElementById('ca-edit').className == 'selected'
- Based on: Add edit section 0
- monobook-only
document.title.indexOf("Editing ") == 0
- From: Quick wikify
/[?&]action=edit/.test(window.location.href)
- Based on: Force edit summary
- This may fail when Wikipedia is under heavy load and you hit Save page but get a preview instead. Then the url contains action=submit.
document.editform
- Should work regardless of skin and language
[edit] History pages
window.location.href.indexOf("&action=history") != -1
- Based on: Changes since I last edited
[edit] Special pages
window.location.href.indexOf("Special:") != -1
- From: Add Edit Top Link
!/wiki\/Special:|w\/index.php?title=Special:/.test(window.location.href)
[edit] Pages with history
document.getElementById('ca-history')
- From: Add purge to tabs
[edit] Editable pages
document.getElementById('ca-edit')
- From: Add edit section 0
[edit] Getting various parts of a page
[edit] Getting the page title
See Wikipedia:WikiProject User scripts/Scripts/Get Page Name and Wikipedia:WikiProject User scripts/Scripts/Get tidy title.
var pagetitleRe=/[^:]*:\/\/en\.wikipedia\.org\/(wiki\/|w\/index\.php\?title=)([^&?]*)/; pagetitleRe.exec(decodeURI(location.href))[2].split('_').join(' ');
- This tries to get the article title from the URL
document.getElementsByTagName('h1')[0].firstChild.nodeValue
- This grabs the title at the top of the page (for example, "Editing Wikipedia:WikiProject User scripts/Techniques (section)").
[edit] Getting the various toolbars (personal, tabs, sidebar)
var tabs = document.getElementById(BAR NAME).getElementsByTagName('ul')[0];
- Where BAR NAME is one of the following strings:
- 'p-cactions'
- the tabs at the top of the page (with the article, discussion, edit, history, move, and watch links)
- 'p-personal'
- the personal toolbar (i.e. the one at the top, with a link to user page, user talk, prefs, watchlist, contribs, log out)
- 'p-navigation'
- the navigation toolbar (i.e. Main page, Community Portal, etc.)
- 'p-tb'
- the toolbox (What links here, Related changes etc.)
- This gets the toolbar in a form that can be the first arg to Wikipedia:WikiProject User scripts/Scripts/Add LI link
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)
[edit] Inserting content
document.getElementById("content").insertBefore(document.createTextNode("abcdef"), document.getElementsByTagName("h1")[0])
- 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? JesseW 20:58, 29 August 2005 (UTC)
[edit] Pressing buttons
document.editform.wpDiff.click()
- Presses the diff button.
[edit] Accesskeys and link titles
Things to add, modify and remove accesskeys and link titles for list items with an id tag.
[edit] Add accesskey and title
ta['ca-purge'] = ['g', 'Purge the internal cache for this page'];
- From: Add purge to tabs
[edit] Disabling accesskeys and titles
ta['pt-logout'] = null;
[edit] Update accesskeys and titles
akeytt();
- From: Add purge to tabs
[edit] Altering existing interface links
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))
[edit] Onload Structure
wikibits.js contains a function called addOnloadHook() that attaches functions to the onLoad event:
addOnloadHook( myFunction );
Functions can also be written inline as
addOnloadHook( 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.
Note: This technique can cause an error message in some situations. If you need the whole page to be loaded when your hook executes, try:
hookEvent("load", myHookFunction);
[edit] Include an external js-file on wikipedia
function import_external(page) { document.write('<script type="text/javascript" src="' + 'http://en.wikipedia.org/w/index.php?title=' + page + '&action=raw&ctype=text/javascript&dontcountme=s"></script>'); } import_external('User:Ilmari Karonen/godmode-light.js');
[edit] AJAX
If you want to read the content from a webpage from JavaScript, and have AJAX fun, use the predefined function sajax_init_object
:
javascript:void(a=sajax_init_object(),a.open("GET","http://enter.the.name.of.a.website.here",true),a.onreadystatechange=function(){if(a.readyState!=4)return;alert("["+a.status+":"+a.statusText+")"+a.responseText);},a.send())
You can copy the above code to the address bar of your browser to test it.
The same in a more friendly layout, like you would use in a script file:
a=sajax_init_object();
a.open("GET", "http://enter.the.name.of.a.website.here", true);
a.onreadystatechange = function()
{
if(a.readyState != 4) return;
alert("[" + a.status + ":" + a.statusText + ")" + a.responseText);
};
a.send();