Custom Javascript

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 with end(). In particular, if end is omitted, the script never ends and the request will time out waiting for end() 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.

12461246

These are the steps we should follow:

  1. 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;
    
  2. Minify this script using Minifier.org and wrap it in function and the start and end 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(); }
    
  3. 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.

953953

📘

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()}