Skip to content

Instantly share code, notes, and snippets.

@tw-Frey
Created June 6, 2022 13:34
Show Gist options
  • Select an option

  • Save tw-Frey/33d6ede3137484655eef99a9b5d84bb3 to your computer and use it in GitHub Desktop.

Select an option

Save tw-Frey/33d6ede3137484655eef99a9b5d84bb3 to your computer and use it in GitHub Desktop.
透過 evaluateJavascript 取得 WebView 當前的連結

有時候用 WebView.url 得到的卻是 about:blank 或 沒有更新

例如 當使用 loadDataWithBaseURL 載頁面後, 變動 url fragment 時 WebView.url 沒有同步

有一些方式可以得到 WebView 當前連結 (含 url fragment)

下面是其中一種方式:

webView?.evaluateJavascript("location.href||''") { href ->
  val url = href.removeSurrounding("\"").takeUnless(CharSequence::isNullOrBlank) ?: return@evaluateJavascript
  // url 為 當前的連結
  ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment