gallery.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0" />
  7. <style>
  8. body { background-color:#474747; }
  9. label { color:white; font-family:sans-serif; }
  10. img { margin-right:4px; cursor:pointer; }
  11. #cont {
  12. margin-top:4px;
  13. overflow-y: scroll;
  14. /* for Firefox: */
  15. scrollbar-color: #222222 rgba(0,0,0, 0.2 );
  16. scrollbar-width: thin;
  17. }
  18. #cont::-webkit-scrollbar {
  19. width: 10px;
  20. background: rgba(0,0,0,0.2);
  21. }
  22. #cont::-webkit-scrollbar-thumb {
  23. background: #222222;
  24. margin:2px;
  25. }
  26. </style>
  27. <script>
  28. var imgs = [], hits = [];
  29. function update() {
  30. var ikey = document.getElementById("keyws").value;
  31. if(document.getElementById("isolated").checked) ikey += " isolated";
  32. ikey = ikey.trim();
  33. console.log(JSON.stringify(ikey));
  34. //alert(keyws);
  35. var request = new XMLHttpRequest();
  36. var url = "https://pixabay.com/api/?image_type=photo"+(ikey==""?"":"&q="+encodeURIComponent(ikey))+"&per_page=200&page=1&key=10554583-a5eacb61c2e61105fdfb6eb88&safesearch=true";
  37. if(ikey=="") {
  38. if(Math.random()<0.005) url="gallery.php?url="+encodeURIComponent(url+"&safesearch=true");
  39. else url="gallery.json";
  40. }
  41. //console.log(url);
  42. request.open("GET", url, true);
  43. request.onload = imgsLoaded.bind(this);
  44. request.onerror = function(e) { console.log(e.target.response); }
  45. request.send();
  46. }
  47. function imgsLoaded(e) {
  48. //console.log(e.target.response);
  49. var iw = window.innerWidth-28, th=100;
  50. var cont = document.getElementById("cont");
  51. cont.setAttribute("style","height:"+(window.innerHeight-42)+"px;");
  52. while(cont.firstChild) cont.removeChild(cont.firstChild);
  53. cont.scrollTop = 0;
  54. hits = JSON.parse(e.target.response)["hits"]; imgs=[];
  55. var heis = [];
  56. for(var i=0; i<hits.length; ) {
  57. var totw = 0, j=i;
  58. while(j<hits.length) {
  59. var hit = hits[j];
  60. var w=hit["previewWidth"], h=hit["previewHeight"];
  61. if(j<=i+1 || totw+w*(th/h)+4 < iw) { totw+=w*(th/h) + 4; j++; }
  62. else break;
  63. }
  64. while(i<j) {
  65. heis.push(th*(iw/totw)); i++;
  66. }
  67. }
  68. for(var i=0; i<hits.length; i++) {
  69. var hit = hits[i];
  70. var img = document.createElement("img");
  71. img.setAttribute("src",hit["previewURL"]);
  72. img.setAttribute("height",heis[i]+"px");
  73. img.addEventListener("click", imgClick);
  74. cont.appendChild(img);
  75. imgs.push(img);
  76. }
  77. console.log(hits);
  78. }
  79. function imgClick(e) {
  80. var ind = imgs.indexOf(e.currentTarget);
  81. var hit = hits[ind]; console.log(hit);
  82. var x = "app.open(\""+hit["largeImageURL"]+"\",null,true);";
  83. window.parent.postMessage(x, "*");
  84. }
  85. function msg(e) { alert("These images come from www.PixaBay.com and are free to use for any purpose."); }
  86. </script>
  87. </head>
  88. <body onload="update()">
  89. <button onclick="msg()">?</button>
  90. <label>Keywords:</label> <input type="text" id="keyws" onchange="update()"></input>
  91. <input type="checkbox" id="isolated" style="vertical-align:middle;" onchange="update()" /> <label for="isolated">Isolated</label>
  92. <div id="cont"></div>
  93. </body>
  94. </html>