User:LinguistAtLarge/imagetoggle.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:LinguistAtLarge/imagetoggle. |
if (1) // wgCanonicalNamespace == '') // in main namespace // no reason to limit this to article space
{
//importScript('User:LinguistAtLarge/cookie.js');
// use importScriptURI so the image toggle script can be called from other wikis
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:LinguistAtLarge/cookie.js&action=raw&ctype=text/javascript');
var imagetoggle = function(force_hide)
{
var b = document.getElementById('bodyContent');
if (!b)
{
return;
}
var im = b.getElementsByTagName('IMG');
if (!im)
{
return;
}
var i;
var hidden = false;
for (i in im)
{
if (typeof(im[i].src) != 'undefined')
{
if (typeof(force_hide) == 'undefined')
{
// toggle hidden or visible
if (im[i].style.display == 'none')
{
im[i].style.display = '';
hidden = false;
}
else
{
im[i].style.display = 'none';
hidden = true;
}
}
else
{
// force hidden or visible
if (force_hide)
{
im[i].style.display = 'none';
hidden = true;
}
else
{
im[i].style.display = '';
hidden = false;
}
}
}
}
// bug fix for when there are no images on the page (hidden does not get set to true)
if (typeof(force_hide) != 'undefined' && force_hide)
{
hidden = true;
}
// get list of articles we should hide images in
var articleids = get_cookie('imagetogglearticleids');
if (articleids)
{
articleids = articleids.split('|');
}
else
{
articleids = [];
}
// add or remove the current article from the list of articles
if (hidden)
{
// add this article to the list of articles to hide images in
var do_hide = true;
for (i = 0; i < articleids.length; ++i)
{
if (articleids[i] == wgArticleId)
{
do_hide = false;
}
}
if (do_hide)
{
articleids.push(wgArticleId);
}
}
else
{
// remove this article from the list of articles to hide images in
var articleids_temp = articleids;
articleids = [];
for (i = 0; i < articleids_temp.length; ++i)
{
if (articleids_temp[i] != wgArticleId)
{
articleids.push(articleids_temp[i]);
}
}
}
// add or remove strikethrough as visual queue that images are hidden or visible
var tabli = document.getElementById('ca-imageswap');
var tabas = tabli.getElementsByTagName('A');
var tab = tabas[0];
if (hidden)
{
tab.style.textDecoration = 'line-through';
}
else
{
tab.style.textDecoration = '';
}
// save list of articles to cookie
if (articleids.length > 0)
{
articleids = articleids.join('|');
}
else
{
articleids = '';
}
var expiration_date = new Date();
expiration_date.setDate(expiration_date.getDate() + 90); // 90 day cookie
set_cookie('imagetogglearticleids', articleids);
};
var imagetoggleinit = function()
{
mw.util.addPortletLink('p-cactions', 'javascript:imagetoggle();', 'Img', 'ca-imageswap', 'Toggle images in article.');
var articleids = get_cookie('imagetogglearticleids');
if (articleids)
{
articleids = articleids.split('|');
if (articleids && articleids.length && articleids.length > 0)
{
var i;
for (i = 0; i < articleids.length; ++i)
{
if (wgArticleId == articleids[i])
{
imagetoggle(true);
//return; // this causes addOnloadHook to bork
}
}
}
}
};
addOnloadHook(imagetoggleinit);
}