Last active
February 6, 2018 11:30
-
-
Save buzamahmooza/dccc1825be7e6a75127883a899e3a9a9 to your computer and use it in GitHub Desktop.
Add a magnet link shortcut and thumbnails of torrents. Also adds a google image search link in case you want to see more pics of the torrent.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name RARBG - Add Magnet Link & thumbnails! | |
// @namespace https://github.com/buzamahmooza | |
// @version 0.5.3 | |
// @description Add a magnet link shortcut and thumbnails of torrents. | |
// @description Also adds a google image saerch link in case you want to see more pics of the torrent. | |
// @author Cisco, forked by Faris Hijazi | |
// @include https://rarbg.to/* | |
// @include http://rarbgmirror.xyz/* | |
// @include https://rarbgunblock.com/* | |
// @include https://rarbg.unblocked.st/* | |
// @include https://*rarbg* | |
// @grant unsafeWindow | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @updateUrl https://gist.github.com/buzamahmooza/dccc1825be7e6a75127883a899e3a9a9/raw/ | |
// @require http://code.jquery.com/jquery-latest.js | |
// @require https://unpkg.com/in-view@0.6.1/dist/in-view.min.js | |
// Original link 1: https://greasyfork.org/scripts/12648-rarbg-add-magnet-link/code/RARBG%20-%20Add%20Magnet%20Link.user.js | |
// Original link 2: https://greasyfork.org/scripts/23493-rarbg-torrent-and-magnet-links/code/RARBG%20-%20torrent%20and%20magnet%20links.user.js | |
// ==/UserScript== | |
// ideas: | |
// [ ] make a link that downloads all the images on the torrent page via xml request | |
// [ ] sound on mouse hover | |
// [ ] | |
const DBG = false; // debug mode (setting this to false will disable the console logging) | |
var currentDocument = document; | |
const SEARCH_ICON_URL = "https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-search-strong-128.png"; | |
const TORRENT_DL_ICO = "https://dyncdn.me/static/20/img/16x16/download.png"; | |
const MAGNET_ICO = "https://dyncdn.me/static/20/img/magnet.gif"; | |
const GoogleImagesSearchURL = "https://encrypted.google.com/search?&hl=en&tbm=isch&q="; | |
const trackers = 'http%3A%2F%2Ftracker.trackerfix.com%3A80%2Fannounce&tr=udp%3A%2F%2F9.rarbg.me%3A2710&tr=udp%3A%2F%2F9.rarbg.to%3A2710'; | |
// "http://icons.iconarchive.com/icons/custom-icon-design/mono-general-2/512/search-icon.png"; // search icon | |
const useGoogleImages = true; | |
function getDdgSearchURL(q){return "https://duckduckgo.com/?q=" + encodeURIComponent(q) + "&atb=v73-5__&iar=images&iax=images&ia=images";} | |
var tbodyEl = document.querySelector('body > table:nth-child(6) > tbody > tr > td:nth-child(2) > div > table > tbody > tr:nth-child(2) > td > table.lista2t > tbody'); | |
const bigPics = false; | |
const nextPage = document.querySelector('a[title="next page"]'), | |
prevPage = document.querySelector('a[title="previous page"]'); | |
var usingSmallThumbnails = GM_getValue('usingSmallThumbnails', true); | |
//if on a single torrent page, it gets special treatment | |
if(matchSite(/\/torrent\//)){ | |
var torrents = document.querySelectorAll('tr.lista_related td:nth-child(1) [href^="/torrent/"]'); | |
for (var i = 0; i < torrents.length; i++) { | |
let thb = getMouseoverThumbnail(torrents[i]); | |
//creating and adding the elements | |
var cell = document.createElement('td'), | |
magnetLink = document.createElement('a'), | |
magnetImg = document.createElement('img'); | |
magnetLink.href = torrents[i].href; | |
cell.classList.add('magnet-cell'); | |
cell.appendChild(magnetLink); | |
// thumbnail | |
magnetImg.classList.add('preview-image'); | |
magnetImg.src = thb; | |
magnetLink.appendChild(magnetImg); | |
torrents[i].parentNode.parentNode.after(cell, torrents[i].parentNode); | |
} | |
if(DBG) console.log('You should see '+i+' thumbnails now.'); | |
return; | |
} | |
inView('#pager_links').on('enter', function(){ | |
appendPage(currentDocument.querySelector('a[title="next page"]')); | |
}); | |
clickToVerifyBrowser(); | |
var addedColumnHeader = false; | |
var title = 'DL ML'; | |
var mls = appendColumn(); | |
// try{ document.querySelector('body > table:nth-child(6) > tbody > tr > td:nth-child(2) > div > table > tbody > tr:nth-child(2) > td > table.lista2t').style.width = "1300px"; }catch(exception){} | |
var magnetImgData = 'data:image/svg+xml;base64,'; | |
let width = 150, maxwidth = 150; | |
width = bigPics ? width*2 : width; | |
maxwidth = bigPics ? maxwidth*2 : maxwidth; | |
let maxheight = 300; | |
var cssRules = '' + | |
'.magnet-cell { text-align: center; } ' + | |
'.magnet-cell a img { transition: transform 250ms ease; width:'+width+'px; max-width:'+maxwidth+'max-width:'+maxheight+';translate3d(0, 0, 100); padding:5px 5px; } ' + | |
// '.magnet-cell a img { transition: transform 250ms ease; width:100px; max-width:150;translate3d(0, 0, 100); padding:5px 5px; } ' + | |
'.magnet-cell a:hover img { transform: scale(1.6, 1.6); } ' + | |
'.a.gs a: hover img { transform: scale(1.3, 1.3); } ' + | |
'a.gs:link {color: red;}'+ | |
'a.gs:visited {color: green;}'+ | |
'a.gs:hover {color: hotpink;}'+ | |
'a.gs:active {color: blue;}'+ | |
'a.gs: {font-size: 8px; padding:5px 5px;}'; | |
var cssBlock = document.createElement('style'); | |
cssBlock.innerText = cssRules; | |
fixCss(); | |
document.getElementsByTagName('head')[0].appendChild(cssBlock); | |
var headCell = document.createElement('td'); | |
headCell.innerHTML = 'Thumbnails'; | |
headCell.classList.add('header6'); | |
headCell.classList.add('header40'); | |
// Adds the click functionality to the Thumbnails header | |
headCell.addEventListener('click', function(e) { toggleThumbnailSize(); }); | |
var torrents = []; | |
observeDocument(dealWithTorrents); | |
function observeDocument(callback) { | |
callback(document.body); | |
new MutationObserver(function(mutations){ | |
for (var i = 0; i < mutations.length; i++) { | |
if (!mutations[i].addedNodes.length) continue; | |
callback(mutations[i].target); | |
} | |
}).observe(document.body, { | |
childList: true, subtree: true, | |
attributes: false, characterData: false | |
}); | |
} | |
document.querySelector('.lista2t tr:first-child td:nth-child(2)').parentNode.insertBefore(headCell, document.querySelector('.lista2t tr:first-child td:nth-child(2)')); | |
function dealWithTorrents(node) { | |
torrents = node.querySelectorAll('.lista2 td:nth-child(2) [href^="/torrent/"]'); | |
if(DBG) console.log('torrents.length: '+torrents.length); | |
for (var i=0; i<torrents.length; i++) { | |
//creating and adding the elements | |
var cell = document.createElement('td'), | |
magnetLink = document.createElement('a'), | |
magnetImg = document.createElement('img'); | |
try{ | |
magnetLink.href = | |
getTorrentDownloadLinkFromAnchor(torrents[i]); | |
// getTorrentDownloadLink(torrents[i].parentNode); | |
}catch(e){ magnetLink.href = mls[i]; if(DBG) console.log('Using mls for MagnetLink:', mls[i]); } | |
// magnetLink.href = mls[i]; | |
cell.classList.add('magnet-cell'); | |
cell.appendChild(magnetLink); | |
cell.style = "height: 200px; width: 301px;"; | |
// thumbnail | |
magnetImg.classList.add('preview-image'); | |
let thumb = getMouseoverThumbnail(torrents[i]); | |
createAndAddAttribute(magnetImg, 'smallSrc', thumb); | |
createAndAddAttribute(magnetImg, 'bigSrc', getLargeThumbnail(thumb)); | |
setThumbnail(magnetImg); | |
magnetLink.appendChild(magnetImg); | |
torrents[i].parentNode.parentNode.insertBefore(cell, torrents[i].parentNode); | |
} | |
torrents.forEach(function(e){ addImageSearchAnchor(e); }); | |
} | |
function setThumbnail(magnetImg){ | |
magnetImg.src = magnetImg.getAttribute((usingSmallThumbnails ? 'smallSrc' : 'bigSrc')); | |
} | |
function toggleThumbnailSize(){ | |
usingSmallThumbnails = !usingSmallThumbnails; | |
GM_setValue('usingSmallThumbnails', usingSmallThumbnails); | |
document.querySelectorAll('.preview-image').forEach(function(e){ | |
setThumbnail(e); | |
}); | |
fixCss(); | |
if(DBG) console.log('toggling thumbnail sizes. usingSmallThumbnails = ', usingSmallThumbnails); | |
} | |
function fixCss(){ | |
cssBlock.innerText = '' + | |
'.magnet-cell { text-align: center; } ' + | |
'.magnet-cell a img { transition: transform 250ms ease; width:'+ (usingSmallThumbnails ? width : width*2) +'px; max-width:' + | |
(usingSmallThumbnails ? width : maxwidth*2) + ';translate3d(0, 0, 100); padding:5px 5px; } ' + | |
'.magnet-cell a:hover img { transform: scale(1.3, 1.3); } ' + | |
'.a.gs a: hover img { transform: scale(1.3, 1.3); } ' + | |
'a.gs:link {color: red;}'+ | |
'a.gs:visited {color: green;}'+ | |
'a.gs:hover {color: hotpink;}'+ | |
'a.gs:active {color: blue;}'+ | |
'a.gs: {font-size: 8px; padding:5px 5px;}' | |
; | |
} | |
function tryBigImage(img){ | |
img.addEventListener("error", bigThumbHandleError); | |
img.oldSrc = img.src; | |
img.src = getLargeThumbnail(img.src); | |
} | |
function bigThumbHandleError(){ | |
this.removeEventListener("error", bigThumbHandleError); | |
this.src = this.oldSrc; | |
console.warn('Failed to load big thumbnail for', this.src, ' Attempting xml request'); | |
addMagnetCell(this.parentNode); | |
} | |
// gets the large thumbnail from the small thumbnail (works for rarbg thumbnails) | |
function getLargeThumbnail(smallThumbUrl){ | |
// Movie example | |
// Small pic: http://dyncdn.me/mimages/316661/over_opt.jpg | |
// Big pic: http://dyncdn.me/mimages/316661/poster_opt.jpg | |
return smallThumbUrl | |
.replace('over_opt','poster_opt') // movie thumbnail replacement | |
// other thumbnail replacement | |
.replace ( 'static/over', 'posters2/'+smallThumbUrl.replace(/(.*?)over\//,'').charAt(0) ) //put "posters2" + the first character after the '/' | |
; | |
} | |
function createAndAddAttribute(node, attributeName, attributeValue){ | |
if(!node) { console.error('Node is null, cannot add attribute.'); return; } | |
var att = document.createAttribute(attributeName); | |
att.value = attributeValue; | |
node.setAttributeNode(att); | |
} | |
/** @Return returns the link from the 'onmouseover' attribute */ | |
function getMouseoverThumbnail(node){ | |
if(!node)console.warn('null torrent anchor:', node); | |
let thb = ''; | |
try { | |
// thb = thb.match(/(?<=(return overlib('\<img src\=\')))(.*?)(?=(\' border\=0\>\'))/i)[0]; | |
thb = node.getAttribute('onmouseover'); | |
thb = thb.substring("return overlib('<img src=\'".length+1, thb.length - "\' border=0>'".length-2); | |
} catch(r) { | |
thb = magnetImgData; | |
if(DBG) console.error('getMouseoverThumbnail error:', r); | |
// console.log("node with error:\t"+node); | |
} | |
return thb; | |
} | |
function addImageSearchAnchor(torrentAnchor){ | |
var searchTd = document.createElement('td'), | |
searchLink = document.createElement('a'), | |
frame = document.createElement('iframe'), | |
searchImg = document.createElement('img') | |
; | |
searchTd.style = "border-top-width: 10px; padding-top: 10px;"; | |
let q = torrentAnchor.title | |
.replace(/rarbg|\.com|\#|x264|DVDRip|720p|1080p|2160p|MP4|IMAGESET|FuGLi|SD|KLEENEX|BRRip|XviD|MP3|XVID|BluRay|HAAC|WEBRip|DHD|rartv|KTR|YAPG/gi,"") //replacing common useless torrent terms | |
.replace(/(\d+\.(\d+)\.(\d+))|[\d]{3,}/g," ") // replace dates (numbers with dots between them) | |
.replace(/[^\w\d]|_/g," ") // replaces non-alpha and non-word characters | |
.replace(/\s\s+/g,' ') // removes double spaces | |
.trim() | |
; | |
frame.src = getDdgSearchURL(q); | |
frame.height="200"; | |
frame.width="300"; | |
searchLink.href = (useGoogleImages) ? (GoogleImagesSearchURL + q) : getDdgSearchURL(q); | |
searchLink.classList.add('gs'); | |
searchLink.target = "_blank"; | |
let showGeneratedSearchQuery = true; | |
// searchLink.innerHTML = ((useGoogleImages) ? "Google" : "DDG") + " Image Search"; | |
var searchEngineText = document.createElement('p5'); | |
var qText = document.createElement('p6'); | |
searchEngineText.innerHTML = (((useGoogleImages) ? "Google" : "DDG") + " Image Search"); | |
qText.innerHTML = (showGeneratedSearchQuery)?':\t'+q: ''; | |
let searchIcon = document.createElement('img'); | |
searchIcon.src = SEARCH_ICON_URL; | |
searchIcon.style.height = '20px'; | |
searchIcon.style.width = '20px'; | |
searchLink.style.padding = '20px'; | |
searchTd.appendChild(searchLink); | |
searchLink.before(searchIcon); | |
searchLink.appendChild(searchEngineText); | |
searchLink.appendChild(qText); | |
torrentAnchor.after(searchTd); | |
} | |
function appendPage(pageLink) { | |
tb = document.createElement('tr'); | |
pageAnchor = createElement('<td><a href="'+pageLink+'"><p1>Go to page</p1></a></td>'); | |
tb.appendChild(pageAnchor); | |
tbodyEl.appendChild(tb); | |
var url = pageLink; | |
var req = new XMLHttpRequest(); | |
req.open('GET', url); | |
req.send(); | |
req.onreadystatechange = function() { | |
if (req.readyState == 4) { | |
var pageHTML = req.responseText; | |
currentDocument = document.createElement('html'); | |
currentDocument.innerHTML = pageHTML; | |
var lista2s = currentDocument.querySelectorAll('tbody>.lista2'); | |
if(DBG) console.log('lista2s:', lista2s); | |
lista2s.forEach(function(e){ | |
tbodyEl.appendChild(e); | |
appendColumnSingle(e.childNodes[1]); | |
}); | |
} | |
}; | |
} | |
function addMagnetCell(torrent){ | |
var url = torrent.href; | |
var req = new XMLHttpRequest(); | |
req.open('GET', url); | |
req.send(); | |
req.onreadystatechange = function() { | |
if (req.readyState == 4) { | |
var pageHTML = req.responseText; | |
var magnetURL = pageHTML.match(/href\=\"(magnet[\:\_\-\+\%\?\=\&\;\.0-9a-zA-Z]*)\"/)[1]; //match magnet URL | |
var thumbURLs = pageHTML.match("src\=\"((.?)*\.jpg)\""); | |
//creating and adding the elements | |
var magnetCell = document.createElement('td'), | |
magnetLink = document.createElement('a'), | |
magnetImg = document.createElement('img'); | |
magnetLink.href = magnetURL; | |
magnetCell.classList.add('magnet-cell'); | |
magnetCell.appendChild(magnetLink); | |
magnetImg.src = thumbURLs[1]; | |
if(!thumbURLs[1]) magnetImg = magnetImgData; | |
magnetLink.appendChild(magnetImg); | |
torrent.parentNode.parentNode.replaceChild(magnetCell, torrent.parentNode.parentNode.childNodes[1]); | |
} | |
}; | |
} | |
function clickToVerifyBrowser(){ | |
document.querySelectorAll('a[href^="/threat_defence.php?defence=1"]').forEach(function(a){ | |
a.click(); | |
}); | |
} | |
window.onkeyup = function(e) { | |
var key = e.keyCode ? e.keyCode : e.which; | |
switch(key){ | |
case 37: // Left arrowKey | |
e.preventDefault(); | |
prevPage.click(); | |
break; | |
case 39:// Right arrowKey | |
e.preventDefault(); | |
nextPage.click(); | |
break; | |
case 192: // ' ` ' key | |
toggleThumbnailSize(); | |
break; | |
} | |
}; | |
/** Create an element by typing it's inner HTML. | |
example: var myAnchor = createElement('<a href="https://example.com">Go to example.com</a>');*/ | |
function createElement(html) { | |
var div = document.createElement('div'); | |
div.innerHTML = html; | |
return div.childNodes[0]; | |
} | |
unsafeWindow.dlTorrents = dlTorrents; | |
function dlTorrents() { console.log('dlTorrents'); | |
if(confirm('Would you like to download all the torrents on the page?')) | |
document.querySelectorAll(".torrent_dl").forEach(function(e, i) { | |
e.click(); | |
console.log(i, e.href); | |
}); | |
} | |
// the magnet and url buttons | |
// @source: https://greasyfork.org/scripts/23493-rarbg-torrent-and-magnet-links/code/RARBG%20-%20torrent%20and%20magnet%20links.user.js | |
function fixCell(cell){ | |
cell.setAttribute('class', 'lista'); | |
cell.setAttribute('width', '50px'); | |
cell.setAttribute('align', 'center'); | |
} | |
function appendColumn() { | |
var array = []; | |
// the initial column 'Files' after of which the extra column will be appended | |
// document.querySelectorAll('.lista2t > tbody > tr > td:nth-child(2) ').forEach(function(entry){ // creation of the extra column | |
// entry.insertAdjacentHTML('afterend', `<td>` + title + `</td>`); | |
// }); | |
// add header | |
if(!addedColumnHeader) { | |
var nearHeader = document.querySelector('.lista2t > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(3)'); // the first cell (header cell) of the new column | |
var header = nearHeader.cloneNode(false); | |
nearHeader.before(header); | |
nearHeader = undefined; // clear memory | |
header.setAttribute('class', 'header6'); | |
header.setAttribute('align', 'center'); | |
header.setAttribute('id', 'DL-header'); | |
var dlAnchor = createElement('<a>DL</a>'); | |
dlAnchor.addEventListener('click', dlTorrents); | |
header.appendChild(dlAnchor); | |
addedColumnHeader = true; | |
} | |
// the rest cells of the new column | |
document.querySelectorAll('.lista2t > tbody > tr[class="lista2"] > td:nth-child(3)').forEach(fixCell); | |
var oldColumn = document.querySelectorAll('.lista2t > tbody > tr[class="lista2"] > td:nth-child(2)'); // torrent links | |
// populate the cells in the new column with DL and ML links | |
oldColumn.forEach(function(col) { | |
array.push(appendColumnSingle(col)); | |
}); | |
return array; | |
} | |
function addDlAndMl(torrentAnchor, cellNode) { | |
torrentAnchor.innerHTML = '<a href="' + getTorrentDownloadLink(cellNode) + ' class="torrent_dl" target = "_blank" >' + | |
'<img src="' + TORRENT_DL_ICO + '"">' + '</>'; // torrent download | |
// matches anything containing "over/*.jpg" *: anything | |
var hash = (/over\/(.*)\.jpg\\/).test(cellNode.firstChild.outerHTML) ? cellNode.firstChild.outerHTML.match(/over\/(.*)\.jpg\\/)[1] : undefined; | |
let title = cellNode.firstChild.innerText; | |
torrentAnchor.innerHTML += (hash !== undefined) ? | |
(' <a class="torrent_ml" href="magnet:?xt=urn:btih:' + hash + '&dn=' + title + '&tr=' + trackers + ' ">' + '<img src="' + MAGNET_ICO + '""></>'): | |
' '; // magnet ink | |
return ('magnet:?xt=urn:btih:' + hash + '&dn=' + title + '&tr=' + trackers); | |
} | |
function getTorrentDownloadLink(oldColumn){ | |
return oldColumn.firstChild.href.replace('torrent/', 'download.php?id=') + '&f=' + oldColumn.firstChild.innerText + '-[rarbg.com].torrent"'; | |
} | |
function getTorrentDownloadLinkFromAnchor(anchor){ | |
return anchor.href.replace('torrent/', 'download.php?id=') + '&f=' + anchor.innerText + '-[rarbg.com].torrent'; | |
} | |
function appendColumnSingle(torrentAnchor) { | |
if(torrentAnchor.classList.contains('has-torrent-DL-ML')) return; | |
// the initial column 'Files' after of which the extra column will be appended | |
// creation of the extra column | |
torrentAnchor.insertAdjacentHTML('afterend', `<td>` + title + `</td>`); | |
torrentAnchor.classList.add('has-torrent-DL-ML'); | |
// add header | |
if(!addedColumnHeader) (function (){ | |
var header = document.querySelector('.lista2t > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(3)'); // the first cell (the header cell) of the new column | |
header.setAttribute('class', 'header6'); | |
header.setAttribute('align', 'center'); | |
var dlAnchor = createElement('<a>DL</a>'); | |
header.addEventListener('click', dlTorrents); | |
header.appendChild(dlAnchor); | |
addedColumnHeader = true; | |
})(); | |
// the rest cells of the new column | |
fixCell(torrentAnchor.nextSibling); | |
var oldColumn = torrentAnchor; // torrent links | |
// populate the cells in the new column with DL and ML links | |
return(addDlAndMl(oldColumn.nextSibling, oldColumn)); | |
} | |
function matchSite(siteRegex){ | |
let result = location.href.match(siteRegex); | |
if(result) if(DBG) console.log("Site matched regex: "+siteRegex); | |
return result; | |
} | |
function loadBigPics(){ | |
fixCss(); | |
// Advised not to use as it uses XML requests and will get you banned form the site if you use it for a few pages. | |
document.querySelectorAll('.preview-image').forEach(function(e){ | |
tryBigImage(e); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added thumbnail support for single page torrents (i.e: when a torrent page is opened).