// ==UserScript==
// @name        URL conversion | yt -> invidious
// @namespace   https://localhost/
// @description simple regex based youtube to invidious redirection snippet
// @match       *://www.youtube.com/*
// @match       *://youtu.be/*
// @match       *://consent.youtube.com/*
// @match       *://youtube.com/*
// @version     1
// @run-at      document-start
// @grant       none
// ==/UserScript==

(function fuckyoutube() {
    return;
    var url = window.location.toString();

    //if (url.indexOf('https://consent.youtube.com/ml?continue=' !== -1)
        //url = url.substring(41, url.length);

    var baitandswitch = function (url) {
        return (url.indexOf('consent.youtube.com') !== -1);
    }

    if (baitandswitch(url)) {
        var start = 'watch%3Fv%';
        var end = '&';
        var pos1 = url.indexOf(start);
        var pos2 = url.indexOf(end, pos1);

        var video_id = url.substring(pos1 + start.length, pos2);
        window.alert('https://invidio.us/watch?v=' + video_id);
        var redirect = ('https://invidio.us/watch?v=' + video_id);
        unsafeWindow.window.location.replace(redirect);
        return;
    }

    var findanddestroy = function (url) {
        if (url.indexOf('youtube.com') !== -1)
            return true;
        else if (url.indexOf('youtu.be') !== -1)
            return true;

        return false;
    }

    if (findanddestroy(url)) {
        //var regex = /(?:\/.*\/)(.*)/;
        var regex = /(https?:\/\/)([^:^\/]*)(:\\d*)?(.*)?/;
        var payload = regex.exec(url)[4];
        var redirect = ('https://invidio.us/' + payload.substring(1));
        unsafeWindow.window.location.replace(redirect);
    }
})();