// ==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() {
    var url = window.location.toString();

    var findanddestroy = function (url) {
        return (url.indexOf('consent.youtube.com') !== -1) ||
            (url.indexOf('youtu.be') !== -1) ||
            (url.indexOf('youtube.com') !== -1);
    }

    if (findanddestroy(url)) {
        var start = (url.indexOf('embed/') !== -1) ?
            'embed/' :
            (url.indexOf('embed%252F') !== -1) ?
                'embed%252F' :
                (url.lastIndexOf('watch%3Fv%3D') !== -1) ?
                    'watch%3Fv%3D' :
                    (url.lastIndexOf('watch?v%3D')) ?
                        'watch?v%3D' :
                        'watch?v=';
        //var start = 'watch%3Fv%';
        var end = '&';
        var pos1 = url.indexOf(start);
        var pos2 = url.indexOf(end, pos1);
        pos2 = pos2 === -1 ? url.length : pos2;

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