Jump to content

User:Weeklyd3/scripts/linkRemover.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.
/* Hides some links I do not frequently use. */
(function() {
var idsToHide = ["t-mute",
'n-portal', 
'n-help', 
'n-introduction',
'n-currentevents',
'n-contents',
'n-aboutsite',
'n-contactpage',
'n-sitesupport'];
if (!globalThis.hiddenLeft) globalThis.hiddenLeft = idsToHide;
for (var i = 0; i < globalThis.hiddenLeft.length; i++) {
	var link = document.querySelector('li#' + globalThis.hiddenLeft[i]);
	if (!link) continue;
	link.style.display = 'none';
}
})();
if (mw.config.get("wgFormattedNamespaces")[mw.config.get("wgNamespaceNumber")] === "Special" 
    && mw.config.get("wgTitle") === "HideLinks") {
	document.querySelector('title').textContent = 'Hide links - Wikipedia';
	document.querySelector('h1').textContent = 'Links to hide';
	document.querySelector('#mw-content-text').innerHTML = `
	<p>Here are some of the IDs of links on the left. Note that other userscripts may add other links.</p>
	<p>To choose the links to hide, set <code>globalThis.hiddenLeft</code> to an array with the ids to hide <em>before loading the link remover script</em>.</p>
	<p>(Note: This list is generated automatically on page load.)</p>
	<table class="wikitable sortable">
	<thead><tr style="">
	<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">Link text
	</th>
	<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">ID
	</th>
	<th class="headerSort" tabindex="0" role="columnheader button" title="Sort ascending">Currently hidden?
	</th></tr></thead><tbody id="links">
	</tbody><tfoot></tfoot></table>
	`;
	var links = document.querySelectorAll('#mw-panel > nav');
	for (var i = 0; i < links.length; i++) {
		var menu = links[i];
		var title = menu.children[0];
		if (title.textContent != "") {
			var titleField = document.createElement('tr');
			var titleText = document.createElement('td');
			titleText.style.textAlign = 'center';
			titleText.textContent = title.textContent;
			titleText.setAttribute('colspan', '3');
			titleField.appendChild(titleText);
			document.querySelector('#links').appendChild(titleField);
		}
		var menuChildren = menu.children[1].children[0];
		if (menuChildren.tagName !== 'UL') continue;
		var menuChildrenItems = menuChildren.children;
		for (var j = 0; j < menuChildrenItems.length; j++) {
			var menuItem = menuChildrenItems[j];
			var data = document.createElement('tr');
			var linkText = document.createElement('td');
			linkText.textContent = menuItem.textContent;
			data.appendChild(linkText);
			var idText = document.createElement('td');
			idText.textContent = menuItem.getAttribute('id');
			data.appendChild(idText);
			var hidden = document.createElement('td');
			hidden.innerHTML = (menuItem.style.display == 'none') ? '<strong>Yes</strong>' : "No";
			data.appendChild(hidden);
			document.querySelector('#links').appendChild(data);
		}
	}
}