Original Content

Automatically redirect content stealing websites to their original counterparts

当前为 2014-05-24 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Original Content
// @namespace   6a268d9ddec60bd8512b0503c4914388
// @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     *://*.bigresource.com/*
// @include     *://*.ubuntuask.com/*
// @include     *://ubuntuask.com/*
// @include     *://*.faultserver.com/*
// @include     *://faultserver.com/*
// @include     *://*.techforumnetwork.com/*
// @include     *://techforumnetwork.com/*
// @include     *://*.linuxine.com/story/*
// @include     *://linuxine.com/story/*
// @include     *://*.forumsee.com/*
// @include     *://forumsee.com/*
// @include     *://*.bighow.net/*
// @include     *://bighow.net/*
// @icon        https://monkeyguts.com/icon/90.jpg
// @version     1.0.5
// @grant       none
// @run-at      document-start
// ==/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);
    }
}


/**
 * Follow a hyperlink on a page
 * 
 * @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_link(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)
		{
            window.location.replace(document.querySelector(selector).href);
		}, 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_url(/^http[s]?:\/\/(?:[^.]+\.)*bighow\.net\/(\d+)-([^.]+)\.html/i,                         "http://bighow.net/track/$1/$2");

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

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