flutter_webview_plugin icon indicating copy to clipboard operation
flutter_webview_plugin copied to clipboard

Extend webview with javascript interface functionality

Open DipoAreoye opened this issue 7 years ago • 10 comments

This could help support webapps, and enable cross communication between our flutter app and the webview as is possible with the native webview. Is this achievable?

DipoAreoye avatar Apr 27 '18 22:04 DipoAreoye

Probably possible, we should be able to create a bridge between the native class and the flutter code

lejard-h avatar Aug 01 '18 11:08 lejard-h

I have implemented on android ,sample code here: add webView.addJavascriptInterface(new FlutterJsInterface(), "Android"); after webView.setWebChromeClient in WebviewManager.java , the FlutterJsInterface class is `class FlutterJsInterface {

    @JavascriptInterface
    public void toService() {
        System.out.println("toservice");
        FlutterWebviewPlugin.channel.invokeMethod("toService", null);
    }
}

` in flutter ,

static const platform = const MethodChannel('flutter_webview_plugin'); platform.setMethodCallHandler((call) { if (call.method == "toService") { Navigator.of(context).push(WebViewRouter(builder: (context) { return new KefuPage(); })); } }); and the WebViewRouter is

` class WebViewRouter extends MaterialPageRoute { var plugin = FlutterWebviewPlugin(); WebViewRouter({ @required WidgetBuilder builder, RouteSettings settings, bool maintainState = true, bool fullscreenDialog = false, }) : super( builder: builder, settings: settings, maintainState: maintainState, fullscreenDialog: fullscreenDialog); @override TickerFuture didPush() { plugin.hide(); return super.didPush(); }

@override bool didPop(result) { plugin.show(); return super.didPop(result); } } `

sorry for the format ,this is my first comment with code

fujinjun avatar Nov 21 '18 03:11 fujinjun

@fujinjun maybe you could create PR? Since you already have code.

charafau avatar Nov 21 '18 10:11 charafau

@charafau I do not have much time on this,but you can see code in my fork https://github.com/fujinjun/flutter_webview_plugin

fujinjun avatar Nov 21 '18 11:11 fujinjun

Is this implemented? I need to add JavaScript communication on my webview

sakinaboriwala avatar Apr 18 '19 10:04 sakinaboriwala

@fujinjun I clone your code and i have been implement at my proj but don't work ,This is my code.

void main() => runApp(MyApp());

class MyApp extends StatefulWidget { @override State<StatefulWidget> createState() { // TODO: implement createState return _Main(); } }

class _Main extends State<MyApp> { final _webviewPlugin = FlutterWebviewPlugin(); static const platform = const MethodChannel('flutter_webview_plugin'); StreamSubscription _onDestroy;

@override void initState() { super.initState();

_webviewPlugin.onUrlChanged.listen((String url) {
  print(url);
});

platform.setMethodCallHandler((call) {
  if (call.method == "toService") {
    print('fblogin');
  }
});

_webviewPlugin.onDestroy.listen((_) => SystemNavigator.pop());

}

void dispose() { _webviewPlugin.dispose(); super.dispose(); }

@override Widget build(BuildContext context) { // TODO: implement build return MaterialApp( theme: new ThemeData(primaryColor: Colors.deepOrange), debugShowCheckedModeBanner: false, home: Scaffold( appBar: PreferredSize( child: Container(), preferredSize: Size.fromHeight(0.0), ), body: WebviewScaffold( url: selectedUrl, appCacheEnabled: true, withLocalStorage: true, withJavascript: true, supportMultipleWindows: true, hidden: true, withZoom: true, ), ), ); } }

don't continue to starting webpage. Pause at handler method.Pls help me

this is javascriptinterface code..

class FlutterJsInterface {

    @JavascriptInterface
    public void loginFacebook() {
        System.out.println("toservice");
        FlutterWebviewPlugin.channel.invokeMethod("toService", null);
    }
}

loginFacebook() is name of javascript from backend

heinzan avatar May 03 '19 03:05 heinzan

In android we an do this as shown below code: In Flutter WebViewController plugin how can I hide header, footer or show only part of the page that has div id and class id?

view.getSettings().setJavaScriptEnabled(true); view.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url)
        {
            view.loadUrl("javascript:(function() { " +
                    "var head = document.getElementsByClassName('header')[0].style.display='none'; " +
                    "var head = document.getElementsByClassName('blog-sidebar')[0].style.display='none'; " +
                    "var head = document.getElementsByClassName('footer-container')[0].style.display='none'; " +
                    "})()");

        }
    });
    view.loadUrl("your url");

NTMS2017 avatar May 17 '19 06:05 NTMS2017

@sakina1403 you can communicate with javascript using eval function on webview

charafau avatar Jun 11 '19 11:06 charafau

I solved this problem with dynamic channel name for Android and iOS #523. Hope it's helpful

rinlv avatar Aug 19 '19 07:08 rinlv

addJavascriptInterface didn't use the plugin?

mkbsugita avatar Mar 27 '23 04:03 mkbsugita