Use and instance of the GoogleCSE function to search your site. This is using vanilla javascript but is still a work in progress. If using this in a production environment make sure you're running everything through Babel.
-
Add this project to the approprate place within your projects. This is most likely where you put all your other libraries. If you don't have anywhere just create a /libs directory and throw it in there.
-
Add GoogleCSE into your bundled Javascript. So if you're using a task runner like gulp bundle this with your Footer.js or the likes.
-
Check if GoogleCSE is defined
typeof GoogleCSE !== "undefined"
-
Create Search Engine ID: https://cse.google.com/cse/
-
Create Google Custom Search v1 api key
https://code.google.com/apis/console/
-
Initialize Your Search:
const apiKey = "API_KEY_HERE";
const cx = "SEARCH_ENGINE_ID_HERE";
// GoogleSearch variable is now an instance of GoogleCSE
const GoogleSearch = new GoogleCSE({ apiKey, cx });
// Possible search query
var searchQuery = "Dank Memes";
// Pass our query
GoogleSearch._searchSite(searchQuery);
// Rendered Results will look for the following markup
<div class="google-wrapper">
<div id="google-controls">
<p id="google-total-results"></p>
<input id="google-search-input" type="text" placeholder="Search..." />
</div>
<div id="google-results"></div>
<div id="google-pagination"></div>
</div>