Created
July 25, 2016 03:22
-
-
Save wkrueger/f63f71c0aa4950431575562b65d4f77b to your computer and use it in GitHub Desktop.
Baijador de memes
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
/* eslint-env es6 */ | |
var __STOP = false | |
/** | |
* @type {HTMLElement} | |
*/ | |
var __DIVA | |
function getReactionUrls() { | |
__STOP = false | |
var imgElements = Array.from(document.querySelectorAll('.UFIRow .mvs img')) | |
var imgUrls = [] | |
STOPBTN() | |
return getUrlsP = imgElements.reduce( (chain,element) => { | |
return chain.then( () => fetchImgUrl(element) ) | |
}, Promise.resolve()).then(() => { | |
console.log(imgUrls) | |
return imgUrls | |
}) | |
function fetchImgUrl(imgElement) { | |
if (__STOP) return | |
imgElement.click() | |
var _src | |
return waitP(4000).then( () => { | |
var url = document.querySelector('.fbPhotoSnowliftContainer img').src | |
imgUrls.push(url) | |
document.querySelector('a[data-testid="xhp_fb__photos__snowliftclose"]').click() | |
return waitP(1000) | |
}) | |
} | |
function waitP(ms) { | |
return new Promise( resolve => { | |
setTimeout(() => { | |
resolve() | |
},ms) | |
}) | |
} | |
function STOPBTN() { | |
if (__DIVA) { | |
document.body.removeChild(__DIVA) | |
} | |
__DIVA = document.createElement('div') | |
__DIVA.innerHTML = 'PARA COM ISSO' | |
__DIVA.style.zIndex = '999999' | |
__DIVA.style.color = 'red' | |
__DIVA.style.position = 'fixed' | |
__DIVA.style.top = '100px' | |
__DIVA.style.left = '100px' | |
__DIVA.style.fontSize = '60px' | |
__DIVA.style.fontWeight = 'bold' | |
__DIVA.addEventListener('click', () => { | |
console.log('TA BOM EU PEÇO PINICO') | |
__STOP = true | |
}) | |
document.body.appendChild(__DIVA) | |
} | |
} | |
function downloadReactions(urls) { | |
console.log('tentando baixar o.o') | |
var all = urls.map( url => { | |
return jqueryGet(url).then( blob => { | |
saveData(blob, url.substr(-20) + '.jpg') | |
}) | |
}) | |
return Promise.all(all).then( () => { | |
console.log('eu acho que terminou!') | |
}) | |
function jqueryGet(url) { | |
return new Promise( (resolve, reject) => { | |
var oReq = new XMLHttpRequest(); | |
oReq.open("GET", url, true); | |
oReq.responseType = "arraybuffer"; | |
oReq.onload = function(oEvent) { | |
var blob = new Blob([oReq.response], {type: "image/jpeg"}); | |
resolve(blob) | |
}; | |
oReq.send(); | |
} ) | |
} | |
function saveData(content,fileName) { | |
var a = document.createElement("a"); | |
a.style = "display: none"; | |
document.body.appendChild(a); | |
var blob = new Blob([content], {type: "image/jpeg"}); | |
var url = window.URL.createObjectURL(blob); | |
a.href = url; | |
a.download = fileName; | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment