exupero's blog

Obb script to execute JavaScript in an open browser

In the two previous posts I showed some ways I've used obb with Obsidian. One more way I've used obb is to execute JavaScript in an open browser.

At work we use a particular website for internal coordination, and I often want to launch the site from Alfred or target it specifically with my window manager of choice. The website doesn't have a standalone desktop app, so I've used WebCatalog to create one. WebCatalog creates an app by bundling the website into an instance of Chromium, which means we can interact with WebCatalog apps via Mac automation tooling the same way we can with Chromium.

For example, to execute arbitrary JavaScript files in the website, I have this obb script:

(let [[script] *command-line-args*
      app (doto (js/Application.currentApplication)
            (as-> $ (set! (.-includeStandardAdditions $) true)))]
  (-> (js/Application "Chromium")
      (aget 0)
      (.execute #js {:javascript (.read app script)})))

I use this script to create Alfred workflows that make API calls. Because the JavaScript executes within the same context as the website's UI, any AJAX calls implicitly include the cookies where authentication tokens are stored.