NPR text-only link fix

Fixes the portal text-only link, and styles the text-only output

Click here to install Browse More Scripts
// ==UserScript==
// @name         NPR text-only link fix
// @namespace    48907c2e497f53a9f6c9736e01c8b484efea070f
// @version      0.1
// @description  Fixes the portal text-only link, and styles the text-only output
// @author       /u/AyrA_ch
// @match        https://choice.npr.org/*
// @match        https://text.npr.org/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function () {
	'use strict';

	// Grab the thing id form the destination.
	//Somewhat based on the npr internal function but changed to actually work.
	var getThingId = function (destination) {
		//Date and id
		var match = destination.match(/https:\/\/www\.npr\.org\/([a-z]+\/){0,2}\d{4}\/\d{2}\/\d{2}\/(\d+)\/.*/);
		if (match) {
			return match[2];
		}

		//ID without a date
		match = destination.match(/https:\/\/www\.npr\.org\/([a-z]+\/){1,2}(\d+)\/.*/);
		if (match) {
			return match[2];
		}

		//special "thing" parameter
		match = destination.match(/https:\/\/www\.npr\.org\/.*[iI]d=(\d{4,}).*/);
		if (match) {
			return match[1];
		}

		//Try our own function
		match = destination.match(/\/\d{4}\/\d{2}\/\d{2}\/(\d+)\//);
		if (match) {
			return match[1];
		}

		// Check if we have a hard coded page url
		//Remove https://www.npr.org from the destination
		var location = destination.substr(19);
		for (var key in window.redirectLookup) {
			// If the first part of the location matches a
			// hard coded url, then we have a match.
			if (location.startsWith(key)) {
				return window.redirectLookup[key];
			}
		}

		//Didn't found anything
		return false;
	};

	var showClock = function () {
		var e = showClock.timeElement;
		if (!e.querySelector("time")) {
			e.innerHTML = "[<time title=\"Current date and time\"></time>]";
		}
		var d = new Date();
		var time = d.toLocaleString();

		if (showClock.blink) {
			time = time.replace(/:/g, " ");
			showClock.blink = false;
		} else {
			showClock.blink = true;
		}

		e.querySelector("time").setAttribute("datetime", d.toJSON());
		e.querySelector("time").textContent = time;

		//Delay compensated timer
		setTimeout(showClock, 500 - (d.getTime() % 500));
	};

	//Text-only article styling
	var initText = function () {
		console.debug("Initializing custom text styling");

		//Add basic styles
		document.body.classList.add("cga-light-green");
		document.body.style.maxWidth = "60em";
		document.body.style.margin = "1em auto";
		document.body.style.padding = "1em";
		document.body.style.border = "10px double #FFF";

		//Get all direct body descendant paragraphs for processing
		var segments = document.querySelectorAll("body > p");
		if (segments.length > 3) {

			//Replace title paragraph with actual title tag
			var title = document.createElement("h1");
			title.textContent = document.title = segments[2].textContent;
			title.classList.add("cga-light-yellow");
			document.body.insertBefore(title, segments[2]);

			//Author
			segments[3].style.textAlign = "right";
			segments[3].classList.add("cga-light-black");

			//Time
			showClock.timeElement = document.createElement("p");
			showClock.timeElement.style.textAlign = "right";
			showClock.timeElement.classList.add("cga-light-black");
			document.body.insertBefore(showClock.timeElement, segments[4]);

			//Remove unnecessary paragraphs
			segments[0].remove();
			segments[1].remove();
			segments[2].remove(); //Title is now a H1 tag

			//Remove static bottom links
			var lists = document.querySelectorAll('body > ul');
			lists[lists.length - 1].remove();

			//Add custom info text so the user knows what's going on
			segments[segments.length - 1].textContent = "Formatted by Tampermonkey script 'NPR text-only link fix'";
			segments[segments.length - 1].style.textAlign = "right";
			segments[segments.length - 1].classList.add("cga-light-black");
		}

		//fix all link colors and targets
		document.querySelectorAll("a").forEach(function (v) {
			var id = getThingId(v.href);
			if (id) {
				//Make sure internal links keep the user on the text portal
				v.href = "https://text.npr.org/r.php?id=" + id;
				//red=internal
				v.classList.add("cga-light-red");
			} else {
				if (v.href.match(/^https?:\/\/text.npr.org\//i)) {
					//Show as internal link since already on text portal
					v.classList.add("cga-light-red");
				} else {
					//blue=external
					v.classList.add("cga-light-blue");
					v.title = "External or non-text link";
				}
			}
		});

		if (showClock.timeElement) {
			showClock();
		}
	};

	//Fixes the text-only button
	var initPortal = function () {
		console.debug("Fixing portal 'text-only' button");
		//We are on the portal that links to the text-only version.
		//Make sure the button actually does.
		//To deter people from using the version without ads or trackers they don't actually set the button URL properly,
		//even though the JS code for it exists.
		var id = getThingId(location.href);
		if (id) {
			document.querySelector("#textLink").href = "https://text.npr.org/r.php?id=" + id;
		}
	};

	//Checks if on the text-only portal
	var isTextPortal = function () {
		return location.hostname.toLowerCase() === "text.npr.org";
	}

	//Initializes the appropriate function
	var init = function () {
		console.debug("Running custom init");
		//Check if we are currently on the text-only portal
		if (isTextPortal()) {
			initText();
		} else {
			initPortal();
		}
	};

	//Insert stylesheet immediately
	if (isTextPortal()) {
		//Add custom stylesheet with font and CGA color scheme
		var l = document.createElement("link");
		l.href = "https://cable.ayra.ch/crt/cga.css";
		l.type = "text/css";
		l.rel = "stylesheet";
		document.head.appendChild(l);
	}

	if (["complete", "loaded", "interactive"].indexOf(document.readyState) >= 0) {
		console.debug("Site ready already");
		init();
	} else {
		console.debug("Site NOT ready. Deferring execution of style routine");
		//Defer execution of body related things to the
		document.addEventListener("DOMContentLoaded", init);
	}
})();

/*
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 3.145.94.251