[英]“Uncaught SecurityError: Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs.” Flutter webview
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:此 CDN 抛出此错误:
Uncaught SecurityError: Failed to read the 'cookie' property from 'Document': Cookies are disabled inside 'data:' URLs." Flutter webview
未捕获的安全错误:无法从“文档”读取“cookie”属性:Cookies 在“数据:”URL 中被禁用。” Flutter Z5A98E2840FD0141780D854E48CZZ608D4
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.你可以试试我的插件flutter_inappwebview ,这是一个 Flutter 插件,允许你添加内联 WebViews 或打开应用内浏览器 window 并且有很多事件、方法和选项来控制 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
:在您的情况下,您可以使用
initialData
参数并通过InAppWebViewInitialData.data
属性设置自定义 HTML 并将 InAppWebViewInitialData.baseUrl 设置为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) {
},
))
])),
),
);
}
}
Now you have access to document.cookie
using JavaScript!现在您可以使用 JavaScript 访问
document.cookie
!
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.另一种方法是将您的 HTML 放入资产文件中(请参阅在资产文件夹中加载文件部分),然后,您可以使用
InAppLocalhostServer
启动本地服务器来为您的 HTML 文件提供脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.