WebView-CG / usage-and-challenges

Documenting usage scenarios for WebView and the challenges they create
https://webview-cg.github.io/usage-and-challenges/
Other
12 stars 4 forks source link

UserScript injection in WebView #10

Closed Token-LiMing closed 2 years ago

Token-LiMing commented 2 years ago

UserScript injection in WebView

Submitter(s)

Jiasheng Wu

Motivation

In daily work, we need to injects the specified script into the webpage’s content at a specific time for advanced customization features. For example, network requests monitor, custom css for element, performance data collection. For different features, we want to control code's injection time(document start, document end), control that whether to inject the script into the main frame or all frames. Perhaps we want to control injected document, only inject javascrip for one party website.

Stakeholders

Stakeholders include:

WebView provider: Apple, Google, deciders on how to enhance WebView's user script injection capability. WebView user: Native Business rely on WebView to build advanced customization features by javascript. End user: Users of the App indirectly using the Features implemented by WebView.

Analysis

WKUserScript in WKWebView provide develop with user script injection capability. It's hard to use android WebView to inject javascript before html loaded. We cannot unregister WKUserScript or control injected website in WKWebView.

Related W3C deliverables and/or work items

Perhaps the specification could mandate that all WebViews must provide develop with user script injection capability.

How is the issue solved in the Browser, and what’s more is needed?

  1. In Safari, we can inject a script that we have written from a Safari app extension into a webpage. Injecting a Script into a Webpage
  2. Maybe, user script can communicate with other user script.
tomayac commented 2 years ago

This is used heavily in opinionated browsers. See the screenshot in https://github.com/WebView-CG/usage-and-challenges/issues/4 for an example.