// ==UserScript== // @in-clude * // @include *//youtube.com/* // @include *.youtube.com/* // @include *//youtube-nocookie.com/* // @include *.youtube-nocookie.com/* // @include *//youtubeeducation.com/* // @include *.youtubeeducation.com/* // @exclude http://tparser.org/* // ==/UserScript== if ( document.contentType=='text/html' || (''+location).match(/[/.]youtube(-nocookie|education|).com\/v\//) ) (function(opera){ var ScriptName = 'youtube_fix.js v17'; var conf = { fixObjects : true // object -> iframe , fixInsertedObjects : { enabled: true // may break sites; use whitelist or // @exclude * , blacklist : ['translate.yandex.ru'] // [] - works anywhere; ['tparser.org'] , whitelist : [] // [] - works anywhere; ['3dnews.ru', 'livejournal.com'] } , blockYoutubeScripts : true // except player base.js //, loadComments : true // don't works , addLinksInEmbeds : true , loadThumbnails : true , fixList : true // for long channel lists , fontFamily: 'Arial' , fontSize: '16px' } var showConf = true; function log(str){ opera.postError(ScriptName +(str?': '+str:'')+(showConf?'\nconf='+JSON.stringify(conf):'')); showConf = false; } function findHost(hosts, host){ var flag = -1; while (flag==-1 && host) { flag = hosts.indexOf(host); host = host.replace(/^[^.]*\.?/,''); } return flag; } var youtubeHosts = ['youtube.com','youtube-nocookie.com','youtubeeducation.com']; function splitKV(str, o) { o = o || {}; var tmp; var re = /([\da-z_]+)=([^&]*)/gi; while ( (tmp = re.exec(str))!==null ) { o[tmp[1]] = decodeURIComponent(tmp[2].replace(/\+/g,' ')); } return o; } function getField(obj) { var tmp = obj; if (tmp) for (var i=1; i<arguments.length; i++) { tmp = tmp[arguments[i]]; if (!tmp) break; } return tmp; } function padZero(number, length) { if (number===null||number===false) return; var str = '' + number; while ( str.length < length) { str = '0' + str; } return str; } function s2hms(n) { var h = Math.floor(n/60/60); var m = Math.floor((n/60)%60); var s = n%60; return padZero(h,2)+':'+padZero(m,2)+':'+padZero(s,2); } function create(el, cn, ih){var tmp=document.createElement(el); if(cn)tmp.className=cn; tmp.innerHTML=ih||''; return tmp; } function xhr(url, method, post, xml, headers){ var x = new XMLHttpRequest(); x.open(method || 'GET', url, false); if (headers) for (var i=0; i<headers.length; i++) x.setRequestHeader(headers[i][0], headers[i][1]); x.send(post); return xml?x.responseXML:x.responseText; } var isYoutube = findHost(youtubeHosts, location.hostname)!=-1; var lpath = location.pathname.split('/'); var lsearch = splitKV(location.search.slice(1)); var polymerShit = ['c','channel','user']; if (isYoutube && lpath[1]) { if (lpath[1]=='v' && (lpath[2]||'').length==11) { stop(); log('redirecting to /embed/'); location = '/embed/'+v[1]; } if (~polymerShit.indexOf(lpath[1]) && !lpath[3]) { stop(); lpath[3]='videos'; location = lpath.join('/'); } } var videoId = lsearch.v || lpath[1]=='embed' && lpath[2]; if (conf.blockYoutubeScripts && isYoutube) { var once = true; window.canBlockPlayerBase = true; var needPlayerBase = true; var func = function(){}; requestAnimationFrame = func; writeEmbed = func; writeLightweightEmbed = func; spf = {script:{path:func, declare:func}}; ytcsi = {info: func, tick:func}; yt = {config_:{}, pushConfigArray:func, setMsg:func} yt.setConfig = function(o,v){ if (v) { yt.config_[o]=v } else {for (var k in o) yt.config_[k]=o[k]} if (needPlayerBase && o && o.PLAYER_CONFIG && o.PLAYER_CONFIG.assets && o.PLAYER_CONFIG.assets.js) { var s = document.createElement('script'); s.src = o.PLAYER_CONFIG.assets.js; document.body.appendChild(s); log('inserted player base.js: '+s.src); needPlayerBase = false; } } var moreButton; /* /s/player/4fbb4d5b/player_ias.vflset/ru_RU/base.js */ function ff(){ if (!moreButton) { moreButton = document.querySelector('button#more'); if (moreButton){ var L=document.selectNodes('//link[@rel="next"]'); for (var l=0; l<L.length; l++) { L[l].parentNode.removeChild(L[l]); }; var ln=document.createElement('link'); ln.rel='next'; ln.href="javascript:document.querySelector('button#more').click()"; document.body.appendChild(ln); opera.postError(ScriptName+' FF'); } } } opera.addEventListener("BeforeExternalScript",function(e){ if (e.element.src.match(/\/player.*?\/base\.js/)) { needPlayerBase = false; } else { e.preventDefault(); if (once) once = log('blocking youtube scripts...'); } },false); } document.addEventListener('DOMContentLoaded',function(){ var searchform = '<form action="/results"><input name="search_query" type="text" tabindex="1" size=88 placeholder="search"/><input type=submit></form>'; if (location.pathname=='/') document.body.innerHTML = searchform; if (lpath[3]=='videos' || lpath[1]=='feed' || lpath[1]=='results') { if (!window["ytInitialData"]) { for (var i=0; i<document.scripts.length; i++){ if (document.scripts[i].innerHTML.indexOf('window["ytInitialData"]')!=-1) eval(document.scripts[i].innerHTML); } } /* https://www.youtube.com/results?search_query=test&search=Search ytInitialData.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.contents[ {videoRenderer.videoId https://www.youtube.com/c/name/videos ytInitialData.contents.twoColumnBrowseResultsRenderer.tabs[1].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0].gridRenderer.items [] */ if (window["ytInitialData"]) { //var data = window["ytInitialData"].contents.twoColumnBrowseResultsRenderer.tabs[1].tabRenderer.content.sectionListRenderer.contents[0].itemSectionRenderer.contents[0].gridRenderer; //alert(JSON.stringify(ytInitialData,null,'\t')); var cont, track; var data = window["ytInitialData"].contents; data = data.twoColumnBrowseResultsRenderer || data.twoColumnSearchResultsRenderer; if (data.tabs) { data = data.tabs; data = data[1]||data[0]; // 1 - channel, 0 - feed data = data.tabRenderer.content; } else { data = data.primaryContents; } data = data.sectionListRenderer; cont = getField(data, 'continuations', 0, 'nextContinuationData', 'continuation'); track = getField(data, 'continuations', 0, 'nextContinuationData', 'clickTrackingParams'); data = data.contents[0].itemSectionRenderer.contents; if (data[0] && data[0].gridRenderer) { data = data[0].gridRenderer || data[0].shelfRenderer.content.expandedShelfContentsRenderer; cont = getField(data, 'continuations', 0, 'nextContinuationData', 'continuation'); track = getField(data, 'continuations', 0, 'nextContinuationData', 'clickTrackingParams'); //var track = data.trackingParams; data = data.items; } var html = [ '<style>body {margin:10px; font-family:'+(conf.fontFamily||'Arial')+'; font-size: '+(conf.fontSize||'16px')+'}' +'.ib {display:inline-block} .vt {width:246px; height: 200px; margin:10px; vertical-align: top}</style>' + searchform+'' ,'<h1>'+(getField(ytInitialData,'header','c4TabbedHeaderRenderer','title')||'')+'</h1>' ,'<div id=vdiv><ul id=vul>' ]; function makeLinks(data){ var html = []; for (var i=0; i<data.length; i++){ var v = data[i].gridVideoRenderer || data[i].videoRenderer; if (v) { var t = v.thumbnail.thumbnails[0]; var title = v.title.simpleText||v.title.runs[0].text; html.push('<span class="ib vt"><a href="/watch?v='+v.videoId+'" t-itle="'+title+'"><img src="'+t.url //+'" width='+t.width+'px height='+t.height+'><br>' +'" width=200px><br>' //+v.thumbnailOverlays[0].thumbnailOverlayTimeStatusRenderer.text.simpleText + (getField(v,'thumbnailOverlays',0,'thumbnailOverlayTimeStatusRenderer','text','simpleText') || "--") +' '+title+'</a></span>'); } } return html.join(''); } html.push(makeLinks(data)); function delMore(){ var tmp = document.querySelector('#more'); if (tmp) {tmp.parentNode.removeChild(tmp);} } function makeMore(cont1,track1){ window.__more_href='/browse_ajax?ctoken='+cont1+'&continuation='+cont1+'&itct='+track1 return '<button id="more" onclick="loadNextPage()" moreHref="/browse_ajax?ctoken='+cont1+'&continuation='+cont1+'&itct='+track1+'" targetId="vul">load more</button>'; } if (cont) { //html.push('<hr><button moreHref="/browse_ajax?action_continuation=1&direct_render=0&continuation='+cont+'&ctoken='+cont+'&itct='+track+'" targetId="vul">load more</button>'); html.push('<hr>'+makeMore(cont,track)); } else if (lpath[1]=='results') { window.__more_crap = ytInitialData.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[1].continuationItemRenderer.continuationEndpoint.continuationCommand.token; if (__more_crap) { html.push('<br><hr><br><button id=more onclick="nextSearch()">load more</button>'); } } html.push('</ul></div>'); document.body.innerHTML = html.join(''); if (cont||window.__more_crap) ff(); } } if (lpath[1]=='watch') { tmp = document.querySelector('#search-container'); if (tmp) tmp.innerHTML = searchform; if (window["ytInitialData"] && window["ytInitialPlayerResponse"]) { tmp = document.querySelector('#info-container'); var mf = getField(ytInitialPlayerResponse, 'microformat', 'playerMicroformatRenderer'); if (tmp && mf) tmp.innerHTML = '<a href="'+mf.ownerProfileUrl+'/videos">'+mf.ownerChannelName+'</a> '+mf.publishDate+'<br>'+s2hms(mf.lengthSeconds)+'<h3>'+mf.title.simpleText+'</h3><br><pre style="white-space:pre-wrap">'+(getField(mf, 'description', 'simpleText') || '')+'</pre>'; }; tmp = document.querySelector('#related'); var related = getField(window, 'ytInitialData', "contents", "twoColumnWatchNextResults", "secondaryResults", "secondaryResults", "results"); document.body.appendChild(create('style','','#player.skeleton.flexy #player-api {margin:0}' +'#info-container {font-size: 16px}' +'.rv {font-size: 14px; margin: 8px}' +'#watch-page-skeleton #related {max-width:200px; margin-top:10px;}' )); if (tmp && related) { var html = []; for (var i=0; i<related.length; i++){ var r = related[i].compactVideoRenderer || related[i].compactAutoplayRenderer.contents[0].compactVideoRenderer; var t = r.thumbnail.thumbnails[0]; var v = '/watch?v='+r.videoId; var pt = getField(r,'publishedTimeText','simpleText')||''; var lt = getField(r,'lengthText','simpleText')||''; html.push('<div class=rv><a href="'+v+'"><img src="'+t.url+'" width='+t.width+'px height='+t.height+'px></a><br><a href="//channel/' +r.channelId+'/videos">'+r.shortBylineText.runs[0].text+'</a> '+pt+'<br>'+lt +' <a href="'+v+'">'+r.title.simpleText+'</a><hr><br></div>'); } tmp.innerHTML = html.join(''); tmp.style.display='block'; } } var tmp = document.querySelectorAll('a[href*="youtube.com/edit?"]'); for (var i=0,a; a=tmp[i]; i++) { var id = a.href.match(/[?&]video_id=([\da-z_-]{11})/i); if (id) a.href = 'https://www.youtube.com/watch?v='+id[1]; } tmp = document.querySelector('#watch8-secondary-actions'); var id = location.search.match(/[?&]v=([\da-z_-]{11})/i); id = id&&id[1]; if (tmp && id) { tmp.innerHTML = '<input readonly size=65 value=\'<iframe width="640" height="360" src="https://www.youtube.com/embed/'+id+'?autoplay=0"></iframe>\'>'; } if (conf.fixObjects) { function fixObjects(embeds) { for (var i=0,e,o; e=embeds[i]; i++){ o=e.parentNode.tagName.toLowerCase()=='object'? e.parentNode : e; var vid = o.outerHTML.match(/youtube(?:education|-nocookie|)\.com\/v\/([\da-z_-]{11})/i); if (vid=vid&&vid[1]) { var iframe = document.createElement('iframe'); var width = o.width || o.style.width || '640px'; var height = o.height || o.style.height || '480px'; if (!/[a-zA-Z%]+$/.test(width)) width +='px'; if (!/[a-zA-Z%]+$/.test(height)) height +='px'; //iframe.style = "width: "+width+" !important ; height: "+height + ' !important'; iframe.style = "width: "+width+"; height: "+height; if (o.className) iframe.className = o.className; if (o.id) iframe.id = o.id; iframe.src = 'https://www.youtube.com/embed/'+vid; // youtube-nocookie.com not always works o.parentNode.insertBefore(iframe, o); o.parentNode.removeChild(o); log('replaced object in html: '+vid); } } } fixObjects(document.querySelectorAll('object')); fixObjects(document.querySelectorAll('embed')); var fixInsertedObjectsEnabled = (conf.fixInsertedObjects && conf.fixInsertedObjects.enabled) && !isYoutube && (window != window.top ? document.body.innerHTML.length>0 : true) // scriptweeder iframe && (!conf.fixInsertedObjects.whitelist || (!conf.fixInsertedObjects.whitelist.length || findHost(conf.fixInsertedObjects.whitelist, location.hostname)!=-1) ) && (!conf.fixInsertedObjects.blacklist || (!conf.fixInsertedObjects.blacklist.length || findHost(conf.fixInsertedObjects.blacklist, location.hostname)==-1) ); if (fixInsertedObjectsEnabled) { log('warning: fixInsertedObjects may break sites'); document.addEventListener('DOMNodeInserted', function(e){ if (e.target.innerHTML && /youtube(education|-nocookie|)\.com\/v\//.test(e.target.innerHTML)) { log('DOMNodeInserted'); var tmp = e.target.outerHTML.replace(/<object[\s\S]*?youtube(?:education|-nocookie|)\.com\/v\/([\da-z_-]{11})[\s\S]*?<\/object>/ig, function(o,id){ var tmp = o.match(/^<object[\s\S]*?>/i)[0]; var width = tmp.match(/width\s*[:=]\s*(\d\w*)/); width = width&&width[1] || '640px'; var height = tmp.match(/height\s*[:=]\s*(\d\w*)/); height = height&&height[1] || '480px'; if (!/[a-zA-Z%]+$/.test(width)) width +='px'; if (!/[a-zA-Z%]+$/.test(height)) height +='px'; return '<iframe src="https://www.youtube.com/embed/'+id+'" style="width: '+width+' !important; height: '+height+' !important"></iframe>'; }); if (e.target.outerHTML!= tmp) e.target.outerHTML = tmp; } }, false); } } if (isYoutube && conf.loadThumbnails) { var pics = document.querySelectorAll('img[data-thumb]'); for (var i=0; i<pics.length; i++) pics[i].src = pics[i].getAttribute('data-thumb'); } if (conf.loadComments && conf.blockYoutubeScripts && isYoutube && location.pathname=='/watch' && window==window.top) { var tmp = document.querySelector('#watch-discussion'); if (!tmp) { log('no #watch-discussion'); } else { tmp.style.display='none'; var comments = create('div', 'crutch-comments-container'); comments.appendChild(create('div', 'crutch-comments-total', 'comments disabled ?')) tmp.parentNode.insertBefore(comments, tmp); var headers = [['Content-Type', 'application/x-www-form-urlencoded'],['X-YouTube-Identity-Token'," "]]; var c = xhr('/watch_fragments2_ajax?v='+videoId+'&tr=scroll&distiller=1&ctoken='+window.yt.config_.COMMENTS_TOKEN+'&frags=comments&spf=load', 'POST' , 'session_token='+encodeURIComponent(window.yt.config_.XSRF_TOKEN)+'&client_url='+location, false, headers); if (c.indexOf('{')!=-1) { c = JSON.parse(c); //alert(JSON.stringify(c,null,'\t')); //alert(c.content_html); comments.innerHTML = c.content_html; log('comments'); } } } if (conf.blockYoutubeScripts && isYoutube && location.pathname=='/watch' && window==window.top) { var s = document.createElement('style'); s.appendChild(document.createTextNode('#watch-description + .yt-uix-expander-collapsed-body {display:none !important} .yt-uix-expander-collapsed #watch-description-text {max-height: 100% !important}')); document.body.appendChild(s); } var id = window.yt && yt.config_ && yt.config_.VIDEO_ID; if (conf.addLinksInEmbeds && !location.pathname.indexOf('/embed/') && id) { var pics = id && window.yt.config_.PLAYER_CONFIG.args || {}; var img = pics.iurlmaxres || pics.iurlhq720 || pics.iurlsd || pics.iurl || 'https://i.ytimg.com/vi/'+id+'/hqdefault.jpg'; var span = document.createElement('span'); span.innerHTML = ' <a href="https://www.youtube.com/watch?v='+id+'" target="_blank">link</a> <a href="'+img+'" target="_blank">img</a>'; span.style='position:absolute; top:0; left:0; background:white'; document.body.appendChild(span); } function randomString(len,alpha){ var s = ''; var s2; while (s.length<len) { s2=Math.random().toString(36).substr(2); if (alpha) s2=s2.replace(/[^a-z]/g,''); s+=s2; } return s.substr(0,len); } function randomNumber(n1,n2){ var r = Math.random() if (!n1) return r; if (!n2) return r*n1|0; return (r*(n2-n1)+n1)|0; } function randomName(n1,n2){ var s = randomString(randomNumber(n1,n2),true); return s[0].toUpperCase()+s.substr(1); } var tz = randomNumber(2,12)+'00'; var sh=randomNumber(1024,2048), sw=randomNumber(1024,4096); var bih = (sh*0.6+randomNumber(sh*0.1))|0, biw = (sw*0.9+randomNumber(sw*0.05))|0; var brdim = randomNumber(13); brdim = [0-brdim,0-brdim,0-brdim,0-brdim,sw,sw+brdim*2,sh+brdim*2,sw-randomNumber(2,13),bih].join('%2C'); var date = new Date(); var ymd = date.getFullYear()+padZero(date.getMonth()+1,2)+padZero(date.getDate(),2); //alert(ymd); //X-YouTube-Client-Version: 2.20201103.05.00 //X-YouTube-Page-Label: youtube.ytfe.desktop_20201102_5_RC0 var headersCont = [ ['X-YouTube-Client-Name','1'] ,['X-YouTube-Client-Version', '2.'+ymd+'.00.00'] //,['X-YouTube-Client-Version', '2.20201103.05.00'] ,['X-YouTube-Device','cbr=Firefox&cbrver=68.0&ceng=Gecko&cengver=68.0&cos=Windows&cosver=6.1'] ,['X-YouTube-Page-CL', randomNumber(326505813,340336202)] ,['X-YouTube-Page-Label','youtube.ytfe.desktop_'+ymd+'_0_RC0'] ,['X-YouTube-Variants-Checksum','49a40afdb89281a8ab56a2aa5b0cab6d'] ,['X-YouTube-Utc-Offset',tz] ,['X-YouTube-Time-Zone',randomName(4,14)+'/'+randomName(4,20)] ,['X-YouTube-Ad-Signals','dt='+Date.now()+'&flash='+randomNumber(2)+'&frm='+randomNumber(2)+'&u_tz='+tz+'&u_his='+randomNumber(4)+'&u_java&u_h='+sh+'&u_w='+sw+'&u_ah='+sh+'&u_aw='+sw+'&u_cd='+randomNumber(25)+'&u_nplug='+randomNumber(2)+'&u_nmime='+randomNumber(3)+'&bc='+randomNumber(30)+'&bih='+bih+'&biw='+biw+'&brdim='+brdim+'&vis='+randomNumber(2)+'&wgl=true&ca_type=image'] ,['X-SPF-Referer',''+location] ,['X-SPF-Previous',''+location] ]; nextSearch = function() { var shit = {"context":{"client": {"clientName": "WEB","clientVersion": "2."+ymd+".00.00"}}, "continuation": __more_crap}; var tmp = xhr("/youtubei/v1/search?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8", 'POST', JSON.stringify(shit), false); //alert(tmp); tmp = JSON.parse(tmp); tmp = tmp.onResponseReceivedCommands[0].appendContinuationItemsAction.continuationItems; __more_crap = getField(tmp,1,'continuationItemRenderer','continuationEndpoint','continuationCommand','token'); tmp = tmp[0].itemSectionRenderer.contents; var more = document.querySelector('#more'); more.parentNode.removeChild(more); //alert(JSON.stringify(tmp,null,'\t')); document.querySelector('#vul').innerHTML+= makeLinks(tmp)+(__more_crap?'<br><hr><br><button id=more onclick="nextSearch()">load more</button>':''); } // load more videos if (conf.blockYoutubeScripts && isYoutube) { var more = document.querySelector('button#more'); if (more) { loadNextPage = function(){ delMore(); //var next = more.getAttribute('moreHref'); // old link all the time! var next = window.__more_href; //alert(next); //var targetid = this.getAttribute('targetId'); if (next) { //xhr(url, 'POST', post, false, headers); var tmp = xhr(next, 'GET', false, false, headersCont); //alert(tmp); tmp = JSON.parse(tmp); //alert(JSON.stringify(tmp,null,'\t')); tmp = tmp[1].response.continuationContents.gridContinuation; var cont = getField(tmp,'continuations',0,'nextContinuationData'); //"nextContinuationData": {"continuation": "4q","clickTrackingParams": "CA" } var html = '<hr>'+makeLinks(tmp.items); if (cont && cont.continuation) { html += '<hr>'+makeMore(cont.continuation, cont.clickTrackingParams); } document.querySelector('#vul').innerHTML+= html; //var more = document.querySelector('#more'); } } } } // first video in channel var upsell = document.querySelector('#upsell-video'); if (upsell) { id = upsell.getAttribute('data-video-id'); if (id) upsell.innerHTML = '<a href="/watch?v='+id+'"><img src="https://i.ytimg.com/vi/'+id+'/mqdefault.jpg"></a>'; } },false); })(opera) |