ParseHub Expressions

This is a relatively advanced tutorial! Without programming knowledge, you should stick to ParseHub's other tutorials.

A ParseHub expression is similar to an expression in a programming language. Some nodes can use expressions to determine exactly how they manipulate a webpage. For example, you can use a conditional command to execute its children only when an expression is truthy.

The conditional, extract, input, and loop commands primarily make use of expressions. 

Syntax

Expressions use a subset of the javascript syntax. Some examples:

  • 1 evaluates to the number 1
  • "1" evaluates to the string 1

A variable in an expression is evaluated in the current scope of the command where that expression is used. Some examples:

  • a in the scope {"a": "b"} evaluates to the string b
  • a in the scope {} is undefined

Objects Available in ParseHub

ParseHub exposes 4 objects available in all expressions. Besides these, ParseHub can also use previously extracted data as variables.

$element or $e represents the current element, and has the following properties:

  • $e.text - gets the inner text or value (depending on what type of element is being selected)
  • $e.outerHTML - get the outer html of the element
  • $e.prop - a function that behaves like jQuery prop function. This lets you compare attributes of an HTML tag that aren't immediately visible on a page. For instance, the star rating of some products
  • $e.css - a function that behaves like jQuery css function
  • $e.parentProp - a function that behaves like jQuery prop function. If the result is undefined, then you apply the jQuery prop to the parent, and continue doing this until you get a result or hit the html element.
  • $e.parent - get the parent element with all the same properties as $e/

$location has the following properties:

  • $location.href - gets the current url of the page

$selection has the following properties:

  • $selection.length - gets the length of the parent select command to this Conditional
  • $selection.index - which is the index of the closest parent selection command

$date.getDate() which gets information about the current date and time

Scope lookup

When a variable in an expression is undefined in the current scope, parent scope lookup occurs. This is similar to variable scoping in a programming language. ParseHub will attempt to resolve the variable by travelling up into the parent scope until the variable is resolved or the root scope is reached. Some examples (current scope in bold):

  • first > second in the scope {"first": 3, "list": [{"second": 1}]} evaluates to true because the number 3 is greater than 1. first is not in the current scope, so the parent scope is checked, and first is found and resolved there.
  • first > second in the scope {"first": 3, "list": [{"second": 1}]} evaluates to false because second is not in the current scope, and the current scope is the root scope. So the expression is comparing a number to undefined, which evaluates to false. Remember, comparisons follow javascript semantics.

Hidden variables

Expressions can also reference hidden variables that are added to the scope from a loop.

The command allows you to iterate over a list. The iterator will be temporarily added to the scope, but will not be visible in the results (thus, hidden). The loop command specifies the name of the iterator variable, this is defaulted to item. Expressions may reference these hidden scope properties.

The tool also exposes $index as a hidden property. This is the position in the loop command. The index starts at 0.

Available functions

The scope has two global functions:

  • parseInt (parseFloat)
  • $createArray

The parseInt(parseFloat) function behaves exactly like the parseInt(parseFloat) javascript function.

The $createArray function creates a new array. This function takes in 1 parameter that specifies its size. This function is useful when you'd like to repeat something n times. To do this, use a loop node with $createArray(n) as the list to iterate over. Note that n can be any expression.

Available ParseHub objects

ParseHub exposes 4 objects available in all expressions:

$element or $e represents the current element, and has the following properties:

  • text - gets the inner text or value (depending on what type of element is being selected)
  • outerHTML - get the outer html of the element
  • prop - a function that behaves like jQuery prop function
  • css - a function that behaves like jQuery css function
  • parentProp - a function that behaves like jQuery prop function. If the result is undefined, then you apply the jQuery prop to the parent, and continue doing this until you get a result or hit the html element.
  • parent - get the parent element with all the same properties as $e/

$location has the following properties:

  • href - which is the current url of the page

$selection has the following properties:

  • length - which is the length of the closest parent selection command
  • index - which is the index of the closest parent selection command

$date.getDate() which gets information about the current date and time

 

Have more questions? Submit request!

0 Comments

Article is closed for comments.