> For the complete documentation index, see [llms.txt](https://docs.bluefletch.com/bluefletch-enterprise/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bluefletch.com/bluefletch-enterprise/product-guides/browser/features/custom-scripts.md). Agents can also search and read this documentation through its [MCP server](https://docs.bluefletch.com/bluefletch-enterprise/~gitbook/mcp).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
