Skip to content

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

Description

@jim-daf

Two related WebView hygiene issues in src/itkach/aard2/ArticleWebView.java.

1. loadUrl("javascript:...") (D06)

onPageStarted and onPageFinished inject the style switcher script using the legacy form:

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 page event and pollutes the article view's back stack.

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

if (isExternal(uri)) {
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
    getContext().startActivity(browserIntent);
    return true;
}
...
if (scheme.equals("http") && host.equals(LOCALHOST) && uri.getQueryParameter("blob") == null) {
    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 a given scheme (mailto:, tel:, custom schemes from dictionaries) the first startActivity raises ActivityNotFoundException and the host activity crashes. The internal ArticleCollectionActivity case is less likely to throw but is still theoretically reachable.

Suggested fix

  1. Swap both loadUrl("javascript:..") calls to view.evaluateJavascript(script, null).
  2. Wrap the two startActivity calls in try { ... } catch (ActivityNotFoundException e) { Log.w(...) }. The external case falls through silently (the user sees no app open). The internal case logs and returns false so the WebView keeps the existing page.

A PR is open at #211.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions