pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.13k stars 1.44k forks source link

Window.print() with popups pages not working #2130

Open A7mdlbanna opened 3 months ago

A7mdlbanna commented 3 months ago

Environment

Technology Version
Flutter version 3.19.0
Plugin version 6.0.0
Android version 10+
iOS version
macOS version
Xcode version
Google Chrome version

Device information:

Description

I'm using the package to display a webpage that contains a print service done by popping a blank page containing an HTML receipt and then using window.print() js function Expected behavior: the blank page pops displaying the content and then the function is executed and an OS print page appears with the content

Current behavior: a blank page is popped but with a white background and no functions are exected the log says that the popped page is about:blank#blocked and the console log gives me a document error (probably for executing window.print() on a blocked page)

Steps to reproduce

  1. run the following code with any website that has a print mechanism

    Dart Code ```dart void main() async{ WidgetsFlutterBinding.ensureInitialized(); runApp(const WebViewScreen()); }

class WebViewScreen extends StatefulWidget { const WebViewScreen({Key? key}) : super(key: key);

@override State createState() => _WebViewScreenState(); }

class _WebViewScreenState extends State with WidgetsBindingObserver {

InAppWebViewController? webViewController; InAppWebViewSettings settings = InAppWebViewSettings( isInspectable: kDebugMode, mediaPlaybackRequiresUserGesture: false, allowsInlineMediaPlayback: true, iframeAllow: "camera; microphone", iframeAllowFullscreen: true);

PullToRefreshController? pullToRefreshController; String url = ""; double progress = 0; final urlController = TextEditingController();

@override void initState() { pullToRefreshController = kIsWeb ? null : PullToRefreshController( settings: PullToRefreshSettings( color: Colors.blue, ), onRefresh: () async { if (defaultTargetPlatform == TargetPlatform.android) { webViewController?.reload(); } else if (defaultTargetPlatform == TargetPlatform.iOS) { webViewController?.loadUrl( urlRequest: URLRequest(url: await webViewController?.getUrl())); } }, ); super.initState(); }

@override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: InAppWebView( initialUrlRequest: URLRequest(url: WebUri("")), initialSettings: settings, pullToRefreshController: pullToRefreshController, onWebViewCreated: (controller) async { webViewController = controller; setState(() {});

        if (defaultTargetPlatform != TargetPlatform.android || await WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
          await controller.addWebMessageListener(WebMessageListener(
            jsObjectName: "login",
            onPostMessage: (message, sourceOrigin, isMainFrame, replyProxy) {
              print('message $message');
            },
          ));
        }
      },
      onLoadStart: (controller, url) {
        setState(() {
          this.url = url.toString();
          urlController.text = this.url;
        });
      },
      onPermissionRequest: (controller, request) async {
        return PermissionResponse(
            resources: request.resources,
            action: PermissionResponseAction.GRANT);
      },
      shouldOverrideUrlLoading: (controller, navigationAction) async {
        print(navigationAction.request.url);
        var uri = navigationAction.request.url!;
        if (uri.toString().contains('wa.me')) {
          if(await canLaunchUrl(uri)){
            launchUrl(uri, mode: LaunchMode.externalApplication);
          }
          return NavigationActionPolicy.CANCEL;
        }

        return NavigationActionPolicy.ALLOW;
      },
      onLoadStop: (controller, url) async {
        pullToRefreshController?.endRefreshing();
        setState(() {
          this.url = url.toString();
          urlController.text = this.url;
        });
      },
      onReceivedError: (controller, request, error) {
        pullToRefreshController?.endRefreshing();
      },
      onProgressChanged: (controller, progress) {
        if (progress == 100) {
          pullToRefreshController?.endRefreshing();
        }
        setState(() {
          this.progress = progress / 100;
          urlController.text = url;
        });
      },
      onUpdateVisitedHistory: (controller, url, androidIsReload) {
        setState(() {
          this.url = url.toString();
          urlController.text = this.url;
        });
      },
      onConsoleMessage: (controller, consoleMessage) {
        if (kDebugMode) {
          print('Console: $consoleMessage');
        }
      },
    ),
  ),
);

} }

</details>

2. press the print button and wait for the result

## Stacktrace/Logcat <!-- if available, else delete -->  
<details>
  <summary>LOG</summary>

[AndroidInAppWebViewController] (android) WebView ID 0 calling "onTitleChanged" using {title: a.glary.sa/ar/invoices} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onTitleChanged" using {title: فواتير المبيعات | الإبداع للإدارة السحابية} I/AssistStructure( 5085): Flattened final assist data: 36300 bytes, containing 1 windows, 47 views I/AssistStructure( 5085): Flattened final assist data: 39020 bytes, containing 1 windows, 47 views I/AssistStructure( 5085): Flattened final assist data: 39372 bytes, containing 1 windows, 47 views W/RenderInspector( 5085): QueueBuffer time out on loai.glary.sa/com.glary.gmc.MainActivity, count=1, avg=20 ms, max=20 ms. I/flutter ( 5085): Console: ConsoleMessage{message: app loading .., messageLevel: LOG} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 1, message: app loading ..} I/flutter ( 5085): Console: ConsoleMessage{message: finish loading .., messageLevel: LOG} I/flutter ( 5085): Console: ConsoleMessage{message: [object Object], messageLevel: LOG} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 1, message: finish loading ..} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 1, message: [object Object]} [AndroidInAppWebViewController] (android) WebView ID 0 calling "shouldOverrideUrlLoading" using {request: {headers: null, method: GET, networkServiceType: null, allowsConstrainedNetworkAccess: null, cachePolicy: null, body: null, url: about:blank#blocked, allowsExpensiveNetworkAccess: null, attribution: null, assumesHTTP3Capable: null, httpShouldUsePipelining: null, allowsCellularAccess: null, httpShouldHandleCookies: null, timeoutInterval: null, mainDocumentURL: null}, sourceFrame: null, isRedirect: false, targetFrame: null, hasGesture: true, shouldPerformDownload: null, isForMainFrame: true, navigationType: null} I/flutter ( 5085): about:blank#blocked I/flutter ( 5085): Console: ConsoleMessage{message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document') I/flutter ( 5085): at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219) I/flutter ( 5085): at HTMLDocument. (https://a.glary.sa/js/app.js?3.412:2:656366) I/flutter ( 5085): at l (https://a.glary.sa/js/app.js?3.412:2:3740830) I/flutter ( 5085): at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined, messageLevel: WARNING} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onReceivedTouchIconUrl" using {precomposed: false, url: https://a.glary.sa/4545-04.ico} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 10} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 100} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 100} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onTitleChanged" using {title: a.glary.sa/ar/invoices} I/flutter ( 5085): Console: ConsoleMessage{message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document') I/flutter ( 5085): at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219) I/flutter ( 5085): at HTMLDocument. (https://a.glary.sa/js/app.js?3.412:2:656366) I/flutter ( 5085): at l (https://a.glary.sa/js/app.js?3.412:2:3740830) I/flutter ( 5085): at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined, messageLevel: WARNING} I/flutter ( 5085): Console: ConsoleMessage{message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document') I/flutter ( 5085): at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219) I/flutter ( 5085): at HTMLDocument. (https://a.glary.sa/js/app.js?3.412:2:656366) I/flutter ( 5085): at l (https://a.glary.sa/js/app.js?3.412:2:3740830) I/flutter ( 5085): at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined, messageLevel: WARNING} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 2, message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document') at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219) at HTMLDocument. (https://a.glary.sa/js/app.js?3.412:2:656366) at l (https://a.glary.sa/js/app.js?3.412:2:3740830) at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 10} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 2, message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document') at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219) at HTMLDocument. (https://a.glary.sa/js/app.js?3.412:2:656366) at l (https://a.glary.sa/js/app.js?3.412:2:3740830) at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onConsoleMessage" using {messageLevel: 2, message: jQuery.Deferred exception: Cannot read properties of null (reading 'document') TypeError: Cannot read properties of null (reading 'document') at Object.print (https://a.glary.sa/js/app.js?3.412:2:1140219) at HTMLDocument. (https://a.glary.sa/js/app.js?3.412:2:656366) at l (https://a.glary.sa/js/app.js?3.412:2:3740830) at u (https://a.glary.sa/js/app.js?3.412:2:3741132) undefined} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onLoadStart" using {url: about:blank#blocked} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onUpdateVisitedHistory" using {isReload: false, url: about:blank#blocked} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 100} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onProgressChanged" using {progress: 100} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onTitleChanged" using {title: about:blank#blocked} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onLoadStop" using {url: about:blank#blocked} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onZoomScaleChanged" using {oldScale: 2.75, newScale: 1.1030611991882324} [AndroidInAppWebViewController] (android) WebView ID 0 calling "onPageCommitVisible" using {url: about:blank#blocked} W/RenderInspector( 5085): QueueBuffer time out on loai.glary.sa/com.glary.gmc.MainActivity, count=1, avg=16 ms, max=16 ms. E/LongScreenshotUtils( 5085): inLargeScreen false I/LongScreenshotUtils( 5085): focus:true I/LongScreenshotUtils( 5085): current ratio width:1.0, height:1.0

github-actions[bot] commented 3 months ago

👋 @A7mdlbanna

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!