Executing JavaScript in the context of another io.Connect Window

Question

I want to execute a JavaScript function within the context of another window running in io.Connect Desktop, calling a function exposed by a third-party web application hosted in the platform.

I know this can be done outside io.Connect by creating an Electron window and using the Node APIs to execute JavaScript in the BrowserWindow. I’d prefer to do it within io.Connect, to take full advantage of its windowing and interop capabilities. Is there a supported way to do this?

Answer

Yes. Use the executeCode() method, which executes code in the context of an io.Connect Window. It is available on IOConnectWindow instances and was introduced in io.Connect Desktop 9.5.

Executing JavaScript code within the context of an io.Connect Window is disabled by default. To enable it, set the "allowScriptExecution" top-level property to true in the app definition of the application that will execute the code:

{
    "allowScriptExecution": true
}

To execute code, obtain a reference to the target io.Connect Window and call executeCode() on it, passing the code as a string. The method resolves with the result returned from the executed code, or rejects if the result of the code is a rejected Promise:

const ID = "2506_04";
const ioConnectWindow = io.windows.findById(ID);
const myCode = "(() => { return 42; })()";

const result = await ioConnectWindow.executeCode(myCode);
console.log(result);

References