Other Commands
Eval
The eval command is intended for executing code written in other programming languages (currently only JavaScript (ES5) is supported). It can only be executed in the context of the block, because it works with the register. However, the command can return a value not only to the register, but can assign the values of variables in JS to digger variable also. This feature allows you to receive several result values at once within one execution. If your JS code will work for too long, it will be automatically interrupted. This is done to avoid misuse of this functionality.
Examples:
# RETURNING RESULT OF EXECUTION TO THE REGISTER
# PLEASE NOTE THAT IN THIS CASE
# JS CODE SHOULD BE USED INSIDE CLOSURE AND RETURN VALUE
- eval:
routine: js
body: "(function (){return new Date().getTime();})();"
# MAP DIGGER VARIABLE `time` WITH JS VARIABLE `jstime` (VALUE OF time VARIABLE WILL BE SAME AS VALUE OF jstime)
- eval:
routine: js
body: "var jstime = new Date().getTime();"
assign:
time: jstime
Usage examples:
- find:
path: body
do:
# EXECUTE JS CODE THAT RETURNS CURRENT DATE/TIME AS UNIX TIMESTAMP
- eval:
routine: js
body: "(function (){return new Date().getTime();})();"
# REGISTER VALUE: 1476403635606
- find:
path: body
do:
# EXECUTE JS CODE THAT ASSIGN CURRENT DATE/TIME AS UNIX TIMESTAMP
# TO JS VARIABLE `jstime` AND MAP IT TO DIGGER VARIABLE `time`
# YOU CAN MAP ANY NUMBER OF VARIABLES THIS WAY
- eval:
routine: js
body: "var jstime = new Date().getTime();"
assign:
time: jstime
- variable_get: time
# REGISTER VALUE: 1476403635606
You can also use digger variables in JS code.
For example:
- find:
path: a
do:
- parse:
attr: href
- variable_set: link
- eval:
routine: js
body: "(function (){return '<%link%>' + '?time=' + new Date().getTime();})();"
# REGISTER VALUE: http://somelink.com/?time=1476403635606
# PLEASE NOTE THAT VARIABLE TAG IS ENCLOSED IN QUOTES
# SO JS INTERPRETER KNOWS THAT ITS A STRING
In the next chapter, we'll read about the commands for reloading and rereading pages.