Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. const button = document.querySelector('#nasaimagesearch');
  2. const clearbutton = document.querySelector('#clearsearches');
  3. const searchStorage = window.localStorage;
  4.  
  5. function grabitems(collection) {
  6. const { items } = collection; // collection.collection.STUFF
  7. // console.log(items.length);
  8. items.forEach((item) => {
  9. const image = item.links[0].href;
  10. const collectionjson = item.href;
  11. fetch(collectionjson).then((r) => { // fetch collection.json file from nasa api
  12. if (r.status === 200) {
  13. return r.json();
  14. }
  15. throw new Error(`return status is ${r.status}`);
  16. }).then((jsonfile) => {
  17. // grab metadatajson file from collectionjson
  18. const metadatajson = jsonfile[jsonfile.length - 1];
  19. return fetch(metadatajson)
  20. }).then((res) => { // fetch metadatajson file
  21. if (res.status === 200) {
  22. return res.json();
  23. }
  24. throw new Error(`return status bad ${res.status}`);
  25. })
  26. .then((metdjson) => {
  27. // console.log(metdjson);
  28. // console.log(metdjson["AVAIL:Description"]);
  29. const descriptions = metdjson['AVAIL:Description'];
  30. const titles = metdjson['AVAIL:Title'];
  31. const nasaid = metdjson['AVAIL:NASAID'];
  32.  
  33. const li = document.createElement('LI');
  34. const img = li.appendChild(document.createElement('img'));
  35. img.outerHTML = `<img src="${image}" width="100" height="100;"/>`;
  36. document.getElementById('follows').appendChild(li);
  37. img.addEventListener('mouseover', () => {
  38. alert(descriptions);
  39. alert(titles);
  40. })
  41. })
  42. })
  43. }
  44.  
  45. function searchImages(keyword) {
  46. fetch(`https://images-api.nasa.gov/search?q=${keyword}&media_type=image`).then((r)=>{
  47. if (r.status === 200) {
  48. return r.json();
  49. }
  50. throw new Error(`return status is ${r.status}`);
  51. }).then((json)=>{
  52. grabitems(json.collection); // grabs top collection
  53. })
  54. }
  55.  
  56. clearbutton.addEventListener('click', () => {
  57. searchStorage.clear();
  58. });
  59.  
  60. button.addEventListener('click', () => {
  61. const inp = document.getElementById('thingtosearch');
  62. if (!searchStorage.getItem('search')) {
  63. searchStorage.setItem('search', inp.value);
  64. }
  65. searchImages(inp.value);
  66. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement