“Uncaught SecurityError: Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs.” Flutter webview

泄露秘密 提交于 2021-01-29 09:24:46

问题


WebView(initialUrl:Uri.dataFromString('<script type="text/javascript" src="https://cdn.embedly.com/widgets/platform.js"></script>'+<html>Some code</html>,mimeType: 'text/html').toString(), javascriptMode: JavascriptMode.unrestricted,),

This CDN is throwing this error:

Uncaught SecurityError: Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs." Flutter webview


回答1:


You can try my plugin flutter_inappwebview, which is a Flutter plugin that allows you to add inline WebViews or open an in-app browser window and has a lot of events, methods, and options to control WebViews.

In your case, you can use the initialData argument and set your custom HTML through the InAppWebViewInitialData.data attribute and set the InAppWebViewInitialData.baseUrl to http://localhost:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  InAppWebViewController webView;
  String customHTML = "";

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

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('InAppWebView Example'),
        ),
        body: Container(
            child: Column(children: <Widget>[
          Expanded(
              child: InAppWebView(
                initialData: InAppWebViewInitialData(data: """
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>InAppWebViewInitialDataTest</title>
        <script type="text/javascript" src="https://cdn.embedly.com/widgets/platform.js"></script>
    </head>
    <body>
        $customHTML
    </body>
</html>
                    """, baseUrl: 'http://localhost'),
            initialHeaders: {},
            initialOptions: InAppWebViewGroupOptions(
              crossPlatform: InAppWebViewOptions(
                  debuggingEnabled: true,
              )
            ),
            onWebViewCreated: (InAppWebViewController controller) {
              webView = controller;
            },
            onLoadStart: (InAppWebViewController controller, String url) {

            },
            onLoadStop:(InAppWebViewController controller, String url) {

            },
          ))
        ])),
      ),
    );
  }
}

Now you have access to document.cookie using JavaScript!

Another way is to put your HTML in an asset file (see the Load a file inside assets folder Section) and, then, you can use InAppLocalhostServer to start a local server to serve your HTML file with your script.



来源:https://stackoverflow.com/questions/62021778/uncaught-securityerror-failed-to-read-the-cookie-property-from-document-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!