aboutsummaryrefslogtreecommitdiffstats
path: root/scripts-greasemonkey/youtube_to_invidious.user.js
blob: 12e3df21785f38f5e3d5144bb66ff111dc24640e (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// ==UserScript==
// @name        YouTube to Invidious
// @author      OdinBrood, Jesús E.
// @namespace   InvidiousRedirect
// @description Scan page for Youtube embeds and 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 shorteners=new RegExp(/^http(s|):\/\/(bit.ly|goo.gl|tinyurl.com|t.co|ow.ly|is.gd|buff.ly|deck.ly|su.pr|lnk.co|fur.ly|moourl.com|)\/.*/);
let params=new RegExp(/^(autoplay|channel|v|playlist|list)$/);
let current=window.location.href.match(ytdomains)===null;
let frames,thumbs,links,skip;

if(current){
    frames=Array.prototype.slice.call(document.getElementsByTagName('iframe')).filter(ytel);
    thumbs=Array.prototype.slice.call(document.getElementsByTagName('img')).filter(ytel);
    if(b==1)links=Array.prototype.slice.call(document.getElementsByTagName('a')).filter(ythref);
    if(frames.length>0)embed();
    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){
            frames=Array.prototype.slice.call(mutation.target.getElementsByTagName('iframe')).filter(ytel);
            thumbs=Array.prototype.slice.call(mutation.target.getElementsByTagName('img')).filter(ytel);
            if(frames.length>0)embed();
            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});

function embed(){
    for(let i=0;i<frames.length;i++){
        frames[i].style.backgroundColor='rgba(35,35,35,1)';
        frames[i].style.backgroundRepeat='no-repeat';
        frames[i].style.backgroundPosition='center center';
        frames[i].style.backgroundSize='auto';
        for(let j=0;j<frames[i].attributes.length;j++) {
            if(frames[i].attributes[j].value.match(ytdomains)){
                let url=new URL(frames[i].attributes[j].value);
                if(!url.hostname.match(/youtube/)){
                    url=new URL(decodeURIComponent(url.href).match(ytdomains)[0]);
                    url.searchParams.set('autoplay',0);
                }
                for(let key of url.searchParams.keys()){
                    if(!(key.match(params)))url.searchParams.delete(key);
                }
                url.hostname=instance;
                if(a==1){
                    if(!url.searchParams.has('autoplay')||url.searchParams.get('autoplay')==='')url.searchParams.set('autoplay',0);
                }else{
                    url.searchParams.set('autoplay',0);
                }
                if(c==1)url.searchParams.set('quality','dash');
                if(d==1)url.searchParams.set('local',true);
                frames[i].setAttribute(frames[i].attributes[j].name,url);
            }
        }
    }
}

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

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

function unshorten(long,short){
    frames=Array.prototype.slice.call(document.getElementsByTagName('iframe'));
    for(let k=0;k<frames.length;k++){
        for(let l=0;l<frames[k].attributes.length;l++) {
            if(frames[k].attributes[l].value==long){
                frames[k].attributes[l].value=short;
                frames=[frames[k]];
                embed();
                return;
            }
        }
    }
}

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(shorteners)&&e==1){
            let long=el.attributes[i].value=val;
            GM_xmlhttpRequest({
                method:'GET',url:long,onload:function(response){
                    let short=response.finalUrl;
                    if(short.match(ytdomains))unshorten(long,short);
                }
            });
        }
        if(val.match(ytdomains)){
            el.attributes[i].value=val;
            return true;
        }
    }
}

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

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