Last active
February 24, 2017 19:46
-
-
Save cansadadeserfeliz/872744f3ce9b36a8768aa7d94510ca8d to your computer and use it in GitHub Desktop.
unsplash.js
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
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