Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Modified to manage scripts injected per browser ID #159

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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