User:GrafZahl/monobook.js
Jump to navigation
Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
- Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Clear the cache in Tools → Preferences
For details and instructions about other browsers, see Wikipedia:Bypass your cache.
Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the central discussion page, Scriptorium. The code will be executed when previewing this page under some skins, including Monobook. You can in the interim if you wish to refresh the content sooner under another skin. |
The accompanying .css page for this skin can be added at User:GrafZahl/monobook.css. |
/*
monobook.js
Copyright (C) 2006, 2007 GrafZahl (en.wikisource.org user)
Licence: GPLv2
==ToggleText==
Displayed text toggle, e.g. for applying errata. Similar to ThomasV's
OptionText (which displays or doesn't display a certain text).
Uses ideas from [[User:ThomasV|ThomasV]].
===Description===
Creates an additional menu (similar to the navigation and toolbox menus) with
links which, when pressed, will hide some portion of the text and display
another instead of it. When pressed again, the old state is restored. More
generally, the style with which both texts are displayed, are swapped, and
then restored.
The link names are specified by the user through specially crafted
<code><nowiki><span></nowiki></code> tags. For example,
<pre>
<span class="ToggleText" title="toggle errata">
</pre>
will create a link called "toggle errata". The text to be toggled is specified
within subtags of the <code><nowiki><span></nowiki></code> tag. For example,
<pre>
<span class="ToggleText" title="toggle errata">
<span id="original">
lorem ipsum
</span>
<span id="replacement" style="display:none">
lorem istud
</span>
</span>
</pre>
will replace the text "lorem ipsum" with "lorem istud" and back again when
the "toggle errata" link is pressed.
<pre>
*/
function ToggleText()
{
// find all ToggleText requests
var spanElts = document.getElementsByTagName('span');
var usedTitles = new Object();
var ttTitles = new Array();
var ttOrig = new Array();
var ttRepl = new Array();
var ttState = new Array();
var spanElt;
// store title, original style and replacement style, and current state
for(var i = 0; spanElt = spanElts[i]; ++i)
if(spanElt.className == 'ToggleText')
{
// need exactly 2 direct subspans for orig/repl
var subSpans = spanElt.getElementsByTagName('span');
var spanOrig = subSpans[0];
var spanRepl = spanOrig;
while(spanRepl = spanRepl.nextSibling)
if(spanRepl.tagName == 'SPAN')
break;
if(!spanRepl)
continue;
// skip already handled elements
if(usedTitles[spanElt.title] == true)
continue;
usedTitles[spanElt.title] = true;
ttTitles.push(spanElt.title);
ttOrig.push(spanOrig.getAttribute('style'));
ttRepl.push(spanRepl.getAttribute('style'));
var cookie = GetCookie('Toggle' + spanElt.title);
if((cookie) &&
(cookie == 'true'))
ttState.push(true);
else
ttState.push(false);
}
// exit if no requests
if(ttTitles.length == 0)
return;
// generate menu
var menu = document.createElement('div');
menu.style.marginTop = '0.7em';
menu.innerHTML = '<h5>display toggle</h5>'
+ '<div class="pBody"><ul id="togglelist">'
+ '</ul></div>';
document.getElementById('p-tb').appendChild(menu);
var list = document.getElementById('togglelist');
// add menu entries
for(var i = 0; i != ttTitles.length; ++i)
{
var anchor = document.createElement('a');
anchor.appendChild(document.createTextNode(ttTitles[i]));
anchor.setAttribute('href', 'javascript:displayToggleText("'
+ ttTitles[i] + '","' + ttOrig[i] + '","'
+ ttRepl[i] + '","' + ttState[i] + '");');
var link = document.createElement('li');
link.appendChild(anchor);
link.setAttribute('id', ttTitles[i]);
list.appendChild(link);
// create well-defined initial state
displayToggleText(ttTitles[i], ttOrig[i], ttRepl[i], ttState[i]);
}
}
function displayToggleText(title, origStyle, replStyle, showRepl)
{
// prepare variables
var link = document.getElementById(title).firstChild; // menu entry
var firstStyle;
var secondStyle;
if(showRepl)
{
link.firstChild.data = 'disable ' + title;
link.setAttribute('href', 'javascript:displayToggleText("'
+ title + '","' + origStyle + '","' + replStyle
+ '", false);');
firstStyle = replStyle;
secondStyle = origStyle;
}
else
{
link.firstChild.data = 'enable ' + title;
link.setAttribute('href', 'javascript:displayToggleText("'
+ title + '","' + origStyle + '","' + replStyle
+ '", true);');
firstStyle = origStyle;
secondStyle = replStyle;
}
// update cookie
SetCookie('Toggle' + title, showRepl);
// alter styles
var spanElt;
for(var i = 0; spanElt = document.getElementsByTagName('span')[i]; ++i)
{
// Handle appropriate elements only
if((spanElt.className != 'ToggleText') ||
(spanElt.title != title))
continue;
var subSpans = spanElt.getElementsByTagName('span');
if(subSpans.length != 2)
continue;
subSpans[0].setAttribute('style', firstStyle);
subSpans[1].setAttribute('style', secondStyle);
}
}
$(ToggleText);
/* Set Footer test */
function SetFooter()
{
/* check whether not to display the footer based on wikimarkup length
* and template/parser function use.
*
* The default is to display the footer.
*/
if(!window.XMLHttpRequest)
{
/* Indiscriminately display footer if AJAX is not available */
DisplayFooter();
return;
}
footerlimit = 1764;
ajax = new XMLHttpRequest();
ajax.onreadystatechange = function()
{
try
{
if(ajax.readyState != 4)
return;
if(ajax.status != 200) /* Trouble, display without checking */
throw "Boo!";
xml = ajax.responseXML;
wikitext = xml.getElementsByTagName("rev")[0].firstChild.data;
if(wikitext.length < footerlimit)
return; /* don't display footer */
throw "Boo!";
}
catch(er)
{
DisplayFooter();
return;
}
}
page = wgScriptPath + '/api.php?action=query&titles=' + wgPageName
+ '&prop=revisions&rvprop=content&rvlimit=1&format=xml';
ajax.open('GET', page, true);
ajax.send(null);
}
/* addOnloadHook(SetFooter); */
/*
</pre>
==OtherVersions==
Allows the user to add links to other versions of the same work (a different
translation, for example) below the "in other languages" box on the left.
Uses ideas employed by [[User:ThomasV|ThomasV]] in the [[:fr:|French Wikisource]].
===Description===
The user places the template {{[[Template:otherversion|otherversion]]}} in a text. This
template takes two parameters, the wikilink and the link text. So, where
you would write <code><nowiki>[[Work title (translated by Jane Doe)|J. Doe
translation]]</nowiki></code> if your intention were to produce a normal
wikilink, you now write <code><nowiki>{{otherversion|Work title (translated by
Jane Doe)|J. Doe translation}}</nowiki></code>.
<pre>
*/
function OtherVersions()
{
// find all other versions
var spanElts = document.getElementsByTagName('span');
var ov = new Array();
var spanElt;
for(var i = 0; spanElt = spanElts[i]; ++i)
if(spanElt.className == 'OtherVersions')
ov.push(spanElt.id);
if(ov.length == 0)
return;
// build link list
var listDiv = document.createElement('div');
listDiv.style.marginTop = '0.7em';
listDiv.innerHTML = '<h5>other versions</h5>';
var pBody = document.createElement('div');
pBody.className = 'pBody';
var list = document.createElement('ul');
list.id = 'ovlist';
var ovText;
var displayBox = false;
while(ovText = ov.shift())
{
var brack = document.getElementById(ovText).title;
// find link/text separator
var pipeIdx = brack.indexOf('|');
if(pipeIdx == -1)
continue;
displayBox = true;
// create anchor
var wikilink = brack.slice(0, pipeIdx);
var wikitext = brack.slice(pipeIdx + 1);
var anchor = document.createElement('a');
anchor.appendChild(document.createTextNode(wikitext));
anchor.setAttribute('href', '/wiki/' + wikilink);
// create list element
var listElt = document.createElement('li');
listElt.appendChild(anchor);
listElt.setAttribute('id', wikilink);
// add list element to list
list.appendChild(listElt);
}
if(displayBox)
{
pBody.appendChild(list);
listDiv.appendChild(pBody);
document.getElementById('p-tb').appendChild(listDiv);
}
}
$(OtherVersions);
/*
</pre>
*/