MovieTV Slash Redirect

Redirect old address.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

"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);
        });
    });
}