MovieTV Slash Redirect

Redirect old address.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

"use strict";

/**
 * Created by Nb on 12/7/2016.
 * Transpiled from ES6 to ES5 by Babel.
 */

// ==UserScript==
// @name         MovieTV Slash Redirect
// @name:zh-CN   随缘旧域名重定向
// @name:zh-TW   隨緣舊域名重定向
// @namespace    NB-Kevin
// @version      0.1
// @description  Redirect old address.
// @description:zh-CN 重定向旧域名到 mtslash.org。
// @description:zh-TW 重定向舊域名 mtslash.org。
// @author       Nb/Kevin
// @match        http://*/*
// @match        https://*/*
// @run-at       document-start
// @grant        none
// @license      MIT
// ==/UserScript==

var OLD_ADDRESS = "www.movietvslash.com";
var ANOTHER_OLD_ADDRESS = "www.mtslash.com";
var NEW_ADDRESS = "www.mtslash.org";

/**
 * Convert a node list to array.
 * @returns {Array.<HTMLElement>}
 */
NodeList.prototype.toArray = function () {
    var that = this;
    return Array.prototype.slice.call(that);
};

var currentAddress = location.href.split('//')[1];
if (currentAddress.startsWith(OLD_ADDRESS) || currentAddress.startsWith(ANOTHER_OLD_ADDRESS)) {
    // Check whether current page is of the old address and
    // redirect if so.
    var targetAddress = location.href.replace(OLD_ADDRESS, NEW_ADDRESS).replace(ANOTHER_OLD_ADDRESS, NEW_ADDRESS);
    console.log("Redirecting to " + targetAddress + "...");
    location.replace(targetAddress);
} else {
    // otherwise check all href link and replace them with new address
    addEventListener('load', function (event) {
        var targetElements = document.querySelectorAll("[href*=\"" + OLD_ADDRESS + "\"]").toArray().concat(document.querySelectorAll("[href*=\"" + ANOTHER_OLD_ADDRESS + "\"]").toArray());
        targetElements.forEach(function (element) {
            var newAddress = element.getAttribute('href').replace(OLD_ADDRESS, NEW_ADDRESS).replace(ANOTHER_OLD_ADDRESS, NEW_ADDRESS);
            console.log("Redirecting " + element.getAttribute('href') + " to " + newAddress + "...");
            element.setAttribute('href', newAddress);
        });
    });
}