Skip to content

Run host scripts in web pages, and show pages only once they are ready - #52

Merged
514sid merged 1 commit into
mainfrom
page-scripts
Nov 23, 2026
Merged

514sid merged 1 commit into
mainfrom
page-scripts

Conversation

@514sid

@514sid 514sid commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Two additions for web assets, both opt-in per asset.

Page scripts

GaplessAsset.javaScript runs in every document the page loads, at document start (WebViewCompat.addDocumentStartJavaScript): before the page's own scripts and before anything is drawn. Styles it adds apply from the first paint, so a hidden cookie banner or scrollbar never flashes up. It runs again on each navigation and refresh. Typical uses: hiding banners, fitting a page to the screen, signing in, scrolling.

  • It runs only in the page's own origin (scheme, host, port), so a redirect to a sign-in page elsewhere doesn't get it. javaScriptOrigins allows others, or setOf("*") all. Non-http pages allow every origin by default.
  • The script registered for a slot is removed when the slot goes blank or loads another page, so it can't leak into the next asset.
  • WebView before 87 can't run scripts at document start; there the script runs at the first paint instead, with a warning.

Pages that say when they are ready

With waitForPageReady = true a page is ready to be shown when it calls gapless.ready(), not when it first paints, for dashboards and sign-ins that paint before they have anything to show. The page or javaScript makes the call. The page loads while the asset before it plays, so it is normally ready by its turn; one that never calls it is shown when play() stops waiting, as an unpainted page is. gapless is a JavascriptInterface on both WebViews; any page can call ready(), but all it decides is when the page is shown.

Sample app

  • web_script_check: the injected script marks that it ran and hides a fake red cookie banner; the page turns green with SCRIPT RAN FIRST, or red NO SCRIPT with the banner.
  • web_ready_8s: red LOADING, then after 8 seconds green READY and gapless.ready(), longer than the 6-second clip it preloads behind.
  • web_ready_never: never ready, for the timeout path.
  • The BBC, Guardian, CNN and AccuWeather pages get a consent banner hiding script; Wikipedia is shown once loaded and scrolls slowly. All in a web_scripts playlist.
  • --ez scripts false drops every script and readiness wait, to compare.

Checked on the TV (WebView 157), screenshots with and without

Page With Without
web_script_check green SCRIPT RAN FIRST, no banner red NO SCRIPT, banner showing
web_ready_8s, 0.3 s after it went on screen green READY (the cut waited for ready()) red LOADING

The live news sites show this TV no consent banner even without the script, so they couldn't confirm the banner hiding; the local check page does.

Robolectric tests cover readiness (not ready at the paint, ready before the paint, the timeout path, plain pages) and the at-paint script path with its origin check. They caught the bridge posting through view.post, which does nothing for a view not attached to a window; it uses a main-thread handler now.

Adds androidx.webkit 1.18.0 (released 2026-10-21).

GaplessAsset.javaScript runs in every document a web asset loads, at
document start through WebViewCompat.addDocumentStartJavaScript: before
the page's own scripts and its first paint, so a hidden cookie banner
never flashes up. It is limited to the page's own origin unless
javaScriptOrigins allows more, removed when the slot moves on, and on
WebView versions before 87 runs at the first paint instead.

waitForPageReady holds the cut until the page calls gapless.ready(),
for dashboards that paint before their data arrives. A page that never
calls it is shown when play() stops waiting, like a page that never
paints.

The sample app gets pages that check both (web_script_check,
web_ready_8s, web_ready_never) in a web_scripts playlist, consent banner
hiding on the news sites, a ready-and-scroll script on Wikipedia, and
--ez scripts false to compare without them. Adds androidx.webkit 1.18.0.
@514sid
514sid merged commit 1d78572 into main Nov 23, 2026
1 check passed
@514sid
514sid deleted the page-scripts branch November 23, 2026 17:43
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.

1 participant