Inject custom Javascript code before an Extract API processes a page
X-Eval allows you to inject custom JavaScript into any website. It is a custom header like any of the above, but has special requirements in regards to syntax.
-
an X-eval script starts with an anonymous function declaration which will be called by Diffbot
function () {}
-
an X-eval script needs to start with
start()
and end withend()
. In particular, ifend
is omitted, the script never ends and the request will time out waiting forend()
to be called.function () {start(); end();}
-
an X-eval script needs to be minified (turned into a single-line). Use a minifier like Minifier.org.
X-evaluate Example
The web page Example.com has a paragraph of text explaining its purpose. Let's add a "Today is X" message to the end of that paragraph, with X being the current day of the week.
These are the steps we should follow:
-
Write a script which successfully does this in the browser console.
const now = new Date(); const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; const day = days[ now.getDay() ] document.querySelector("p").innerText += "Today is " + day;
-
Minify this script using Minifier.org and wrap it in
function
and thestart
andend
calls.function() {start(); const now=new Date();const days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];const day=days[now.getDay()];document.querySelector("a").innerText="Today is "+day; end(); }
-
Pass the script along as the
X-Forward-X-Evaluate
custom header. We'll use Postman here, but you can also attach it permanently using the dashboard or raw rule backup/restore as documented above. Issuing the request will reveal our desired text in the return data.
X-Evaluate will only be executed if called from the API the rule resides in.
If you have an X-Evaluate script in your Article API rule and make a request with the Analyze API that identifies the page as an article, the X-Evaluate will not be executed.
Saving data directly to a JSON output field
Javascript data types and data structures can be saved directly to a named field in the JSON result, using the save
function as follows:
save(fieldName, fieldValue)
The example below grabs the cookie from the browser and outputs it to a field called theCookie:
function() {
start();
save("theCookie", document.cookie);
end();
}
Minified, this would look like:
function(){start(),save("theCookie",document.cookie),end()}