Skip to content

Use evaluateJavascript in ArticleWebView and guard external startActivity calls#211

Open
jim-daf wants to merge 1 commit into
itkach:masterfrom
jim-daf:fix-webview-evaluatejs-and-trycatch
Open

Use evaluateJavascript in ArticleWebView and guard external startActivity calls#211
jim-daf wants to merge 1 commit into
itkach:masterfrom
jim-daf:fix-webview-evaluatejs-and-trycatch

Conversation

@jim-daf
Copy link
Copy Markdown

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

Closes #210.

Two related WebView hygiene fixes in ArticleWebView.

loadUrl("javascript:...") -> evaluateJavascript

onPageStarted and onPageFinished used the legacy loadUrl("javascript:...") form to inject the style switcher script. The Android docs deprecated that pattern in KitKat (4.4) in favour of WebView.evaluateJavascript, because the legacy form adds a navigation entry to the back/forward history on every call.

Both call sites now use view.evaluateJavascript(script, null). Same script payload, no back-stack pollution.

startActivity inside shouldOverrideUrlLoading

shouldOverrideUrlLoading previously dispatched two intents without try/catch:

  • An external ACTION_VIEW for off-app URLs (mailto:, tel:, custom schemes from dictionaries) which can raise ActivityNotFoundException if no handler is installed and crash the host activity.
  • An internal launch of ArticleCollectionActivity for localhost article links.

Both calls are now wrapped in try { ... } catch (ActivityNotFoundException e). The external case logs and falls through silently. The internal case logs and returns false, so the WebView keeps the existing page if the dispatch ever fails.

…vity calls

Two related WebView hygiene fixes in ArticleWebView.

1) Legacy javascript: URI form (D06)

   The onPageStarted and onPageFinished handlers used:

       view.loadUrl("javascript:" + styleSwitcherJs);
       view.loadUrl("javascript:" + styleSwitcherJs +
               ";$SLOB.setStyleTitles($styleSwitcher.getTitles())");

   loadUrl("javascript:...") was deprecated in Android 4.4 in
   favour of WebView.evaluateJavascript. The legacy form adds a
   navigation entry to the back/forward history, so the style
   bootstrap runs on every onPageStarted/onPageFinished pollute
   the back stack on the article view.

   Switch both call sites to view.evaluateJavascript(script, null).

2) startActivity without try/catch inside shouldOverrideUrlLoading
   (U04)

   shouldOverrideUrlLoading dispatched two intents directly:

       Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
       getContext().startActivity(browserIntent);

       Intent intent = new Intent(getContext(),
               ArticleCollectionActivity.class);
       intent.setData(uri);
       getContext().startActivity(intent);

   If the device has no app registered to handle ACTION_VIEW for
   that scheme, startActivity raises ActivityNotFoundException and
   the activity hosting the WebView crashes. The second call is
   internal and should not raise, but ActivityNotFound is still
   theoretically reachable through component aliasing or split
   profiles.

   Wrap both in try { ... } catch (ActivityNotFoundException e)
   { Log.w(...) }. The external case falls through (the user sees
   no app open). The internal case logs and returns false so the
   WebView keeps the existing page.

Assisted-by: Claude (Anthropic)
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.

ArticleWebView uses legacy loadUrl("javascript:...") and dispatches startActivity without try/catch

1 participant