Jump to content

User:LinguistAtLarge/imagetoggle.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
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);
}