addUserScript method

Future<void> addUserScript({
  1. required UserScript userScript,
})

Injects the specified userScript into the webpage’s content.

NOTE for iOS: this method will throw an error if the WebView.windowId has been set. There isn't any way to add/remove user scripts specific to iOS window WebViews. This is a limitation of the native iOS WebKit APIs.

Official iOS API: https://developer.apple.com/documentation/webkit/wkusercontentcontroller/1537448-adduserscript

Implementation

Future<void> addUserScript({required UserScript userScript}) async {
  assert(_webview?.windowId == null ||
      defaultTargetPlatform != TargetPlatform.iOS);

  Map<String, dynamic> args = <String, dynamic>{};
  args.putIfAbsent('userScript', () => userScript.toMap());
  if (!(_userScripts[userScript.injectionTime]?.contains(userScript) ??
      false)) {
    _userScripts[userScript.injectionTime]?.add(userScript);
    await _channel.invokeMethod('addUserScript', args);
  }
}