Last active
September 8, 2020 20:24
-
-
Save brodul/21449d416ac68664bbfe741a73afe4f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Huble Bundle Books | |
// @namespace http://brodul.org | |
// @version 0.1 | |
// @description Downloads books in a format from huble bundle | |
// @author Andraz Brodnik brodul | |
// @match https://www.humblebundle.com/* | |
// @grant GM_download | |
// @run-at document-idle | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function getElementByXpath(path) { | |
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; | |
} | |
// wait until the select element appears | |
var checkExist = setInterval(function() { | |
if (document.getElementById("switch-platform")) { | |
clearInterval(checkExist); | |
main() | |
} | |
}, 100); | |
// main function | |
function main() { | |
var platformSelect = document.getElementById("switch-platform"); | |
platformSelect.addEventListener("change", (e) => { | |
var selectedOption = e.target.options[e.target.selectedIndex].value; | |
if (selectedOption === "ebook") { | |
render(); | |
} else { | |
remove(); | |
} | |
}); | |
} | |
function render() { | |
let el = document.createElement("div"); | |
var downLoadAll = document.createElement("h4"); | |
downLoadAll.innerHTML = "DOWNLOAD ALL"; | |
el.className = "container" | |
el.id = "tmp"; | |
el.style.cursor = "pointer"; | |
el.appendChild(downLoadAll); | |
el.addEventListener("click", download); | |
document.getElementsByClassName("header")[0].appendChild(el); | |
} | |
function remove() { | |
document.getElementById("tmp").remove(); | |
} | |
function download() { | |
var books = document.getElementsByClassName("column subproducts-holder js-subproducts-holder")[0]; | |
Array.from(books.children).forEach( (el, i) => { | |
setTimeout(() => { | |
el.click(); | |
setTimeout(() => getElementByXpath("//h4[contains(text(),'MOBI')]/..").click(), 1000); | |
setTimeout(() => getElementByXpath("//h4[contains(text(),'EPUB')]/..").click(), 3000); | |
// setTimeout(() => getElementByXpath("//h4[contains(text(),'PDF')]/..").click(), 4000); | |
}, 15000*i); | |
}); | |
// console.log(books); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment