Skip to content

fix(direct-edit): route off-server links out of the editor WebView#3192

Open
jim-daf wants to merge 1 commit into
nextcloud:mainfrom
jim-daf:fix/direct-edit-webview-external-link-routing
Open

fix(direct-edit): route off-server links out of the editor WebView#3192
jim-daf wants to merge 1 commit into
nextcloud:mainfrom
jim-daf:fix/direct-edit-webview-external-link-routing

Conversation

@jim-daf
Copy link
Copy Markdown

@jim-daf jim-daf commented May 16, 2026

Closes #3191.

The NoteDirectEditFragment WebView loads the Nextcloud Text editor for the user's account but does not override shouldOverrideUrlLoading. Any link the editor renders (a markdown link to another site, a mailto: in a frontmatter table, a mention to a user on a different Nextcloud server) navigates the editor WebView itself, which drops the user out of the edit session.

Change

Override shouldOverrideUrlLoading. Keep navigations whose URL starts with the current account URL inside the WebView (so server-internal links the editor produces continue to work). Route everything else out via Intent.ACTION_VIEW. Wrap the launch in try/catch (ActivityNotFoundException) so a missing handler does not crash the Fragment:

override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
    val url = request?.url ?: return false
    val scheme = url.scheme?.lowercase()
    val accountUrl = runCatching { account.url }.getOrNull()
    if ((scheme == "http" || scheme == "https") &&
        accountUrl != null && url.toString().startsWith(accountUrl)
    ) {
        return false
    }
    return try {
        val intent = Intent(Intent.ACTION_VIEW, url).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        }
        startActivity(intent)
        true
    } catch (e: ActivityNotFoundException) {
        Log.w(TAG, "No app available to open $url", e)
        true
    }
}

The runCatching around account.url is so a transient access failure here cannot crash the WebViewClient callback. If the account URL is not available we just fall through to the external-route branch instead of blowing up.

Behaviour

  • Same-server https link inside the editor (the case the editor actually produces): unchanged, stays in the WebView.
  • External https link tapped from a note: opens in the system browser.
  • mailto:, tel:, intent: link tapped from a note: opens in the matching system app.
  • No handler installed: warning logged, no crash.

The direct-edit WebView loads the Nextcloud Text editor for the user's
account but never overrides shouldOverrideUrlLoading, so any link the
editor renders (mentions to other users, file references, embedded
images, mailto: in a markdown table) navigates the editor WebView itself
and breaks the edit session.

Override shouldOverrideUrlLoading to keep navigations whose URL starts
with the current account URL inside the WebView and route everything
else out via Intent.ACTION_VIEW. Wrap the launch in try/catch so a
missing handler does not crash the Fragment.
@codacy-production
Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 1 minor

Alerts:
⚠ 1 issue (≤ 0 issues of at least minor severity)

Results:
1 new issue

Category Results
Complexity 1 minor

View in Codacy

🟢 Metrics 0 duplication

Metric Results
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NoteDirectEditFragment WebView navigates itself for off-server links

1 participant