Last active
January 15, 2018 01:08
-
-
Save sompylasar/c244b07cb4365500d68747179bf1c639 to your computer and use it in GitHub Desktop.
Avoid clicking every subreddit to view the full image at https://www.reddit.com/r/ColorizedHistory/
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
// Avoid clicking every subreddit to view the full image, for example, at https://www.reddit.com/r/ColorizedHistory/ | |
// Run the following code via the Chrome DevTools Console in the top frame of the Reddit page. | |
(function hoistRedditImages() { | |
Promise.all( | |
[].slice | |
.call(document.querySelectorAll("#siteTable a[data-href-url]")) | |
.map(el => { | |
if (document.querySelectorAll(".hoistRedditImages").length) { | |
return; | |
} | |
const url = el.getAttribute("data-href-url"); | |
if (url.indexOf("/r/") !== 0) { | |
return; | |
} | |
return fetch(url) | |
.then(response => response.text()) | |
.then(html => [el, url, html]); | |
}) | |
.filter(x => !!x) | |
).then(all => { | |
all | |
.map(([el, url, html]) => [ | |
el, | |
url, | |
(html => { | |
const root = document.createElement("html"); | |
root.innerHTML = html; | |
return [].slice.call( | |
root.querySelectorAll( | |
'#siteTable [data-permalink][data-domain="i.redd.it"]' | |
) | |
); | |
})(html).map(el => el.getAttribute("data-url")) | |
]) | |
.forEach(([el, url, imageurls]) => { | |
const top = el.parentNode.querySelector(".top-matter"); | |
if (top) { | |
const container = document.createElement("div"); | |
container.className = "hoistRedditImages"; | |
top.appendChild(container); | |
imageurls.forEach(imageurl => { | |
const img = document.createElement("img"); | |
img.src = imageurl; | |
img.style.maxWidth = "70vw"; | |
container.appendChild(img); | |
}); | |
} | |
}); | |
console.log("hoistRedditImages: done."); | |
}); | |
console.log("hoistRedditImages: please wait..."); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment