【问题标题】:"Uncaught SecurityError: Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs." Flutter webview“未捕获的安全错误:无法从 'Document' 读取 'cookie' 属性:在 'data:' URL 中禁用 Cookie。”颤振网页视图
【发布时间】:2020-09-13 05:37:56
【问题描述】:
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,),

此 CDN 抛出此错误:

未捕获的安全错误:无法从 'Document' 读取 'cookie' 属性:Cookie 在 'data:' URL 中被禁用。" Flutter webview

【问题讨论】:

  • 这里有几个问题,我不确定它们是否与您的问题有关。您能否提供有关您要实现的目标的更多背景信息?你的环境是什么?您还应该修复您的代码,因为它无效。例如,您在调用的函数的第一个参数旁边有 HTML 标记。
  • WebView(initialUrl:Uri.dataFromString('hml code here',mimeType: 'text/html').toString(), javascriptMode: JavascriptMode.unrestricted,),其实我想用javascript cdn在这个html代码中......我想知道我应该如何在webview中使用这个脚本cdn??
  • 考虑更新您的原始帖子。注释中的代码不可读。

标签: javascript flutter cookies webview


【解决方案1】:

你可以试试我的插件flutter_inappwebview,这是一个 Flutter 插件,可以让你添加内联 WebViews 或打开应用内浏览器窗口,并且有很多事件、方法和选项来控制 WebViews。

在您的情况下,您可以使用initialData 参数并通过InAppWebViewInitialData.data 属性设置您的自定义HTML,并将InAppWebViewInitialData.baseUrl 设置为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) {

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

现在您可以使用 JavaScript 访问document.cookie

另一种方法是将您的 HTML 放入资产文件中(请参阅 Load a file inside assets folder 部分),然后您可以使用 InAppLocalhostServer 启动本地服务器以使用您的脚本为您的 HTML 文件提供服务。

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2022-11-23
    • 2015-10-20
    • 2021-06-16
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多