Skip to content

Instantly share code, notes, and snippets.

@AlexR1712
Created October 7, 2017 18:47
Show Gist options
  • Save AlexR1712/01835f72cfdddc9d78478b82b513645b to your computer and use it in GitHub Desktop.
Save AlexR1712/01835f72cfdddc9d78478b82b513645b to your computer and use it in GitHub Desktop.
Tumblr Post type Text, thumbnails
/* Created by AlexR1712 */
function getImg (str) {
var m,
urls = [],
rex = /<img[^>]+src="(http:\/\/[^">]+)"/g;
while ( m = rex.exec( str ) ) {
urls.push( m[1] );
}
return urls;
}
var posts = document.querySelectorAll('.post-excerpt');
posts.forEach(function(body) {
imgs = getImg(body.innerHTML);
/* remove html tags */
body.innerHTML = body.textContent;
if (imgs.length > 0) {
/* Set first image as default in view*/
body.parentElement.parentElement.childNodes[0].src = imgs.shift();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment