"Search for interesting elephant facts." There's my big contribution to zoology. One minute later there's a list from DuckDuckGo, and by 7:59 my phone is telling me that a newborn calf weighs about 19 stone. Where did the 19 stone come from? I can tap the green tool result in the third picture and have a look. There's a link to "115 Amazing Facts about Elephants". I asked the phone to search. I didn't ask it to open any of those 115 facts.
I wanted to be able to do this with whatever model I happened to have open in
Cumbersome, including Ollama on my Mac. Apple had just the thing for it. In the
WWDC lakes demo (opens in new tab), go to about four minutes
in. You can have a WebPage load a site and let its JavaScript run. A WebView gives you a window
on it. Apple's presenter points out that "WebPage can be used completely on its own." That last bit
was where I went wrong.
What happens if I turn the window off?
Somebody asked a good question in 2017 (opens in new tab): "Does this work with content that is loaded dynamically with JS?" A site he'd used before would only scrape the HTML. At the time I was using PhantomJS to see what had changed on a webpage in Versionista. I said it worked pretty well (opens in new tab), though the page could take its time finishing. A couple of years later (opens in new tab) I was running Chrome on Fargate and shipping its HTML off to a Lambda to get the text out.
Fast forward to this little switch on my iPhone.
I'd wired the invisible fetch to URLSession. In December I served it a page with
<h1>Loading</h1> and some JavaScript to change the heading. Anybody sitting at the Mac could read
"Article is ready." Guess what my invisible browser was going to give the model? Loading.
I corrected the April post after fixing that. You can verify it yourself with a local HTML file. Put
<h1>Loading</h1><script>document.querySelector('h1').textContent = 'Article is ready'</script> in
it, serve it from localhost, and use the same address for both requests. Ask the viewless WebPage
for document.querySelector('h1').outerHTML after navigation. My Mac returned
<h1>Article is ready</h1>. That's the one I'd want to turn into Markdown.
Bad choice of address if I'm after a particular BBC report. Good choice if I want that morning's front page. Both are perfectly good things to ask for, so the tool returns the final URL along with the text. The search terms show up in the chat too.
The Swift that opened the page in April
@available(iOS 26.0, macOS 26.0, *)
private struct LocalBrowserToolModalModern: View {
var coordinator: LocalToolCoordinator
@State private var webPage = WebPage()
@State private var lastLoadedURL: URL?
var body: some View {
WebView(webPage)
.onAppear { startLoadingCurrentURLIfNeeded() }
.onChange(of: webPage.isLoading) { wasLoading, isLoading in
// Finished loading? Grab the rendered HTML and hand it to
// the extractor that feeds the AI's tool result.
guard wasLoading, !isLoading else { return }
Task {
try? await Task.sleep(for: .milliseconds(1200))
let html = try? await webPage.callJavaScript(
"return document.documentElement.outerHTML.toString();"
) as? String
coordinator.extractAndComplete(
renderedHTML: html,
finalURL: webPage.url
)
}
}
}
private func startLoadingCurrentURLIfNeeded() {
guard let url = coordinator.currentBrowserURL,
lastLoadedURL != url else { return }
lastLoadedURL = url
webPage.load(URLRequest(url: url))
}
}
Take away the view and there goes its .onAppear. Nothing would even start loading. My headless
tool starts its own navigation, reads the page, and hands it to the same extractor. About that 1.2
seconds in the sample: it was a little extra time for this visible sheet. If the next paragraph
arrives after two seconds, you're still going to miss it.
WebKit has navigation events (opens in new tab) so I can at least
tell when navigation finishes.
I'd gone looking for elephants again. The question for the human at the Mac this time was "Select all squares containing a duck." I kept the window.
This photo was taken with the switch off. On a new 2.3.1 install it's on. Settings > Tools is where to change it.
I use the same tools for direct and gateway keys. Vercel has since documented how to call its own search over HTTP (opens in new tab), another choice I didn't have instructions for in April. With a cloud model, the website still gets my phone's request, and the model still gets what I pull out of the page. For comparing answers in Face/Off I like being able to see exactly what that was. If I want to keep the reading on my device too, there's Endemic.
Bless up! 🙏✨