Raw Source
alexander255 / Original Content

// ==UserScript==
// @name        Original Content
// @namespace   ecc96048145ed148517312249505f046
// @description Automatically redirect content stealing websites to their original counterparts
// @require     https://cdnjs.cloudflare.com/ajax/libs/xregexp/2.0.0/xregexp-min.js
// @include     http://*.bigresource.com/*
// @include     http://*.ubuntuask.com/*
// @include     http://ubuntuask.com/*
// @include     http://*.faultserver.com/*
// @include     http://faultserver.com/*
// @include     http://*.techforumnetwork.com/*
// @include     http://techforumnetwork.com/*
// @include     http://*.linuxine.com/story/*
// @include     http://linuxine.com/story/*
// @icon        https://monkeyguts.com/icon/90.jpg
// @version     1.0.2
// @grant       none
// @run-at      document-start
// @updateURL   https://monkeyguts.com/90.meta.js?c
// @downloadURL https://monkeyguts.com/90.user.js?c
// ==/UserScript==

/**
 * The icon has been released by the user "Nisha A" on Flickr under the CC-BY license
 * 
 * Image on Flickr: https://www.flickr.com/photos/samiksha/445070705/sizes/o/in/photostream/
 * Created by user: https://www.flickr.com/photos/samiksha/
 */

const URL = window.location.href;

/**
 * Call the given callback function at the given time during page load
 * 
 * Note: This function assumes that it is being called during "document-start"!
 * 
 * @param {Function} callback
 *        The function to call
 * @param {String}   run_at
 *        During what stage of the page load the function should be called ("document-start", "document-end" or "load")
 * @param {Object}   ...
 *        Additional parameters to pass to the callback function
 */
function _callback_run_at(callback, run_at)
{
	// Read the additonal parameters
	let params = Array.prototype.slice.call(arguments, 2);
	
	switch(run_at) {
		case 'document-start':
			Function.prototype.apply.call(callback, null, params);
		break;
		case 'load':
			window.addEventListener("load", function()
			{
				Function.prototype.apply.call(callback, null, params);
			}, false);
		break;
		default:
			document.addEventListener("DOMContentLoaded", function()
			{
				Function.prototype.apply.call(callback, null, params);
			}, false);
	}
}


/**
 * Process a single URL rewrite rule
 *
 * If `rewriter` is a function then it will receive the following parameters:
 *  - URL:    The URL of the current page
 *  - result: The result of the regular expression matching operation
 * 
 * @param {RegExp|String}   regexp
 *        The regular expression that must be match by the current page URL
 * @param {Function|String} rewriter
 *        A function or XRegExp rewrite string used for replacing the URL
 */
function rule_url(regexp, rewriter)
{
    let result = URL.match(regexp);
    if(result) {
        let href = null;
        if(typeof(rewriter) == 'function') {
            href = rewriter(URL, result);
        } else {
            // Rewrite URL using XRegExp :-)
            href = XRegExp.replace(URL, regexp, rewriter);
        }
        
        // Prevent page load
        //TODO: Get this to do anything more blanking the page
        var interval = window.setInterval(function()
		{
			document.documentElement.innerHTML = "";
		}, 1);
        document.addEventListener("DOMContentLoaded", function(event)
		{
			clearInterval(interval);
		});
        
        // Rewrite URL
        window.location.replace(href);
    }
}


/**
 * Process a single URL click action rule
 * 
 * @param {RegExp|String}   regexp
 *        The regular expression that must be match by the current page URL
 * @param {Function|String} selector
 *        A function or XRegExp rewrite string used to generated to selector of the element to click on
 * @param {String}         [run_at="document-end"]
 *        When to perform the action ("document-start", "document-end" or "load")
 */
function rule_click(regexp, selector, run_at)
{
	let result = URL.match(regexp);
	if(result) {
		if(typeof(selector) == 'function') {
			selector = selector(URL, result);
		} else {
			// Rewrite URL using XRegExp
			selector = XRegExp.replace(URL, regexp, selector);
		}
		
		// Click at the correct stage during page load
		_callback_run_at(function(selector)
		{alert(selector)
			document.querySelector(selector).click();
		}, run_at, selector);
	}
}


/**
 * Process a single URL action rule
 *
 * If `callback` is a function then it will receive the following parameters:
 *  - URL:    The URL of the current page
 *  - result: The result of the regular expression matching operation
 * If `callback` is a string then that string will be executed in the page's context
 * 
 * @param {RegExp|String}   regexp
 *        The regular expression that must be match by the current page URL
 * @param {Function|String} callback(URL)
 *        The function to call when the page URL matches the given regex
 * @param {String}         [run_at="document-end"]
 *        When to perform the action ("document-start", "document-end" or "load")
 */
function rule_action(regexp, callback, run_at)
{
	let result = URL.match(regexp);
    if(result) {
		// Create wrapper function for string callback
		if(typeof(callback) == "string") {
			let code = callback;
			
			function callback()
			{
				window.location.href = "javascript:" + code;
			}
		}
		
		// Execute callback function at the correct stage during page load
		_callback_run_at(callback, run_at, URL, result);
	}
}

rule_url(/^http[s]?:\/\/(?:[^.]+\.)*ubuntuask\.com\/q\/answers[-](?:[^-]+[-])+(\d+)\.html/i,   "https://askubuntu.com/questions/$1");
rule_url(/^http[s]?:\/\/(?:[^.]+\.)*faultserver\.com\/q\/answers[-](?:[^-]+[-])+(\d+)\.html/i, "https://serverfault.com/questions/$1");
rule_url(/^http[s]?:\/\/([^.]+)\.bigresource\.com\/(?:[^-]+[-])+([A-Za-z0-9]{9})\.html/i,      "http://$1.bigresource.com/Track/$1-$2/");

rule_action(/^http[s]?:\/\/(?:[^.]+\.)*techforumnetwork\.com\/techqns\/[a-z-]+\//i, "readPost()", "document-end");

rule_click(/^http[s]?:\/\/(?:[^.]+\.)*linuxine.com\/story\/.+/i, "a.pviewlink", "document-end");