Flutter webview find difficulties to get PWA button action

I’ve a flutter app and this app also contain a pwa. I use in webview_flutter.
In pwa I have a button “share”. What I need is:
When I tap on on this share button inside the pwa I need the flutter app open the share widget.

I start to try using method as addJavaScriptChannel, runJavaScript as in the code I add below.

The problem is that I can’t get the click action of that button.
This button is make in this way by watching it in inspect.enter image description here

I try several times, this is one of the latest times. I need to understand how to get the result. Also in different ways.

I can also contact the dev who create that button to change small things.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:webview_flutter/webview_flutter.dart';
Widget inAppWebViewCustom({
required String url,
required SharedPreferences prefs,
bool hideHeaderFooter = false,
}) {
final RxBool isLoading = true.obs;
final WebViewController controller = WebViewController();
controller.setJavaScriptMode(JavaScriptMode.unrestricted);
controller.setBackgroundColor(const Color(0x00000000));
Timer? timer;
void saveGetTokenFunction({
required bool hideHeaderFooter,
required Rx<bool> isLoading,
required WebViewController controller,
required SharedPreferences prefs,
}) async {
isLoading.value = false;
if (hideHeaderFooter) {
await controller.runJavaScript('''
document.querySelector('header').style.display = 'none';
document.querySelector('footer').style.display = 'none';
''');
}
final Object? accessToken = await controller.runJavaScriptReturningResult(
'localStorage.getItem("accessToken");',
);
if (accessToken != null &&
accessToken != '' &&
!accessToken.toString().contains('null') &&
appToken.value != accessToken.toString()) {
await prefs.setString(kAccessToken, accessToken.toString());
timer?.cancel();
appToken.value = accessToken.toString().replaceAll('"', '');
} else {
if (appToken.value != '') {
await controller.runJavaScript(
'window.localStorage.setItem("accessToken", "${appToken.value})";',
);
timer?.cancel();
}
}
}
controller.addJavaScriptChannel(
'mychannel',
onMessageReceived: (JavaScriptMessage message) {
if (message.message == 'share') {
Share.share('Text to share', subject: 'subject');
}
},
);
controller.setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) async {
if (progress == 100) isLoading.value = false;
},
onPageStarted: (String url) async {
if (appToken.value != null &&
appToken.value != '' &&
!appToken.value.toString().contains('null')) {
await controller.runJavaScript(
'window.localStorage.setItem("accessToken", "${appToken.value})";',
);
}
},
onPageFinished: (String url) async {
timer = Timer.periodic(
const Duration(seconds: 1),
(Timer t) => saveGetTokenFunction(
isLoading: isLoading,
controller: controller,
prefs: prefs,
hideHeaderFooter: hideHeaderFooter,
),
);
},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
return NavigationDecision.navigate;
},
onUrlChange: (UrlChange UrlChange) async {
await controller.runJavaScript('''
const button = document.getElementsByClassName('MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textInherit MuiButton-sizeMedium MuiButton-textSizeMedium MuiButton-colorInherit MuiButton-disableElevation MuiButton-root MuiButton-text MuiButton-textInherit MuiButton-sizeMedium MuiButton-textSizeMedium MuiButton-colorInherit MuiButton-disableElevation css-1a0ejbe');
mychannel.postMessage('share');
''');
},
),
);
controller.loadRequest(Uri.parse(url));
return Stack(
children: <Widget>[
if (isLoading.value)
const Center(
child: CircularProgressIndicator(
color: AppColors.primary,
),
)
else
Container(),
WebViewWidget(controller: controller),
],
);
}
</code>
<code>import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:share_plus/share_plus.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:webview_flutter/webview_flutter.dart'; Widget inAppWebViewCustom({ required String url, required SharedPreferences prefs, bool hideHeaderFooter = false, }) { final RxBool isLoading = true.obs; final WebViewController controller = WebViewController(); controller.setJavaScriptMode(JavaScriptMode.unrestricted); controller.setBackgroundColor(const Color(0x00000000)); Timer? timer; void saveGetTokenFunction({ required bool hideHeaderFooter, required Rx<bool> isLoading, required WebViewController controller, required SharedPreferences prefs, }) async { isLoading.value = false; if (hideHeaderFooter) { await controller.runJavaScript(''' document.querySelector('header').style.display = 'none'; document.querySelector('footer').style.display = 'none'; '''); } final Object? accessToken = await controller.runJavaScriptReturningResult( 'localStorage.getItem("accessToken");', ); if (accessToken != null && accessToken != '' && !accessToken.toString().contains('null') && appToken.value != accessToken.toString()) { await prefs.setString(kAccessToken, accessToken.toString()); timer?.cancel(); appToken.value = accessToken.toString().replaceAll('"', ''); } else { if (appToken.value != '') { await controller.runJavaScript( 'window.localStorage.setItem("accessToken", "${appToken.value})";', ); timer?.cancel(); } } } controller.addJavaScriptChannel( 'mychannel', onMessageReceived: (JavaScriptMessage message) { if (message.message == 'share') { Share.share('Text to share', subject: 'subject'); } }, ); controller.setNavigationDelegate( NavigationDelegate( onProgress: (int progress) async { if (progress == 100) isLoading.value = false; }, onPageStarted: (String url) async { if (appToken.value != null && appToken.value != '' && !appToken.value.toString().contains('null')) { await controller.runJavaScript( 'window.localStorage.setItem("accessToken", "${appToken.value})";', ); } }, onPageFinished: (String url) async { timer = Timer.periodic( const Duration(seconds: 1), (Timer t) => saveGetTokenFunction( isLoading: isLoading, controller: controller, prefs: prefs, hideHeaderFooter: hideHeaderFooter, ), ); }, onWebResourceError: (WebResourceError error) {}, onNavigationRequest: (NavigationRequest request) { return NavigationDecision.navigate; }, onUrlChange: (UrlChange UrlChange) async { await controller.runJavaScript(''' const button = document.getElementsByClassName('MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textInherit MuiButton-sizeMedium MuiButton-textSizeMedium MuiButton-colorInherit MuiButton-disableElevation MuiButton-root MuiButton-text MuiButton-textInherit MuiButton-sizeMedium MuiButton-textSizeMedium MuiButton-colorInherit MuiButton-disableElevation css-1a0ejbe'); mychannel.postMessage('share'); '''); }, ), ); controller.loadRequest(Uri.parse(url)); return Stack( children: <Widget>[ if (isLoading.value) const Center( child: CircularProgressIndicator( color: AppColors.primary, ), ) else Container(), WebViewWidget(controller: controller), ], ); } </code>
import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:webview_flutter/webview_flutter.dart';

Widget inAppWebViewCustom({
  required String url,
  required SharedPreferences prefs,
  bool hideHeaderFooter = false,
}) {
  final RxBool isLoading = true.obs;
  final WebViewController controller = WebViewController();
  controller.setJavaScriptMode(JavaScriptMode.unrestricted);
  controller.setBackgroundColor(const Color(0x00000000));
  Timer? timer;

  void saveGetTokenFunction({
    required bool hideHeaderFooter,
    required Rx<bool> isLoading,
    required WebViewController controller,
    required SharedPreferences prefs,
  }) async {
    isLoading.value = false;
    if (hideHeaderFooter) {
      await controller.runJavaScript('''
          document.querySelector('header').style.display = 'none';
          document.querySelector('footer').style.display = 'none';
        ''');
    }
    final Object? accessToken = await controller.runJavaScriptReturningResult(
      'localStorage.getItem("accessToken");',
    );
    if (accessToken != null &&
        accessToken != '' &&
        !accessToken.toString().contains('null') &&
        appToken.value != accessToken.toString()) {
      await prefs.setString(kAccessToken, accessToken.toString());
      timer?.cancel();
      appToken.value = accessToken.toString().replaceAll('"', '');
    } else {
      if (appToken.value != '') {
        await controller.runJavaScript(
          'window.localStorage.setItem("accessToken", "${appToken.value})";',
        );
        timer?.cancel();
      }
    }
  }
  controller.addJavaScriptChannel(
    'mychannel',
    onMessageReceived: (JavaScriptMessage message) {
      if (message.message == 'share') {
        Share.share('Text to share', subject: 'subject');
      }
    },
  );

  controller.setNavigationDelegate(
    NavigationDelegate(
        onProgress: (int progress) async {
          if (progress == 100) isLoading.value = false;
        },
        onPageStarted: (String url) async {
          if (appToken.value != null &&
              appToken.value != '' &&
              !appToken.value.toString().contains('null')) {
            await controller.runJavaScript(
              'window.localStorage.setItem("accessToken", "${appToken.value})";',
            );
          }


        },
        onPageFinished: (String url) async {
          timer = Timer.periodic(
            const Duration(seconds: 1),
            (Timer t) => saveGetTokenFunction(
              isLoading: isLoading,
              controller: controller,
              prefs: prefs,
              hideHeaderFooter: hideHeaderFooter,
            ),
          );
        },
        onWebResourceError: (WebResourceError error) {},
        onNavigationRequest: (NavigationRequest request) {
          return NavigationDecision.navigate;
        },
      onUrlChange: (UrlChange UrlChange) async {

        await controller.runJavaScript('''
      const button = document.getElementsByClassName('MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textInherit MuiButton-sizeMedium MuiButton-textSizeMedium MuiButton-colorInherit MuiButton-disableElevation MuiButton-root MuiButton-text MuiButton-textInherit MuiButton-sizeMedium MuiButton-textSizeMedium MuiButton-colorInherit MuiButton-disableElevation css-1a0ejbe'); 
      mychannel.postMessage('share');
      ''');
      },
        ),
  );

  controller.loadRequest(Uri.parse(url));

  return Stack(
    children: <Widget>[
      if (isLoading.value)
        const Center(
          child: CircularProgressIndicator(
            color: AppColors.primary,
          ),
        )
      else
        Container(),
      WebViewWidget(controller: controller),
    ],
  );
}


Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật