How we can create a custom search engine for searching Wikipedia Infos? Solution: See this Custom Wikipedia Search Engine With JavaScript CSS, Wiki Search API Based.
Previously I have shared some search input animation, but this is an API based real search engine for getting Wikipedia’s results. Basically, Wikipedia is a free, open content online encyclopedia created which can be created or edit by any users. When we need info about any things, we go and check Wikipedia first. We also can create a search engine for only wiki Infos and articles, because it provides an API for creating.
Today you will learn to create a Wiki Search API based search engine. Basically, there is a heading for telling about the search engine, a search input, and a button with an icon. The search input has also an animation effect on click, and the button for open random Wikipedia feeds. At first, it will show results about programming, you can change this in JS functions. When you will click on the search button, it will expand and becomes an input field. Then you can type any topic and press enter for getting the result.
So, Today I am sharing Custom Wikipedia Search Engine With JavaScript and CSS. This program is based on the Wikipedia search API, which is provided by the wiki team for development purposes. There I have used many JS libraries like jQuery, bootstrap, solid.js, etc. This means it is not a pure JavaScript program. This program has AJAX-based search results without reloading.
If you are thinking now how this search program actually is, then see the preview given below.
Preview Of Wiki Search API Based Program
See this video preview to getting an idea of how this program looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
Custom Wikipedia Search Engine With JavaScript CSS Source Code
Before sharing source code, let’s talk about it. First I have created a main div named container and placed a heading, a search input, and a button inside it. The layout is based on Bootstrap, as we know bootstrap creating responsive elements. In the HTML file, I have linked external files like CSS, JS, and other libraries’ CDN link.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. With CSS I gave basic styles like size, position, margin, padding, color values, etc. There I have used CSS @Keyframe command for creating the search input animation, it is also handled by jQuery codes. I have used :after :before and transform command for creating the elements.
JS handling here the two main features of the program, one is search input toggle and the other is getting results using API over URL parameters. There is AJAX for getting data without load or reload. You can get results on typing like KeyUP function, you don’t have to press enter to get the result. I have created dynamic elements like div, heading, text in the JS file after getting results using jQuery .append command.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second File for CSS, and the third file for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com )--> <html lang="en" > <head> <meta charset="UTF-8"> <title>Wikipedia Search Engine | Webdevtrick.com</title> <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/animate.css@3.5.2/animate.min.css'> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css'> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <div class="main"> <h1 class="text-center">Wikipedia Search Engine </h1> <div class="content"> <form> <input style="cursor: pointer;" type="text" id="search"/> <div class="after"></div> </form> <a class="icon-container" href="https://en.wikipedia.org/wiki/Special:Random" target="_blank" style="color: white"> <i class="fas fa-random" id="randomIcon"></i> </a> </div> </div> </div> <script src='https://code.jquery.com/jquery-3.3.1.js'></script> <script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js'></script> <script src='https://use.fontawesome.com/releases/v5.0.8/js/solid.js'></script> <script src='https://use.fontawesome.com/releases/v5.0.8/js/fontawesome.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ @import url('https://fonts.googleapis.com/css?family=Bebas+Neue&display=swap'); @import url('https://fonts.googleapis.com/css?family=IBM+Plex+Serif'); html{ min-height: 100vh; } body{ background-color: white; } .main{ background-color: white; } h1 { font-family: 'Bebas Neue', cursive; color: #212121; margin-bottom: 15px; padding: 40px; } .content{ display: flex; flex-direction: row; justify-content: center; align-content: center; } form { display: flex; flex-direction: row; } .icon-container { margin-left: 10px; } #randomIcon{ margin-left: 10px; border-radius: 3px; cursor: pointer; vertical-align: text-bottom; font-size: 40px; color: #ff444b; } .wikiContent{ width: 35em; margin: 0 auto; border-radius: 3px; opacity: 0.7; margin-top: 10px; font-family: 'IBM Plex Serif', serif; padding: 3px 10px 0.1px 10px; max-width: 100%; } .wikiTitle{ font-size: 18px; font-weight: bold; } .wikiTitle > a{ color: black } @keyframes alert { 0%{ transform: scale(0); } 80% { transform: scale(1.2); } 100% { transform: scale(1); } } form { transition: all 0.15s; } form.explode { transform: scale(1.4); opacity: 0; } form { width: 36px; height: 36px; display: block; box-sizing: border-box; position: relative; } input[type="submit"]{ display: none !important; } input { width: 100%; } input { background: none; border: 3px solid #ff444b; border-radius: 26px; box-sizing: border-box; padding: 5px 15px 7px; font-size: 14px; color: #212121; z-index: 2; position: relative; } input:focus { outline: none; } .after { width: 36px; height: 36px; position: absolute; top: 1px; right: 0; z-index: 1; } form.open .after { cursor: pointer; } .after:before, .after:after { content: ''; width: 13px; height: 3px; background-color: #ff444b; border-radius: 3px; position: absolute; transform-origin: 100% 100%; } .after:after{ bottom: -3px; right: -3px; transform: rotate(45deg); } .after:before { top: -3px; right: -3px; transform: rotate(-45deg); opacity: 0; } form, form .after, form .after:before, form .after:after { animation-duration: 1.1s; animation-fill-mode: forwards; } form.in { animation-name: expand; } form.in .after:before { animation-name: beforemagic; } form.in .after:after { animation-name: aftermagic; } form.close, form.close .after, form.close .after:before, form.close .after:after { animation-direction: reverse; } form.close { animation-name: expand; } form.close .after:before { animation-name: beforemagic; } form.close .after:after { animation-name: aftermagic; } form.open { width: 250px; color: #000; } form.open .after { z-index: 3; } form.open .after:before { width: 20px; top: 9px; right: 13px; opacity: 1; } form.open .after:after { width: 20px; bottom: 10px; right: 15px; } @keyframes aftermagic { 0% {} 10% { width: 24px; bottom: -10px; right: -10px; } 15%{ opacity: 1; } 35% { width: 13px; bottom: -3px; right: -3px; opacity: 0; } 25% { opacity: 0; } 64% { opacity: 0; } 65% { opacity: 1; width: 13px; bottom: -2px; right: -3px; } 75% { width: 30px; bottom: 4px; right: 10px; } 90% { width: 20px; bottom: 10px; right: 15px; } 100% { width: 20px; bottom: 10px; right: 15px; } } @keyframes beforemagic { 0% {} 50% { opacity: 0; } 55% { opacity: 1; width: 13px; top: -4px; right: -3px; } 65% { width: 30px; top: 6px; right: 10px; } 80% { width: 20px; top: 9px; right: 13px; } 100% { width: 20px; top: 9px; right: 13px; opacity: 1; } } @keyframes expand { 0% { color: transparent; } 20% { width: 36px; } 45% { width: 250px; } 99% { color: transparent; } 100% { width: 250px; color: #fff; } } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
//Code By Webdevtrick ( https://webdevtrick.com ) $(document).ready(function () { const search = document.getElementById("search"); var searchContent; document.getElementById("search").addEventListener("input", () => { searchContent = search.value; if (searchContent.length === 0){ return; } else{ ajax(searchContent); } }, false); ajax("programming"); const s = $('input'), f = $('form'), a = $('.after'); $("#search").keypress(e => { if(e.which == 13) e.preventDefault(); }) s.focus(function(){ if( f.hasClass('open') ) return; f.addClass('in'); s.css("cursor", "text"); setTimeout(function(){ f.addClass('open'); f.removeClass('in'); }, 1300); }); a.on('click', function(e){ e.preventDefault(); if( !f.hasClass('open') ) return; s.val(''); f.addClass('close'); f.removeClass('open'); s.css("cursor", "pointer") setTimeout(function(){ f.removeClass('close'); }, 1300); }) }); function ajax(search){ $.ajax({ type: "method", url: "https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=" + search + "&prop=info|description&inprop=url|displaytitle&format=json&srlimit=10", data: "data", dataType: "jsonp", success: function (response) { showResults(response); }, error: function (){ alert("Error retrieving search results, please refresh page.") } }); } function showResults(response){ if (document.getElementsByClassName("wikiContent")){ $(".wikiContent").remove(); } for(var i=0; i < 10; i++){ var result = response.query.search[i]; var title = result.title; var description = result.snippet; var url = title.replace(/ /g, "_"); var wikiURL = "https://en.wikipedia.org/wiki/" + url $(".main").append( "<div class=\"wikiContent animated bounceInUp container\">" + "<p class=\"wikiTitle\"><a href=\"" + wikiURL + "\"" + "target=\"_blank\">"+ title +"</a></p>" + "<p class=\"wikiDescription\">" + description + "</p>" + "</div>"); } } |
That’s It. Now you have successfully created Custom Wikipedia Search Engine With JavaScript CSS, Wiki Search API Based Program. If you have any doubt or questions comment down below.
Thanks For Visiting, Keep Visiting.