Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Last active February 24, 2017 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cansadadeserfeliz/872744f3ce9b36a8768aa7d94510ca8d to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/872744f3ce9b36a8768aa7d94510ca8d to your computer and use it in GitHub Desktop.
unsplash.js
function replace_unsplash_urls(width, height) {
var url = '';
var prev_url = '';
var links = document.querySelectorAll('#gridMulti a');
for(var i = 0; i < links.length; i++) {
var item = links.item(i);
url = links.item(i).style.getPropertyValue('background-image');
console.log(i, url)
if (url) {
url = url.slice(5, -2);
url = url.replace(/(w=).*?(&)/,'$1' + width + '$2');
url = url.replace(/(h=).*?(&)/,'$1' + height + '$2');
prev_url = url;
console.log('save url', url)
}
if (i % 2 != 0) {
//console.log(item, prev_url)
item.href = prev_url;
console.log('asign url', item.href, prev_url);
var p = document.createElement('p')
var a = document.createElement('a')
a.setAttribute('href', prev_url);
a.setAttribute('target', '_blank');
a.innerHTML = width + 'x' + height;;
p.appendChild(a);
item.parentElement.parentElement.parentElement.prepend(p);
}
}
}
replace_unsplash_urls(1500, 600);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment