如何在Android WebView中禁用内容安全策略?

huangapple go评论92阅读模式
英文:

How to disable Content Security Policy in Android WebView?

问题

我正在尝试在 WebView 中加载 TikTok 网站。但是我无法加载成功。在 logcat 中我得到了这个错误:

"[仅报告] 拒绝加载脚本 'https://sf16-scmcdn-va.ibytedtos.com/goofy/slardar/fe/sdk/plugins/monitors.3.5.4.maliva.js',因为它违反了以下内容安全策略指令:'script-src 'self' blob: data: 'unsafe-inline' 'unsafe-eval' polyfill.io *.kakao.com *.cdn-apple.com *.tiktokcdn.com *.byteoversea.com *.tiktok.com'。",
来源:https://www.tiktok.com/ (0)

似乎 Chromium 阻止了一些 JavaScript 文件,这就是我得到白屏的原因。我应该如何禁用它,或者是否有任何解决方案?

我的 WebView 代码:

    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewWebClient());
    webView.getSettings().setSaveFormData(false);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setUserAgentString(System.getProperty("http.agent"));
    webView.loadUrl("https://www.tiktok.com/");
英文:

I'm trying to load TikTok website in WebView. But I can't. In logcat I got this error:

"[Report Only] Refused to load the script 'https://sf16-scmcdn-va.ibytedtos.com/goofy/slardar/fe/sdk/plugins/monitors.3.5.4.maliva.js' because it violates the following Content Security Policy directive: "script-src 'self' blob: data: 'unsafe-inline' 'unsafe-eval' polyfill.io *.kakao.com *.cdn-apple.com *.tiktokcdn.com *.byteoversea.com *.tiktok.com".
", source: https://www.tiktok.com/ (0)

Seems Chromium blocking some javascript files that's why I got white screen. How can I disable or any solutions for it?

My Webview:

    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewWebClient());
    webView.getSettings().setSaveFormData(false);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setUserAgentString(System.getProperty("http.agent"));
    webView.loadUrl("https://www.tiktok.com/");

答案1

得分: 1

webView.getSettings().setDomStorageEnabled(true);

已修复。感谢所有人。

英文:
webView.getSettings().setDomStorageEnabled(true);

Fixed it. Thanks all.

huangapple
  • 本文由 发表于 2020年9月23日 21:56:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64029585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定