// ==UserScript== // @name RARBG Assistant // @namespace https://github.com/PoLaKoSz // @version 1.8.2 // @description Userscript for RARBG.to to make it more user friendly. // @author Tom PoLáKoSz // @grant none // @icon https://raw.githubusercontent.com/PoLaKoSz/RARBG-Assistant/master/assets/icon.png // @run-at document-start // @include https://rarbg.to/trailers.php* // @downloadURL none // ==/UserScript== 'use strict'; class KeyDetector{isEscPressed(e){return"key"in(e=e||window.event)?"Escape"===e.key||"Esc"===e.key:27===e.keyCode}}class RarbgAssistant{constructor(){this.requestCatcher=new XMLHttpRequestCatcher,this.trailersPage=new TrailersPage}initalize(){this.requestCatcher.register("/ajax.php?mode=trailers&mode2=list",this.trailersPage),document.addEventListener("DOMContentLoaded",function(){const e=new TrailerWindow;e.float(),e.onOpen([{instance:e,method:e.addSearchButton},{instance:e,method:e.fixOverlayForAdblockers},{instance:e,method:e.hideScrollbar}]),e.onClose([{instance:e,method:e.showScrollbar}]),document.onkeydown=function(t){(new KeyDetector).isEscPressed(t)&&e.close()}})}}class TrailersPage{invoke(e){const t=e.match(/(?<=id="trailer_)(\d+)/g);if(!t){const t="Couldn't find any trailer Id!";throw console.log(t,{payload:e}),t}t.forEach(e=>{document.querySelector(`#trailer_${e}`).querySelectorAll("a").forEach(e=>{let t=e.onclick;e.onclick=function(){return t(),!1}})})}}class TrailerWindow{constructor(){this.trailerModalNode=document.querySelector("#trailer_modal"),this.openEventSubscribers=[],this.closeEventSubscribers=[]}float(){document.body.appendChild(this.trailerModalNode),this.trailerModalNode.classList.add("trailer-modal")}close(){this.getCloseFunction()("#")}onOpen(e){this.openEventSubscribers=e,this.attachEventToWindowOpen()}addSearchButton(e){const t=e.trailerModalNode.querySelector("table:nth-child(3) > tbody > tr > td:nth-child(1)"),o=t.querySelector("b"),n=`\n \n \n \n Search for ${o.innerText}\n \n \n `;t.innerHTML+=n}attachEventToWindowOpen(){const e=jQuery.fn.jqm,t=this.openEventSubscribers;jQuery.fn.jqm=function(){return arguments[0].onLoad=function(){t.forEach(e=>{e.method(e.instance)})},e.apply(this,arguments)}}fixOverlayForAdblockers(){const e=document.querySelector(".jqmOverlay");e.classList.remove("jqmOverlay"),e.setAttribute("id","jqmOverlay"),e.style.backgroundColor="#000";const t=this.instance;e.onclick=function(){t.close()}}hideScrollbar(){const e=document.body.clientWidth;document.body.style.overflow="hidden",document.body.style.width=`${e}px`}showScrollbar(){document.body.style.overflow="visible",document.body.style.width="auto"}onClose(e){this.closeEventSubscribers=e,this.attachEventToWindowClose()}attachEventToWindowClose(){const e=this.closeEventSubscribers,t=this.getCloseFunction();window.closejqm=function(){return e.forEach(e=>{e.method(e.instance)}),t.apply(this,arguments)}}getCloseFunction(){const e=window.closejqm;if(void 0===e)throw"RARBG developers removed closejqm global function!";return e}}class XMLHttpRequestCatcher{constructor(){const e=window.XMLHttpRequest.prototype.send,t=this;window.XMLHttpRequest.prototype.send=function(){const o=this,n=window.setInterval(function(){4==o.readyState&&(t.invokeActionIfExists(o),clearInterval(n))},1);return e.apply(this,[].slice.call(arguments))},this.lookupTable={}}register(e,t){e=new URL(`https://rarbg.to${e}`),this.lookupTable[e]=t}invokeActionIfExists(e){if(!e.responseURL)return;const t=new URL(e.responseURL);Object.keys(this.lookupTable).forEach(o=>{const n=new URL(o);if(n.pathname!==t.pathname)return;const s=Array.from(n.searchParams.keys()),r=Array.from(t.searchParams.keys());if(s.length>r.length)return;let a=0;s.forEach(e=>{a+=n.searchParams.get(e)===t.searchParams.get(e)}),a==s.length&&this.lookupTable[o].invoke(e.responseText)})}}const rarbgAssistant=new RarbgAssistant;rarbgAssistant.initalize();const styleNode=document.createElement("style");styleNode.type="text/css",styleNode.innerText=".trailer-modal{position:fixed!important}",document.head.appendChild(styleNode);