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:

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);

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.

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.

For a list of available actions, refer to the Page Action section in the technical guide.

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.

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

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.

Last updated