Skip to content

Javascript Code

Execute custom JavaScript code in any field using the powerful Monaco Editor. Create advanced automation scripts, manipulate page elements, and integrate with external APIs.

You can use JavaScript code in any field or set a field to JavaScript Code type only. We use Monaco Editor for professional code editing experience.

js
// Where your code is ending, add this line only then the form will run further.
$fns.return("1");
  • You can set default value in the field type.

How to open the editor?

Click on the Action button of the field. Image given below.

Javascript Code

Global Variable

Access entry data and predefined functions using global variables in your JavaScript code.

VariableTypeDescriptionUsage
$entryObjectCurrent entry and Excel dataconsole.log($entry);
$fnsObjectPredefined functions libraryconsole.log($fns);

Use $entry variable in the javascript code for access entry and excel data.

js
// ACCESS ENTRY DATA [OBJECT]
console.log($entry);

// PREDEFINE FUNCTIONS [OBJECT]
console.log($fns);

// [1]: STORE DATA IN THE SCRAPER EXCEL (PORTRAIT MODE)
// DATA TYPE: ARRAY
var response = { storeDataInScraper: [] };

// [2]: STORE DATA IN THE CURRENT ENTRY EXCEL ROW (LANDSCAPE MODE)
// DATA TYPE: OBJECT AND ALL OBJECT KEY NAME IS UNIQUE
var response = { storeDataInEntry: {} };

// [3] STORE DATA IN THE ACTIVE/CURRENT FIELD VALUE
var response = { storeDataInField: "String" };

// [4]: SKIP CURRENT FORM
var response = { skipForm: true };

// [5]: SKIP SEGMENT
var response = { skipSegment: true };

// [6]: STOP LOOP
var response = { loopStop: true };

// [7]: RETURN ERROR RESPONSE (BY DEFAULT SET SUCCESS RESPONSE)
var response = { status: false, message: "Error Message" };

// RETURN RESPONSE
$fns.return(JSON.stringify(response));

Editor Shortcut Keys

ShortcutActionDescription
F11Full ScreenToggle full-screen editor mode
Ctrl + SSaveSave current code
Ctrl + ZWord WrapToggle word wrapping
Shift + Alt + LDebug Console LogInsert console.log statement

Predefine Functions

Access powerful built-in functions through the $fns object.

Event Functions

FunctionDescriptionParameters
triggerEventTrigger DOM events on elementselement, eventArray
triggerMouseEventTrigger mouse events on elementselement, eventArray
waitPause execution for specified millisecondsmilliseconds
trimAdvanced text manipulationtext, options
elementExistsCheck if element exists in DOMselectorQuery
elementVisibleCheck if element is visibleselectorQuery
elementInvisibleCheck if element is invisibleselectorQuery
checkElementVisibleCheck element visibility with force optionselectorQuery, isForceCheck
checkElementInvisibleCheck element invisibility with force optionselectorQuery, isForceCheck
checkElementExistsCheck element existence with force optionselectorQuery, isForceCheck
randomHexColorGenerate random hex colorNone
getRandomStringGenerate random stringlength
randomBooleanGenerate random booleanNone
randomUniqueIDGenerate random unique IDlength
randomIntegerGenerate random integermin, max
randomFloatGenerate random floatmin, max, fixed
setSessionStorageSets a value in session storagekey, value
setLocalStorageSets a value in local storagekey, value
js
// triggerEvent
await $fns.triggerEvent(element, ["change", "focus"]);

// triggerMouseEvent
await $fns.triggerMouseEvent(element, ["mousedown", "mouseup"]);

// wait
await $fns.wait(1000);

// trim
await $fns.trim(text, isRemoveAllSpace: Boolean, isLowerCase: Boolean, {
  isUpperCase: Boolean,
  isRemoveSpecialChar: Boolean,
  isRemoveExtraSpace: Boolean,
  replaceChar: Array[2]
});

// elementExists
$fns.elementExists(selectorQuery);

// elementVisible
$fns.elementVisible(selectorQuery);

// elementInvisible
$fns.elementInvisible(selectorQuery);

// checkElementVisible
$fns.checkElementVisible(selectorQuery, isForceCheck = false);

// checkElementInvisible
$fns.checkElementInvisible(selectorQuery, isForceCheck = false);

// checkElementExists
await $fns.checkElementExists(selectorQuery, isForceCheck = false);

// randomHexColor
await $fns.randomHexColor();

// getRandomString
await $fns.getRandomString(length: Number);

// randomBoolean
await $fns.randomBoolean();

// randomUniqueID
await $fns.randomUniqueID(length: Number);

// randomInteger
await $fns.randomInteger(min: Number, max: Number);

// randomFloat
await $fns.randomFloat(min: Number, max: Number, fixed: Number);

// setSessionStorage - Sets a value in the session storage for the current domain
$fns.setSessionStorage(key, value);

// setLocalStorage - Sets a value in the local storage for the current domain
$fns.setLocalStorage(key, value);

Custom JavaScript Examples

🎚️ jQuery Slider Integration

Fill values in jQuery UI sliders programmatically.

Reference: https://jqueryui.com/slider/

js
$slider = $(".bar");
$slider.slider("value", 100);
$slider.slider("option", "slide").apply($slider, [null, { value: $slider.slider("value") }]);

🔄 Override Existing JavaScript Code

Modify or replace existing JavaScript code within the target website. Useful for changing button behavior, functions, or any code snippet on the page.

MethodDescriptionUse Case
Method 1Remove onclick attribute and add event listenerSimple button behavior changes
Method 2Remove element and inject new HTMLComplete element replacement
Method 3Override JS code by script injectionFunction or variable replacement

Complete Implementation Example:

js
function submitAction() {
  alert("Submitted.");
}

function resetAction() {
  alert("Reset.");
}

/**
  [METHOD-01] If using `onclick` attribute in the element
*/
// var submitbtn = document.querySelector("#submit")
// if (submitbtn) {
//   submitbtn.removeAttribute('onclick');
//   submitbtn.addEventListener("click", submitAction)
// }

/**
  [METHOD-02] Remove Button and Inject New Button HTML
*/
// var submitbtn = document.querySelector("#submit")
// if (submitbtn) submitbtn.remove();
// var container = document.querySelector("#container")
// if (container) container.insertAdjacentHTML("afterbegin", '<button class="btn btn-success" type="submit" id="submit">Submit</button>')
// var submitbtn = document.querySelector("#submit")
// if (submitbtn) {
//   submitbtn.addEventListener("click", submitAction);
// }

/**
  [METHOD-03] Override JS Code
*/
var actualCode = `
  function submitAction() {
    alert("Submitted.")
  }
`;

var script = document.createElement("script");
script.textContent = actualCode;
(document.head || document.documentElement).appendChild(script);
script.remove();

// Return
$fns.return("1");

Important

Always test your JavaScript code in the browser console before implementing in production forms.

Examples

Learn how to write JavaScript code for common automation tasks like filling inputs, clicking buttons, and interacting with page elements.

📝 Fill Input Field

Fill text into an input field and trigger necessary events.

js
(async () => {
  // Select the input field
  const inputField = document.querySelector("#username");

  // Set the value
  inputField.value = "[email protected]";

  // Trigger events to notify the page
  await $fns.triggerEvent(inputField, ["input", "change"]);

  // Return success
  $fns.return("1");
})();

🖱️ Click Button

Click a button and wait for the action to complete.

js
(async () => {
  // Find the submit button
  const submitButton = document.querySelector("#submit-btn");

  // Click the button
  submitButton.click();

  // Wait for 1 second to allow the action to complete
  await $fns.wait(1000);

  // Return success
  $fns.return("1");
})();

📋 Select Dropdown Option

Select an option from a dropdown menu by value or text.

js
(async () => {
  // Select by value
  const dropdown = document.querySelector("#country");
  dropdown.value = "USA";
  await $fns.triggerEvent(dropdown, ["change"]);

  // OR Select by visible text
  const options = dropdown.querySelectorAll("option");
  for (let option of options) {
    if (option.textContent.trim() === "United States") {
      dropdown.value = option.value;
      await $fns.triggerEvent(dropdown, ["change"]);
      break;
    }
  }

  $fns.return("1");
})();

☑️ Check Checkbox or Radio Button

Check or uncheck checkboxes and select radio buttons.

js
(async () => {
  // Check a checkbox
  const checkbox = document.querySelector("#terms-checkbox");
  checkbox.checked = true;
  await $fns.triggerEvent(checkbox, ["change", "click"]);

  // Select a radio button
  const radioButton = document.querySelector('input[name="gender"][value="male"]');
  radioButton.checked = true;
  await $fns.triggerEvent(radioButton, ["change", "click"]);

  $fns.return("1");
})();

⏳ Wait for Element to Appear

Wait for an element to appear on the page before interacting with it.

js
(async () => {
  // Wait for element to exist (max 10 seconds)
  const elementExists = await $fns.checkElementExists("#dynamic-content", true);

  if (elementExists) {
    const element = document.querySelector("#dynamic-content");
    element.click();
    $fns.return("1");
  } else {
    // Return error if element not found
    const response = { status: false, message: "Element not found" };
    $fns.return(JSON.stringify(response));
  }
})();

🔄 Fill Multiple Fields

Fill multiple form fields in sequence.

js
(async () => {
  // Fill first name
  const firstName = document.querySelector("#first-name");
  firstName.value = $entry.firstName;
  await $fns.triggerEvent(firstName, ["input", "change"]);

  // Fill last name
  const lastName = document.querySelector("#last-name");
  lastName.value = $entry.lastName;
  await $fns.triggerEvent(lastName, ["input", "change"]);

  // Fill email
  const email = document.querySelector("#email");
  email.value = $entry.email;
  await $fns.triggerEvent(email, ["input", "change"]);

  // Wait a bit before submitting
  await $fns.wait(500);

  // Click submit
  const submitBtn = document.querySelector("#submit");
  submitBtn.click();

  $fns.return("1");
})();

🎯 Conditional Logic

Execute different actions based on page conditions.

js
// Check if error message is visible
const errorExists = $fns.elementVisible(".error-message");

if (errorExists) {
  // Skip this form if there's an error
  const response = { skipForm: true };
  $fns.return(JSON.stringify(response));
} else {
  // Continue with normal flow
  const submitBtn = document.querySelector("#submit");
  submitBtn.click();
  $fns.return("1");
}

💾 Store Data from Page

Extract data from the page and store it in Excel.

js
// Extract data from the page
const productName = document.querySelector(".product-name")?.textContent.trim();
const productPrice = document.querySelector(".product-price")?.textContent.trim();
const productRating = document.querySelector(".rating")?.textContent.trim();

// Store data in the current entry row (landscape mode)
const response = {
  storeDataInEntry: {
    productName: productName,
    productPrice: productPrice,
    productRating: productRating,
  },
};

$fns.return(JSON.stringify(response));

🌐 Request Monitoring Script

Use the following event listener to intercept request details and return custom success/error responses.

javascript
async function getDetails(e) {
  var data = e.detail;
  data = JSON.parse(data);
  console.log("CTH: data", data);

  // Return logic
  $fns.return(JSON.stringify({ status: false, message: "Error Message" }));
  // $fns.return(JSON.stringify({ status: true, message: "Success Message" }));
}

// Get Web Request Data
document.addEventListener('CTH-EXCELLENT-FILLER-REQUEST-EVENT', getDetails);

Event Data Output Schema:

json
{
  "headers": "object",
  "method": "string",
  "status": "number",
  "url": "string"
}

🔍 Advanced Element Selection

Use various methods to find and interact with elements.

js
// By ID
const elementById = document.querySelector("#my-id");

// By Class
const elementByClass = document.querySelector(".my-class");

// By Attribute
const elementByAttr = document.querySelector('[data-test="submit-button"]');

// By XPath (using document.evaluate)
const xpath = "//button[contains(text(), 'Submit')]";
const xpathResult = document.evaluate(
  xpath,
  document,
  null,
  XPathResult.FIRST_ORDERED_NODE_TYPE,
  null
);
const elementByXPath = xpathResult.singleNodeValue;

// Click the found element
if (elementByXPath) {
  elementByXPath.click();
}

$fns.return("1");

Pro Tip

Always use await with asynchronous functions like $fns.wait(), $fns.triggerEvent(), and $fns.checkElementExists() to ensure proper execution order.

Released under the MIT License.