Skip to content

Commit

Permalink
Merge pull request #159 from manunite/feature/browserId_inject_script
Browse files Browse the repository at this point in the history
fix: Modified to manage scripts injected per browser ID
  • Loading branch information
hlwhl authored Nov 28, 2024
2 parents 16c4b25 + a6607ed commit ebd5319
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/src/webview_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class WebviewManager extends ValueNotifier<bool> {
final MethodChannel pluginChannel = const MethodChannel("webview_cef");

final Map<int, WebViewController> _webViews = <int, WebViewController>{};
final Map<int, InjectUserScripts?> _injectUserScripts = <int, InjectUserScripts>{};

final Map<int, WebViewController> _tempWebViews = <int, WebViewController>{};
InjectUserScripts? _injectUserScripts = InjectUserScripts();
final Map<int, InjectUserScripts?> _tempInjectUserScripts = <int, InjectUserScripts>{};

int nextIndex = 1;

Expand All @@ -33,7 +34,7 @@ class WebviewManager extends ValueNotifier<bool> {
final controller =
WebViewController(pluginChannel, browserIndex, loading: loading);
_tempWebViews[browserIndex] = controller;
_injectUserScripts = injectUserScripts;
_tempInjectUserScripts[browserIndex] = injectUserScripts;

return controller;
}
Expand Down Expand Up @@ -74,7 +75,10 @@ class WebviewManager extends ValueNotifier<bool> {

void onBrowserCreated(int browserIndex, int browserId) {
_webViews[browserId] = _tempWebViews[browserIndex]!;
_injectUserScripts[browserId] = _tempInjectUserScripts[browserIndex];

_tempWebViews.remove(browserIndex);
_tempInjectUserScripts.remove(browserIndex);
}

Future<void> methodCallhandler(MethodCall call) async {
Expand Down Expand Up @@ -134,7 +138,7 @@ class WebviewManager extends ValueNotifier<bool> {
int browserId = call.arguments["browserId"] as int;
String urlId = call.arguments["urlId"] as String;

await _injectUserScriptIfNeeds(browserId, _injectUserScripts?.retrieveLoadStartInjectScripts() ?? []);
await _injectUserScriptIfNeeds(browserId, _injectUserScripts[browserId]?.retrieveLoadStartInjectScripts() ?? []);

WebViewController controller =
_webViews[browserId] as WebViewController;
Expand All @@ -144,7 +148,7 @@ class WebviewManager extends ValueNotifier<bool> {
int browserId = call.arguments["browserId"] as int;
String urlId = call.arguments["urlId"] as String;

await _injectUserScriptIfNeeds(browserId, _injectUserScripts?.retrieveLoadEndInjectScripts() ?? []);
await _injectUserScriptIfNeeds(browserId, _injectUserScripts[browserId]?.retrieveLoadEndInjectScripts() ?? []);

WebViewController controller =
_webViews[browserId] as WebViewController;
Expand Down

0 comments on commit ebd5319

Please sign in to comment.