User:Cryptic/spoiler.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Cryptic/spoiler. |
//<pre><nowiki>
var hide_spoilers_initially = window.hide_spoilers_initially || false;
var hide_endspoiler_tag = window.hide_endspoiler_tag || false;
var spoiler_num = 0;
function spoiler_toggle(num)
{
var contents = document.getElementById('spoilercontents_' + num);
var lk = document.getElementById('spoilerlk_' + num);
if (!contents || !lk)
return;
if (contents.style.display == 'none')
{
contents.style.display = '';
lk.replaceChild(document.createTextNode('hide'), lk.firstChild);
}
else
{
contents.style.display = 'none';
lk.replaceChild(document.createTextNode('show'), lk.firstChild);
}
}
function setup_spoilers()
{
var divs = document.getElementsByTagName('div');
var hide_initially = hide_spoilers_initially
&& !/(\?title=|\/wiki\/)(Talk|User|Wikipedia|Image|MediaWiki|Template|Help|Category|Portal)(_talk)?:/.test(window.location.href);
for (var i = 0; i < divs.length; ++i)
{
var node = divs[i];
if (node.id != 'spoiler')
continue;
++spoiler_num;
var lk = document.createElement('a');
lk.setAttribute('href', 'javascript:spoiler_toggle(' + spoiler_num + ')');
lk.appendChild(document.createTextNode(hide_initially ? 'show' : 'hide'));
lk.id = 'spoilerlk_' + spoiler_num;
lk.className = 'spoilertoggle';
node.appendChild(document.createTextNode(' ['));
node.appendChild(lk);
node.appendChild(document.createTextNode(']'));
var contents = document.createElement('div');
contents.className = 'spoilercontents';
contents.id = 'spoilercontents_' + spoiler_num;
if (hide_initially)
contents.style.display = 'none';
var depth = 0;
while (depth >= 0)
{
var n = node.nextSibling;
if (!n)
break;
if (n.id == 'spoiler')
++depth;
else if (n.className && n.className.indexOf('endspoiler') >= 0
&& --depth < 0 && !hide_endspoiler_tag)
break;
n.parentNode.removeChild(n);
contents.appendChild(n);
}
if (node.nextSibling)
node.parentNode.insertBefore(contents, node.nextSibling);
else
node.parentNode.appendChild(contents);
}
}
addOnloadHook(setup_spoilers);
//</nowiki></pre>