Need to control jupyterlab javascript/python code execution

I am in the process of embedding the jupyterlab notebook App in a host App. I have the following needs:

  1. Intercept javascript code during cell execution and have it sent to the host App for execution instead of sending it to the kernel for execution on the server. The output from the execution then has to be displayed in the output area in context. In other words, javascript code is executed locally within the browser.

  2. Similarly intercept python code, and if it contains a string within recognized delimiters (such as between ‘%’ character), make an appropriate replacement before sending it to the kernel for execution.

Wondering whether it is possible to accomplish this through the jupyterlab extension mechanism, and specifically what objects should I be looking to provide. Helpful guidance is appreciated very much. I want to add that I have some familiarity with the general extension set up process.

You can get a reference to the current NotebookPanel by requiring INotebookTracker (Common Extension Points — JupyterLab 3.2.1 documentation). Once you have a reference to a NotebookPanel, then you can get its Notebook from its content property. Further, the cells and their respective output can be found in the Notebook’s widgets array.