diff options
author | Jesús <heckyel@hyperbola.info> | 2020-01-01 13:30:57 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2020-01-01 13:30:57 -0500 |
commit | 80a8b19fd552a55bc5bbd0460aaa5834fdbe67ce (patch) | |
tree | cf6c43d8e4448c6df9439a539ddcbb413e5425bc | |
parent | 730ca49e53903452d6e5ecac51eb882f2d1925cb (diff) | |
download | book-80a8b19fd552a55bc5bbd0460aaa5834fdbe67ce.tar.lz book-80a8b19fd552a55bc5bbd0460aaa5834fdbe67ce.tar.xz book-80a8b19fd552a55bc5bbd0460aaa5834fdbe67ce.zip |
[YouTube to Invidious] reorganize code
-rw-r--r-- | scripts-greasemonkey/youtube_to_invidious.user.js | 145 |
1 files changed, 76 insertions, 69 deletions
diff --git a/scripts-greasemonkey/youtube_to_invidious.user.js b/scripts-greasemonkey/youtube_to_invidious.user.js index 12e3df2..b807431 100644 --- a/scripts-greasemonkey/youtube_to_invidious.user.js +++ b/scripts-greasemonkey/youtube_to_invidious.user.js @@ -34,46 +34,24 @@ 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 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(); +function statuscheck(){ + // Console Feedback + console.log("%cUSERSCRIPT | " + GM_info.script.name + " " + GM_info.script.version + " | successfully initialized", consoleCSS); } -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 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 embed(){ for(let i=0;i<frames.length;i++){ @@ -105,39 +83,6 @@ function embed(){ } } -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++){ @@ -177,7 +122,69 @@ 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); +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){ + 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}); |