JavaScript code

Table of contents


Description

This action allows you to execute custom JavaScript code on site pages. Also, using this action, you can perform arithmetic operations with project variables:


How to add an action to a project?

Via context menu Add Action -> Custom Code -> JavaScript

Or use smart search.


What is it used for?

  • Arithmetic operations on variables

  • Interaction with page elements using JavaScript


How to work with an action?

The action has several modes of operation:

Regardless of the selected operating mode, the action settings must contain a variable to which the result of the work will be saved (even if the logic of your code does not imply a return value)

Locally

The code will be executed in a sandboxed environment (regardless of the browser, outside of it). This method can be used to work with variables. Work with numbers and strings. Perform any actions with data that JS allows.

You can test this code using the JavaScript Tester.

When working in this mode, you do not need to specify the return keyword if you want to return some value. The result of this action will be the value calculated in the last line of the action. In the example below, the value “6” will get into the project variable {-Variable.result-}, the result of the expression 2 + 2 * 2.


On the current page

The code will be executed in the browser (current instance). This method should be used to work with the DOM tree of the page, to interact with the elements of the page.

When working in this mode, you have access to all objects of the current page, including you can use libraries and frameworks connected to the site (for example jQuery).

On extension page

The code will be executed in the context of the activated extension.

When creating a page window

The script will execute during the DOMWindowCreated event. Using this mode, you can override any JavaScript objects before the first access to them on the site. This mode has several options:

  • one time - the code will be executed once;

  • on the domain - the code will be executed every time a window is created for the specified domain (if the “In all tabs” option is checked, the code is executed in all tabs of the instance)

  • every time - the code is executed every time a window is created, regardless of the domain (if the “In all tabs” option is checked, the code is executed in all tabs of the instance)

On page load

In this case, the script is executed during the DOMContentLoaded event. One use case: to disable the display of unnecessary or obstructing elements on the page

As in the previous mode, there are several options for execution:

  • one time - the code will be executed once;

  • on the domain - the code will be executed every time a window is created for the specified domain (if the In all tabs option is checked, the code is executed in all tabs of the instance)

  • every time - the code is executed every time a window is created, regardless of the domain (if the In all tabs option is checked, the code is executed in all tabs of the instance)


Usage example

Arithmetic operations

As a result of executing this action, the result of dividing the project variable height by 2 will be saved to the result project variable.

Connecting JavaScript libraries

Using this action, you can embed a library on a page that was not originally there. For example, you can add jQuery using this code

var s = document.createElement('script'); s.type = 'text/javascript'; s.src = 'https://code.jquery.com/jquery-1.10.2.min.js'; document.body.appendChild(s);

Project example: