aboutsummaryrefslogtreecommitdiffstats
path: root/scripts-greasemonkey/youtube_to_invidious.user.js
blob: 06d5f38ed9a5e4ad3999724b0e2c371c49a9694b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// ==UserScript==
// @name        YouTube to Invidious
// @author      OdinBrood, Jesús E.
// @namespace   InvidiousRedirect
// @description Scan page for Youtube urls and replace with Invidious.
// @homepageURL https://libregit.org/heckyel/book/src/branch/master/scripts-greasemonkey
// @include     *
// @exclude     /^http(s|)://(www[.]|)invidio[.]us/.*$/
// @exclude     /^http(s|)://(www[.]|proxy[.]|)invidious[.]snopyta[.]org/.*$/
// @exclude     /^http(s|)://(www[.]|)vid[.]wxzm[.]sx/.*$/
// @exclude     /^http(s|)://(www[.]|)invidious[.]kabi[.]tk/.*$/
// @exclude     /^http(s|)://(www[.]|)invidiou[.]sh/.*$/
// @exclude     /^http(s|)://(www[.]|)yewtu[.]be/.*$/
// @exclude     /^http(s|)://(www[.]|)invidious[.]ggc-project[.]de/.*$/
// @exclude     /^http(s|)://(www[.]|)invidious[.]enkirton[.]net/.*$/
// @exclude     /^http(s|)://(www[.]|)tube[.]poal[.]co/.*$/
// @exclude     /^http(s|)://(www[.]|)invidious[.]13ad[.]de/.*$/
// @grant       GM_xmlhttpRequest
// @version     8.4.6
// @license     GPL version 3 or any later version::: https://www.gnu.org/licenses/gpl-3.0.html
// ==/UserScript==
/* jshint esversion: 6 */

// set you favorite Invidious instance! (https://github.com/omarroth/invidious/wiki/Invidious-Instances)
let instance = 'invidio.us';

// Console Style - Debug
let consoleCSS = 'background: #000; color: #00FF00; padding: 0px 7px; border: 1px solid #00FF00; line-height: 16px;';

// change script options, default values recommended
let a=1; //set to 0 to force autoplay off, set to 1 to keep embed's value [default 1]
let b=1; //set to 0 to not replace all youtube links to Invidious [default 1]
let c=1; //set to 0 to disable DASH playback (beta feature) [default 1]
let d=1; //set to 0 to disable Invidious proxy [default 1]
let e=1; //set to 0 to disable bypass of url shorteners [default 1]

let ytdomains=new RegExp(/http(s|):\/\/(m[.]|i[.]|www[.]|img[.]|)(youtu(|be|be-nocookie)|.*ytimg)[.](com|be)\/.*/);
let params=new RegExp(/^(autoplay|channel|v|playlist|list)$/);
let current=window.location.href.match(ytdomains)===null;
let thumbs,links,skip;

function statuscheck(){
    // Console Feedback
    console.log("%cUSERSCRIPT | " + GM_info.script.name + " " + GM_info.script.version + " | successfully initialized", consoleCSS);
}

function link(){
    for(let i=0;i<links.length;i++){
        let url=new URL(links[i].href.match(ytdomains)[0]);
        url.hostname=instance;
        links[i].href=url;
    }
}

function ytel(el){
    for(let i=0;i<el.attributes.length;i++){
        let val=el.attributes[i].value;
        if(val.substring(0,2)=='//')val='https:'+val;
        try{val=decodeURIComponent(val);}catch(e){}
        if(val.match(ytdomains)){
            el.attributes[i].value=val;
            return true;
        }
    }
}

function ythref(el){
    return(decodeURIComponent(el.href).match(ytdomains));
}

function thumb(){
    for(let i=0;i<thumbs.length;i++){
        let url=new URL(thumbs[i].src.match(ytdomains)[0]);
        url.hostname=instance;
        thumbs[i].src=url;
    }
}

function redir(){
    let url=new URL(window.location.href);
    url.hostname=instance;
    location.href=url;
}

function addbtn(){
    for(let i=0;i<title.length;i++){
        let btn=document.createElement('a');
        btn.innerHTML='<h2>Watch on '+instance+'</h2>';
        btn.href='javascript:void(0)';
        btn.onclick=function(){redir();};
        btn.className='skipinv';
        title[i].parentNode.appendChild(btn);
    }
}

if(current){
    thumbs=Array.prototype.slice.call(document.getElementsByTagName('img')).filter(ytel);
    if(b==1)links=Array.prototype.slice.call(document.getElementsByTagName('a')).filter(ythref);
    if(thumbs.length>0)thumb();
    if(links.length>0)link();
    statuscheck();
}else{
    let title=Array.prototype.slice.call(document.getElementsByTagName('h1'));
    addbtn();
}

let observer=new MutationObserver(function(mutations){
    mutations.forEach(function(mutation){
        if(current){
            thumbs=Array.prototype.slice.call(mutation.target.getElementsByTagName('img')).filter(ytel);
            if(thumbs.length>0){
                thumb();
            }
            if(b==1){
                links=Array.prototype.slice.call(mutation.target.getElementsByTagName('a')).filter(ythref);
                if(links.length>0)link();
            }
        }else{
            skip=Array.prototype.slice.call(mutation.target.getElementsByClassName('skipinv'));
            if(skip<1){
                title=Array.prototype.slice.call(mutation.target.getElementsByTagName('h1'));
                addbtn();
            }
        }
    });
});

observer.observe(document.body,{childList:true,subtree:true});