GithubHelp home page GithubHelp logo

caroso1222 / amazon-autocomplete Goto Github PK

View Code? Open in Web Editor NEW
399.0 10.0 47.0 193 KB

🚀 Unlock the full power of the Amazon autocompletion engine right into your search input. JavaScript Plugin.

Home Page: https://carlosroso.com/amazon-autocomplete

License: MIT License

JavaScript 100.00%

amazon-autocomplete's Introduction

Amazon Autocomplete JS Plugin

npm version size

AmazonAutocomplete is a vanilla JavaScript plugin to unlock the full power of the Amazon autocompletion engine right into your search input.

Demo: http://carlosroso.com/amazon-autocomplete

demo gif

Features

  • 🐣 Tiny footprint (<3K gzipped)
  • 🔥 Support on all major browsers and +IE10
  • 👓 Library agnostic
  • ⚡️ Data fetched over JSONP
  • ✨ Perf optimized. Debounce events and fetch only when necessary.

Installation

You can grab the minified file from /dist or unminified from /src but I highly recommend installation through npm.

npm install --save amazon-autocomplete

Or you can install it using yarn as well:

yarn add amazon-autocomplete

Now add it to your html file:

<html>
  <body>
    ...
    <script src="/path/to/amazon-autocomplete.min.js" type="text/javascript"></script>
  </body>
</html>

Usage

Create a text input in your html file for the search field.

<input type="text" id="search-input"/> 

Edit your main JavaScript file to create an AmazonAutocomplete instance with your search field CSS selector.

let searchInput = new AmazonAutocomplete('#search-input');

Now you got a search field on steroids. Go ahead and apply some styles to make it shine.

Styling

This is a pretty lightweight JavaScript library so it applies just a few styles to some elements to make it work. You can apply your own styles and customize the look of all the components within the widget. If you’re not that much into CSS, you can grab the following snippet and safely shot it into your stylesheet to get a decent default look. As you can see, AmazonAutocomplete goes all the way BEM.

/* Words container */
.ac__inner{
    background: #f6f6f6;
}

/* Individual word element */
.ac__word{
    margin: 0;
    padding: 5px;
}

/* Word prefix style. It’s the span containing the search keyword within the word. */
.ac__prefix{
    font-weight: bold;
}

/* Style applied to each words while navigating through the list or on hover */
.ac__word--selected, .ac__word:hover{
    background-color: #e3e3e3;
}

Advanced Usage

Configuration

You can customize the plugin behaviour by passing along a config object when instantiating AmazonAutocomplete. These are the properties you can specify:

new AmazonAutocomplete([paramsObject])

Param Type Required Details
selector string Yes CSS selector of the search field.
delay integer No The keyup event on the search field is debounced. This attribute will set the fire rate limit (in milliseconds) on the keyup event callback. Default: 200
showWords boolean No Enable/disable revealing of the words list panel. Can be useful if you want to show the suggested words on your own custom widget. Default: true
hideOnblur boolean No Indicates whether the words list panel should hide when the search field loses focus. Default: true

Events

Each AmazonAutocomplete instance will fire some events. You can susbscribe to these events to, for example, save the selected word in your DB or to show suggested words in your own widget.

Event Callback Param Details
onSelectedWord string This is event is fired when some of the following actions takes place:
  • User clicks a word
  • User navigates through the words list and hits enter
  • User types keyword and hits enter (no suggested word selected)
The callback function will be called with the selected word as its only argument.
onNewWords array This is event is fired when there are new suggested words available. It mostly happens when the keyup event fires on the search field. Keep in mind that the keyup is debounced to improve performance.

Advanced usage example

The next snippet shows how to initialize a AmazonAutocomplete with a 200ms debounce limit, not showing the words panel and not hiding on input text blur. As the words won’t show in the dropdown panel we’ll have to shown them in a custom panel.

//Create the AmazonAutocomplete with our desired properties
let searchInput = new AmazonAutocomplete({
            selector: '#search-input',
            delay: 200,
            showWords: false,
            hideOnblur: false
        });

//Log the selected word to the console
searchInput.onSelectedWord(word => console.log(`searching for ${word}...`));

//Populate your custom panel whenever there are new suggested words available
searchInput.onNewWords(words => words.forEach(word => addWordToCustomPanel(word)));

Features

  • Size: 3.9kb. Goes down to 2.8kb when gzipped.
  • Browser support: Amazon Autocomplete is supported by all major browsers and +IE10. I don’t have any plans to ever support IE8 on any of my projects.
  • Library Agnostic: This plugin is all about vanilla JavaScript. No jQuery required. You can use it in any of your projects whether you’re working on Angular, React, Vue, or plain JavaScript.
  • JSONP: Yep. Amazon Autocomplete uses JSONP to fetch the data from Amazon. Requests are not being made via XHR because of the same-origin policy. Fortunately, the Amazon autocompletion endpoint is JSONP enabled so we can bypass this restriction.
  • Multiple instances: You can have multiple search fields in the same page all of them powered with Amazon Autocomplete. Don’t worry about setting different callbacks when fetching the Amazon autocompletion endpoint. Just create a new instance for each search field and this plugin will automagically handle everything for you. How cool huh?
  • Keyup debounced: With great power comes great responsibility. You don’t want to execute a GET request each time the user types a character and overload the Amazon autocompletion engine server. Amazon Autocomplete debounces the keyup event so that it will request new words only if some time has passed since last request. This is called debouncing.

Licence

AmazonAutocomplete is licensed under MIT licence.

amazon-autocomplete's People

Contributors

blairanderson avatar caroso1222 avatar dependabot[bot] avatar mikescops avatar poetro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

amazon-autocomplete's Issues

Autocomplete not working on Android

Hello Carlos,
thanks for the good work.

I have just found out that the script does not work on Android (my phone has Android 8.0 and Chromium 86.0.4240.110).
The reason seems to be a Chromium bug where the keyCode of the keyUp event is not properly recognized and it is always 0 or 229. Therefore the condition if (/[a-zA-Z0-9-_ ]/.test(char) || key === 8) is never satisfied.

I have used a simple workaround and replaced this condition with something like:
if(self._prevInputValue != self._input.value && validInput(self._input.value))
and it works just fine. I am not a JS guy so there might be a more elegant way.

Regards,
Ikrk

Feature request: Allow filter using search-alias field

Was messing around with amazon and found out that it is possible to filter search results based on their main departments. Let me explain.

You know how when using amazon you can select the department like books/toys & games etc in the search bar? Some network requests capturing and html source fiddling led me to believe filtering/searching by department is possible, albeit only for either one or all departments.

If you view the source of the dropdown selector you find this:

<select class="nav-search-dropdown searchSelect" data-nav-digest="" data-nav-selected="0" id="searchDropdownBox" name="url" tabindex="18" title="Search in" style="top: 0px;">
<option selected="selected" value="search-alias=aps">All Departments</option>
<option value="search-alias=alexa-skills">Alexa Skills</option>
<option value="search-alias=instant-video">Amazon Video</option>
<option value="search-alias=warehouse-deals">Amazon Warehouse Deals</option>
<option value="search-alias=appliances">Appliances</option>
<option value="search-alias=mobile-apps">Apps &amp; Games</option>
<option value="search-alias=arts-crafts">Arts, Crafts &amp; Sewing</option>
<option value="search-alias=automotive">Automotive Parts &amp; Accessories</option>
<option value="search-alias=baby-products">Baby</option>
<option value="search-alias=beauty">Beauty &amp; Personal Care</option>
<option value="search-alias=stripbooks">Books</option>
<option value="search-alias=popular">CDs &amp; Vinyl</option>
<option value="search-alias=mobile">Cell Phones &amp; Accessories</option>
<option value="search-alias=fashion">Clothing, Shoes &amp; Jewelry</option>
<option value="search-alias=fashion-womens">&nbsp;&nbsp;&nbsp;Women</option>
<option value="search-alias=fashion-mens">&nbsp;&nbsp;&nbsp;Men</option>
<option value="search-alias=fashion-girls">&nbsp;&nbsp;&nbsp;Girls</option>
<option value="search-alias=fashion-boys">&nbsp;&nbsp;&nbsp;Boys</option>
<option value="search-alias=fashion-baby">&nbsp;&nbsp;&nbsp;Baby</option>
<option value="search-alias=collectibles">Collectibles &amp; Fine Art</option>
<option value="search-alias=computers">Computers</option>
<option value="search-alias=courses">Courses</option>
<option value="search-alias=financial">Credit and Payment Cards</option>
<option value="search-alias=digital-music">Digital Music</option>
<option value="search-alias=electronics">Electronics</option>
<option value="search-alias=gift-cards">Gift Cards</option>
<option value="search-alias=grocery">Grocery &amp; Gourmet Food</option>
<option value="search-alias=handmade">Handmade</option>
<option value="search-alias=hpc">Health, Household &amp; Baby Care</option>
<option value="search-alias=local-services">Home &amp; Business Services</option>
<option value="search-alias=garden">Home &amp; Kitchen</option>
<option value="search-alias=industrial">Industrial &amp; Scientific</option>
<option value="search-alias=digital-text">Kindle Store</option>
<option value="search-alias=fashion-luggage">Luggage &amp; Travel Gear</option>
<option value="search-alias=luxury-beauty">Luxury Beauty</option>
<option value="search-alias=magazines">Magazine Subscriptions</option>
<option value="search-alias=movies-tv">Movies &amp; TV</option>
<option value="search-alias=mi">Musical Instruments</option>
<option value="search-alias=office-products">Office Products</option>
<option value="search-alias=lawngarden">Patio, Lawn &amp; Garden</option>
<option value="search-alias=pets">Pet Supplies</option>
<option value="search-alias=pantry">Prime Pantry</option>
<option value="search-alias=software">Software</option>
<option value="search-alias=sporting">Sports &amp; Outdoors</option>
<option value="search-alias=tools">Tools &amp; Home Improvement</option>
<option value="search-alias=toys-and-games">Toys &amp; Games</option>
<option value="search-alias=vehicles">Vehicles</option>
<option value="search-alias=videogames">Video Games</option>
<option value="search-alias=wine">Wine</option>
</select>

Some of them seem to be main departments plus some sub departments too. The search-alias seems to be added to the completion engine fields if any of them are selected.

For example lets say I want to search for books, you get search-alias=stripbooks appended to the completion engine request. If I want to search for the book "lab girl". An example completion engine call would be

https://completion.amazon.com/search/complete?method=completion&mkt=1&client=amazon-search-ui&search-alias=stripbooks&q=lab%20girl

I can see how this would be useful and certainly adds more functionality to the existing project

Feature request: Expose more data returned in the jsonp request?

For example searching for "Disney Ufufy" returns

AmazonAutocomplete.AmazonJSONPCallbackHandler_1(["disney ufufy",["disney ufufy"],[{"nodes":[{"name":"Toys & Games","alias":"toys-and-games"}]}],[],"22QXE08TSOQAD"]);

I guess the nodes name and alias would be useful to some (since the request is already made, why waste the data, do you have any idea whats the last field though?)

Destroying

How do I destroy/cleanup the DOM created by amazon-autocomplete?

Language searches

How i would configure the autocomplete to make searches in other language, in spanish or portuguese?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.