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

Cross Communication from intented site #259

Closed
geneho opened this issue Jan 15, 2020 · 2 comments
Closed

Cross Communication from intented site #259

geneho opened this issue Jan 15, 2020 · 2 comments
Labels

Comments

@geneho
Copy link

geneho commented Jan 15, 2020

Hi all,

Currently im using https://github.com/fluttercommunity/flutter_webview_plugin and would like to migrate over. I'm having some issues with the cross communication portion.

I know roughly that I have to make use of flutter_inappwebview.callHandler and addJavaScriptHandler functions but having some issues making it work.

Could anyone point me in the right direction? Thank you.

In the previous plugin , I can make use of javascript channels to do it as per below:

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

final Set<JavascriptChannel> jsChannels = [
  JavascriptChannel(
      name: 'Print',
      onMessageReceived: (JavascriptMessage message) {
        print('message.message: ${message.message}');
      }),
].toSet();

void main() {
  final flutterWebviewPlugin = FlutterWebviewPlugin();
  flutterWebviewPlugin.onStateChanged.listen((state) async {
    if (state.type == WebViewState.finishLoad) {
      String script =
          'window.addEventListener("message", receiveMessage, false);' +
              'function receiveMessage(event) {Print.postMessage(event.data);}';
      flutterWebviewPlugin.evalJavascript(script);
    }
  });
  return runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        "/": (_) => WebviewScaffold(
              url: Uri.dataFromString(
                      '<html><button onclick="window.postMessage(\'test\', \'*\');">Click me</button></html>',
                      mimeType: 'text/html')
                  .toString(),
              appBar: new AppBar(title: new Text("Widget webview")),
              javascriptChannels: jsChannels,
            ),
      },
    );
  }
}
@geneho geneho changed the title Cross Communication from my intented site Cross Communication from intented site Jan 15, 2020
This was referenced Jul 6, 2020
Copy link

github-actions bot commented Oct 7, 2024

This issue is stale and has been automatically closed because it has been open for more than 365 days with no activity. Please reopen a new issue if you still have it.

@github-actions github-actions bot added the stale label Oct 7, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 7, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant