Last active
August 7, 2022 14:10
-
-
Save samunders-core/d180d4e3e28a84c48daf5cfba5b4f40a to your computer and use it in GitHub Desktop.
This GreaseMonkey / TamperMonkey / ViolentMonkey user script redirects downloads from https://uloz.to to speciffied Aria2 instance
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 uloz.to Aria2 bridge | |
// @namespace Violentmonkey Scripts | |
// @match https://uloz.to/* | |
// @run-at document-start | |
// @grant GM.xmlHttpRequest | |
// @version 1.0 | |
// @author sam_ | |
// @description Redirects downloads from uloz.to to speciffied Aria2 instance | |
// ==/UserScript== | |
function windowOpen(url, name, features) { | |
var aria2 = "https://192.168.1.1:6800/jsonrpc"; | |
var secret = "TODO: You should setup here your own"; | |
if (!url.includes("download.uloz.to")) { | |
return window.open(url, name, features); | |
} | |
var headers = [ | |
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0", | |
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | |
"Accept-Language: sk,cs;q=0.8,en-US;q=0.5,en;q=0.3", | |
"Accept-Encoding: gzip, deflate, br", | |
"Referer: " + document.evaluate('//meta[@property="og:url"]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.getAttribute("content"), | |
"Cookie: " + document.cookie, | |
"Upgrade-Insecure-Requests: 1" | |
]; | |
var id = document.getElementsByName("timestamp"); | |
var req = new XMLHttpRequest(); | |
// req.onload = /*console.log;*/ function(response) { | |
// console.log(response); | |
// alert(response.toString()); | |
// }; | |
// req.onabort = req.onload; | |
// req.onerror = req.onload; | |
req.open("POST", aria2); | |
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); | |
req.send(JSON.stringify({ | |
jsonrpc: "2.0", id: id && id.length > 0 ? id[0].value : Date.now().toString(), method: "aria2.addUri", params: [ | |
"token:" + secret, [url], {header: headers} | |
] | |
})); | |
} | |
function console_log() { | |
//console.log.apply(null, arguments); | |
} | |
function newScript(text) { | |
var newScript = document.createElement("script"); | |
newScript.type = "text/javascript"; | |
newScript.textContent = text; | |
return newScript; | |
} | |
var postponed = null; | |
window.addEventListener('beforescriptexecute', function(e) { | |
src = e.target.src; | |
if (postponed != null) { | |
console_log("postponing " + (src.length > 0 ? src : e.target.innerHTML)); | |
postponed.push(e.target); | |
e.preventDefault(); | |
e.stopPropagation(); | |
} else if (src.includes("ulozto.min.js")) { | |
console_log("loading " + src); | |
postponed = []; | |
e.preventDefault(); | |
e.stopPropagation(); | |
GM.xmlHttpRequest({ | |
method: "GET", | |
url: e.target.src, | |
onload: function(response) { | |
console_log("" + postponed.length + " postponed, prepending " + windowOpen + " and patching all occurences of window.open to windowOpen"); | |
response.responseText = "" + windowOpen + "\n" + response.responseText.replace(/\bwindow[.]open\b/g, "windowOpen"); | |
var targets = postponed; | |
postponed = null; | |
addPostponed(response.responseText, targets); | |
} | |
}); | |
} else { | |
console_log("Before execution of " + (src.length > 0 ? src : e.target.innerHTML)); | |
} | |
}); | |
function addPostponed(first, rest) { | |
if (first.startsWith("http")) { | |
console_log("loading " + first + ", " + rest.length + " postponed"); | |
if (rest.length > 0) { | |
postponed = rest; | |
} | |
GM.xmlHttpRequest({ | |
method: "GET", | |
url: first, | |
onload: function(response) { | |
var targets = postponed == null ? [] : postponed; | |
postponed = null; | |
addPostponed(response.responseText, targets); | |
} | |
}); | |
} else { | |
var head = document.getElementsByTagName('head')[0]; | |
if (rest.length > 0) { | |
if (head == rest[0].parentNode) { | |
head.insertBefore(newScript(first), rest[0]); | |
} else { | |
head.appendChild(newScript(first)); | |
} | |
first = rest.shift(); | |
addPostponed(first.src.length > 0 ? first.src : first.innerHTML, rest); | |
} else { | |
head.appendChild(newScript(first)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment