WebView

In-app browser for OAuth flows and web-based interactions.

Declare "WEBVIEW" in plugin.json capabilities. Opens an in-app browser window for OAuth login flows, captcha pages, or any web-based UI your plugin needs.

Consuming

Obtain the service in main() and pass it to implementations that need webview access (typically CoreAPI for authentication):

import dev.krtirtho.plugin_interfaces.host_apis.WebViewAPI
import dev.krtirtho.plugin_interfaces.host_apis.WebViewAPI_SERVICE_NAME

fun main() {
    val webView = zipline.take<WebViewAPI>(WebViewAPI_SERVICE_NAME)

    zipline.bind<CoreAPI>(CoreAPI_SERVICE_NAME, RealCoreAPI(storage, webView))
}

Interface

interface WebViewAPI : ZiplineService {
    fun navigateTo(url: String)
    fun navigateToHTML(html: String)
    suspend fun getCookies(url: String): List<Cookie>
    suspend fun evaluateJavaScript(script: String): String?
    fun urlChangeFlow(): Flow<String>
    fun webviewCreatedFlow(): Flow<Unit>
    fun postMessagesFlow(): Flow<String>
    fun exitWebView()
}

OAuth flow pattern

The typical pattern for authentication: navigate to the provider's authorize URL, watch the URL for the redirect callback, extract the auth code, exchange for tokens, store them, and close.

override suspend fun login() {
    val authUrl = "https://auth.example.com/authorize?client_id=..."
    webView.navigateTo(authUrl)

    webView.urlChangeFlow().collect { url ->
        if (url.startsWith("myapp://callback")) {
            val code = extractCode(url)
            val tokens = exchangeCodeForTokens(code)
            storage.putString("access_token", tokens.accessToken)
            (_loggedInFlow as MutableStateFlow).value = true
            webView.exitWebView()
        }
    }
}