All Downloads are FREE. Search and download functionalities are using the official Maven repository.

desktopMain.com.multiplatform.webview.web.WebEngineExt.kt Maven / Gradle / Ivy

There is a newer version: 1.9.40
Show newest version
package com.multiplatform.webview.web

import co.touchlab.kermit.Logger
import org.cef.CefSettings
import org.cef.browser.CefBrowser
import org.cef.browser.CefFrame
import org.cef.handler.CefDisplayHandler
import org.cef.handler.CefLoadHandler
import org.cef.network.CefRequest

/**
 * Created By Kevin Zou On 2023/9/12
 */
internal fun CefBrowser.getCurrentUrl(): String? {
    return this.url
}

internal fun CefBrowser.addDisplayHandler(state: WebViewState) {
    this.client.addDisplayHandler(object : CefDisplayHandler {
        override fun onAddressChange(browser: CefBrowser?, frame: CefFrame?, url: String?) {
            state.lastLoadedUrl = getCurrentUrl()
        }

        override fun onTitleChange(browser: CefBrowser?, title: String?) {
            Logger.i { "titleProperty: $title" }
            state.pageTitle = title
        }

        override fun onTooltip(browser: CefBrowser?, text: String?): Boolean {
            return false
        }

        override fun onStatusMessage(browser: CefBrowser?, value: String?) {}

        override fun onConsoleMessage(
            browser: CefBrowser?,
            level: CefSettings.LogSeverity?,
            message: String?,
            source: String?,
            line: Int
        ): Boolean {
            return false
        }

        override fun onCursorChange(browser: CefBrowser?, cursorType: Int): Boolean {
            return false
        }

    })
}

internal fun CefBrowser.addLoadListener(state: WebViewState, navigator: WebViewNavigator) {
    this.client.addLoadHandler(object : CefLoadHandler {
        override fun onLoadingStateChange(
            browser: CefBrowser?,
            isLoading: Boolean,
            canGoBack: Boolean,
            canGoForward: Boolean
        ) {
            if (isLoading) {
                state.loadingState = LoadingState.Initializing
            } else {
                state.loadingState = LoadingState.Finished
            }
            navigator.canGoBack = canGoBack
            navigator.canGoForward = canGoForward
        }

        override fun onLoadStart(
            browser: CefBrowser?,
            frame: CefFrame?,
            transitionType: CefRequest.TransitionType?
        ) {
            Logger.i { "Load Start" }
            state.loadingState = LoadingState.Loading(0F)
        }

        override fun onLoadEnd(browser: CefBrowser?, frame: CefFrame?, httpStatusCode: Int) {
            Logger.i { "Load End" }
            state.loadingState = LoadingState.Finished
            navigator.canGoBack = canGoBack()
            navigator.canGoBack = canGoForward()
            state.lastLoadedUrl = getCurrentUrl()
        }

        override fun onLoadError(
            browser: CefBrowser?,
            frame: CefFrame?,
            errorCode: CefLoadHandler.ErrorCode?,
            errorText: String?,
            failedUrl: String?
        ) {
            state.loadingState = LoadingState.Finished
            state.errorsForCurrentRequest.add(
                WebViewError(
                    code = errorCode?.code ?: 404,
                    description = "Failed to load url: ${failedUrl}\n$errorText"
                )
            )
        }

    })
}

// can be used for more workarounds as well
internal fun String.applyWorkaroundsForHtmlString(): String {
    val removeHexCharHtml =
        "#(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})".toRegex().replace(this) { matchResult ->
            matchResult.value.substring(1)
        }
    return removeHexCharHtml
}

internal fun String.toDataUri(): String {
    return "data:text/html,${this.applyWorkaroundsForHtmlString()}"
}

internal fun CefBrowser.loadHtml(html: String) {
    this.loadURL(html.toDataUri())
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy