Tag Stripper

Provides stripAll() and autoStrip() which strips a website of everything except a few whitelisted things

Click here to install Browse More Scripts
// ==UserScript==
// @name         Tag Stripper
// @namespace    5b5772ac9a58f2cbcdd4e040c0eb874e1c41acea
// @version      0.9
// @description  Provides stripAll() and autoStrip() which strips a website of everything except a few whitelisted things
// @author       /u/AyrA_ch
// @match        http://*/*
// @match        https://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

//Exposed functions
//stripAll()      - Strips all tags not in the whitelist
//autoStrip()     - Enables automatic tag stripping on the current origin
//stopAutoStrip() - Disables automatic tag stripping on the current origin

(function () {
	'use strict';
	//This list contains elements themselves that shouldn't be removed
	//The explanation for autoStrip() will be here if needed.
	var wlTags = [];
	//Tag white-list. There's no technical necessity for a certain order.
	//Everything not in this list will be removed from the document
	var wl = [
		//Required elements
		"html", "body", "head", "title",
		//Generic Elements
		"p", "div", "pre", "hr",
		//Links
		"a",
		//Text formatting
		"code", "b", "strong", "i", "u", "span", "br",
		//Lists
		"ul", "ol", "li", "dd", "dt", "dl",
		//Headings
		"h1", "h2", "h3", "h4", "h5", "h6",
		//Table
		"table", "thead", "tbody", "tfoot", "tr", "td", "th",
		//Forms
		"form", "input", "button", "select", "textarea", "option", "optgroup"
	];
	//Attribute white-list
	//Attributes not in this list will be stripped completely from all whitelisted tags
	var awl = [
		//Generic
		"id", "name", "title", "lang",
		//Form
		"action", "method", "type", "value", "placeholder", "pattern", "rows", "cols",
		//Links
		"href", "target", "rel"
	];
	window.stripAll = function () {
		//Gets Everything
		var ele = document.querySelectorAll('*');
		for (var i = 0; i < ele.length; i++) {
			var e = ele[i];
			if (wlTags.indexOf(e) < 0) {
				//Don't bother to remove attributes if the tag is not whitelisted
				//We are going to remove it anyways completely
				if (wl.indexOf(e.tagName.toLowerCase()) < 0) {
					console.debug("Removing Tag", e.tagName);
					e.remove();
				} else {
					var attrs = e.attributes;
					for (var j = 0; j < attrs.length; j++) {
						var attr = attrs[j].name.toLowerCase();
						if (awl.indexOf(attr) < 0) {
							console.debug("Removing Attribute", attr);
							e.removeAttribute(attr);
						}
					}
				}
			}
		}
	};

	//Automatic tag stripping
	window.autoStrip = function () {
		localStorage.setItem("ayra.autoStripTags", "1");
		window.stripAll();
		addAutoStripTag();
		window.autoStrip = function () {
			console.info('Auto Stripper already enabled.');
		};
	};

	//Disable automatic tag stripping
	window.stopAutoStrip = function () {
		localStorage.removeItem("ayra.autoStripTags");
		alert("Tags are no longer automatically stripped. Reload for the changes to take effect.");
	};

	//Adds a tag to the page top that informs the user why it looks "reduced"
	var addAutoStripTag = function () {
		var d = document.createElement("div");
		d.innerText = "Tags are automatically stripped on " + location.origin + ". Click here to disable autoStrip";
		d.onclick = function () {
			window.stopAutoStrip();
			d.remove();
		};
		d.style.color = "#F00";
		d.style.cursor = "pointer";
		document.body.insertBefore(d, document.body.childNodes[0]);
		wlTags.push(d);
		return d;
	};

	//Check for autoStrip on startup and take action if enabled
	var checkAuto = function () {
		if (!checkAuto.processed && localStorage.getItem("ayra.autoStripTags") === "1") {
			window.stripAll();
			if (document.body) {
				addAutoStripTag();
			} else {
				document.addEventListener("DOMContentLoaded", addAutoStripTag);
			}

			var mo = new MutationObserver(function (v) {
					window.stripAll();
				});

			mo.observe(document.documentElement, {
				attributes: true,
				childList: true,
				characterData: false,
				subtree: true
			});
			checkAuto.processed = true;
		}
	};

	if (document.documentElement) {
		checkAuto();
	} else {
		document.addEventListener("DOMContentLoaded", checkAuto);
	}
})();

/*
LICENSE:
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
The full license text can be found here: http://creativecommons.org/licenses/by-nc-sa/4.0/
The link has an easy to understand version of the license and the full license text.

DISCLAIMER:
THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/

Copyright © 2018 by Kevin Gut 📧 | More services | Generated for 54.236.62.49