0

Lets say my blog is http://foo.tumblr.com.

All the post's images are stored in xx.media.tumblr... (for example: https://24.media.tumblr.com/tumblr_kzjlfiTnfe1qz4rgho1_250.jpg) (first 2 numbers can be skipped)

But i want the URL of the image be in the same domain of my blog, and looks something like this:

http://foo.tumblr.com/tumblr_kzjlfiTnfe1qz4rgho1_250.jpg 

(that doesn't exist)

Why i need that? I am creating a script, and it generates a canvas that detects if the image have transparency with a getImageData (all the .jpg are skipped), but since the subdomain is different, i get a cross-domain security error, and the canvas is tainted, avoiding the use of getImageData.

So.. how can i do that?

I think Tumblr API could be useful, but how?

yukatta
  • 505
  • 2
  • 7
  • 16
  • 1
    I don't think its possible, as you mention Tumblr servers all its images from `media.tumblr.com` more info: http://stackoverflow.com/questions/16832963/url-schema-of-tumblr-images/16836223#16836223 – mikedidthis Mar 25 '14 at 08:26

1 Answers1

0

Scrape your sitemap for all posts and get their images. You could use the API or just with Javascript in the browser console:

xmlin = prompt(); // view-source:biochemistri.es/sitemap1.xml
parser = new DOMParser();
xmlDoc=parser.parseFromString(xmlin,"text/xml");
xmlDoc.querySelectorAll('loc')[0].remove();
posts = xmlDoc.querySelectorAll('loc');
postlist = [];
for (i=0;i<posts.length;i++) {postlist.push(posts[i].innerHTML)};

...to generate an array containing all posts, which can be navigated through for photo posts (div.post.photo) and their URLs copied.

Then simply generate a new list of images with a for loop and newImg = document.createElement('img'), setting an origin attribute using newImg.setAttribute('origin') = myPhotoList[n] which can then be used to select an image programmatically:

document.querySelector("img[origin='"+{PhotoURL-HighRes}+"']"

(or {PhotoURL-1280}, {PhotoURL-500}, {PhotoURL-250} etc. Once retrieved over an XMLHttpRequest, you could simply switch the post in the DOM. The {PhotoURL-HighRes} in my example above wouldn't work, it'd be an attribute from the page I'm just indicating which part you'd want to get from the theme HTML.

As explained in this post, there is a variable which could be used as a more concise attribute than the full origin URL if you want to be a bit more specific with regular expressions.

This would effectively put all of your images onto your local URL, with URLs like foo.tumblr.com/images/tumblr_kzjlfiTnfe1qz4rgho1_250.jpg, and avoid cross-domain restrictions. I'm guessing it'd work only if you don't have a ton of posts as custom pages such as you'd be using to store images do have a restriction on their size (though I suppose you could make a second one).

Also might be sensible to include CSS to set display: none in case anyone stumbles upon the page by accident, and a redirect function to the homepage with window.onload or similar.

Community
  • 1
  • 1
Louis Maddox
  • 5,226
  • 5
  • 36
  • 66