removeUserScriptsByGroupName method

Future<void> removeUserScriptsByGroupName({
  1. required String groupName,
})

Removes all the UserScripts with groupName as group name from the webpage’s content. User scripts already loaded into the webpage's content cannot be removed. This will have effect only on the next page load.

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.

Implementation

Future<void> removeUserScriptsByGroupName({required String groupName}) async {
  assert(_webview?.windowId == null ||
      defaultTargetPlatform != TargetPlatform.iOS);

  final List<UserScript> userScriptsAtDocumentStart = List.from(
      _userScripts[UserScriptInjectionTime.AT_DOCUMENT_START] ?? []);
  for (final userScript in userScriptsAtDocumentStart) {
    if (userScript.groupName == groupName) {
      _userScripts[userScript.injectionTime]?.remove(userScript);
    }
  }

  final List<UserScript> userScriptsAtDocumentEnd =
      List.from(_userScripts[UserScriptInjectionTime.AT_DOCUMENT_END] ?? []);
  for (final userScript in userScriptsAtDocumentEnd) {
    if (userScript.groupName == groupName) {
      _userScripts[userScript.injectionTime]?.remove(userScript);
    }
  }

  Map<String, dynamic> args = <String, dynamic>{};
  args.putIfAbsent('groupName', () => groupName);
  await _channel.invokeMethod('removeUserScriptsByGroupName', args);
}