Skip to content

Instantly share code, notes, and snippets.

@ratacibernetica
Created October 23, 2017 18:48
Show Gist options
  • Save ratacibernetica/df045450c7620d7f280378d7b9f9209e to your computer and use it in GitHub Desktop.
Save ratacibernetica/df045450c7620d7f280378d7b9f9209e to your computer and use it in GitHub Desktop.
Image searcher using Bing image Search API
<div class="prd-gallery">
<div class="title"></div>
<div class="contents"></div>
</div>
<script>
//PoC only, remove before final version
const loadImagesFromTheWeb = (q) => {
if (q) {
$.ajax({
'url': 'https://api.cognitive.microsoft.com/bing/v5.0/images/search',
'data': {
q,
mkt: 'en'
},
'beforeSend': xhr => {
xhr.setRequestHeader('Ocp-Apim-Subscription-Key', 'your-hash-id');
}
}).done(response => {
const images = response.value.reduce((carry, item) => {
carry.push(item.thumbnailUrl);
return carry;
}, []);
$('.title', '.prd-gallery').html(`Web results for "${q}"`);
showImages(images);
});
}
}
const showImages = images => {
// language=JQuery-CSS
$('.contents', '.prd-gallery').html('');
images.map(url => {
$('.contents', '.prd-gallery').append(`<img src='${url}'>`);
});
}
<script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment