# Custom Scripts

You can run your own Javascript with the Browser to automate certain tasks or to tweak the application behavior when running on the device Browser.

Browser will evaluate the script you define after the page loads.  It is your script's responsibility to determine whether or not it should run for a given page.

## Writing a custom script

In the example below, the script is configured to automatically close the Browser window when the text "You are now logged off" appears on the page:

{% code lineNumbers="true" %}

```javascript
setTimeout(function() {

    const pageContains = function(text) {
        const body =  document.body.innerHTML;
        if (body.search(text) > -1) return true;
        else return false;
    }
    
    if (window.location.href.includes("https://login.microsoftonline.com/") {
        if (pageContains("You are now logged off")) {
            android.quit();
        }
    }

}, 300);
```

{% endcode %}

In the above example, line 9 determines if the logic needs to run if on a specific page.  Line 10 uses a previously defined `pageContains` function and if it returns true will call one of the built-in actions `android.quit()` to close the current Browser.

{% hint style="info" %}
The entire script is wrapped in a `setTimeout` function -- this is optional, but it allows it to run asynchronously and can sometimes assist in making sure the entire page is loaded before running.
{% endhint %}

{% hint style="info" %}
For a list of available actions, refer to the Page Action section in the technical guide.
{% endhint %}

## Configuring the Browser to use the script

Save the above script into a `.js` file and you will need to set the Browser configuration to point to your saved `.js` script.   If your javascript file is hosted, you can specify it directly as follows:

```
"browser" : {
    ...
    "customScriptPath" : "https://storage.bluefletch.com/scripts/customhelper.js",
    ...
}
```

Or you can use Launcher's Asset Manager functionality to serve the javascript file to the browser as follows.

```json
"assets" : {
   ...
   "customScript" : "/sdcard/Download/ems/custom_script.js"
},
...
"browser" : {
   ...
   "customScriptPath" : "asset:customScript",
   ...
}
```

{% hint style="info" %}
If logging is set to `debug` mode, calls to `console.log` will appear in the Browser log files.  This may help when developing your script.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bluefletch.com/bluefletch-enterprise/product-guides/browser/features/custom-scripts.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
