Scraper
The Scraper is used to get data from an eCommerce site's page, given a configuration data object.
Example
import $ from 'jquery'
import fetch from 'fetch'
import scraper from 'Scraper'
// setup scraper
scraper.setjQuery($)
// load configuration
fetch('client_scraper_config.json').then(config) {
// get the current page's data
let scrapedData = scraper.scrape(config)
}
Setup
After importing the Scraper you first need to run setjQuery
which as well as giving the Scraper access to JQuery will call setHtml
. This is the only required step before using the Scraper.
Once you have called setjQuery
you can then call scrape
. This will both return data for the current page, as well as set up watchers for such things as Product Quick Views, Quick Carts and full page apps.
Public Interface
setjQuery( <JQuery> )
Gives the Scraper access to JQuery.
setHtml( <JQuery Response> )
Sets the current HTML to Scrape, or defaults to the current pages HTML. It requires a JQuery response such as $('html')
.
setStash( <Stash> )
Sets a Hodor Stash to use. This is used by the Scraper to store information such as discount codes if it needs to remember them over time. If this is not set, data is saved in LocalStorage.
setCustomTransform( <Object> )
Setting a custom scraper allows customized pre and post-scrape Javascript to be called. The pre-scrape call will happen after the page.type has been set, and the post-scrape call will happen after all scraping is complete, but also be called.
The object should be as follows:
{
pre: function ( state, config ) {},
post: function ( state, config, event ) {}
}
The state will be the current state object. The config will be the current configuration object.
Events will be the same events names as the bus (see below) takes. If you define only a pre or only a post method, only that will be called. Note the state is passed by reference.
setupScraperOnFaplayr()
Points window.fanplayr.scraper
to the use Scraper.
scrape( <ScrapeData> )
Returns the scraped data given an object as defined in Data Format.
bus.on( Event<String>, Callback<Function> )
Sets an event watcher. This uses JQuery events .on.
Possible events are:
- product-quick-view
- cart-change
- page-change
bus.off( Event<String>, Callback<Function> )
Sets an event watcher. This uses JQuery events .off.
getLookAtValue( <Lookup> )
Returns the value of a Lookup. This may return a value or an HTMLElement or even JQuery object depending on filters.
getLookAtValueFrom( <Lookup>, <JQuery Response> )
Returns the value of a Lookup, as above, given a specific JQuery Response. Can be used to look for values in a subset of the DOM such as a line item from the cart.
getVersion()
Simply returns the value of "version" from the ScrapeData. Only set after calling scrape
.
applyToCart( { code: <String> } )
A function that can be called to do the Apply to Cart action. This may be null if no function exists for the current call.