我正在尝试在应用程序中显示带有iframe的页面。它显示简单的页面,但不能显示googlemap iframe。
我试过了:
hardvre加速-已打开(据我所知,它仅与视频有关),
Java脚本-已启用,
使用Web chrome客户端,
loadurl(由于从内部存储器加载)等
代码是:
wewview = new WebView(getActivity().getApplicationContext());
wewview.getSettings().setJavaScriptEnabled(true);
wewview.getSettings().setLoadWithOverviewMode(true);
wewview.getSettings().setUseWideViewPort(true);
wewview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
wewview.setWebChromeClient(new WebChromeClient());
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
wewview.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
wewview.getSettings().setPluginState(WebSettings.PluginState.ON);
wewview.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
linearMain.addView(wewview,0);
wewview.loadUrl("file://path... /frames.html");
有人可以建议我如何以编程方式为我的Webview打开“忽略X框架选项”吗?
更新1
在日志中找到另一条消息
05-19 10:01:49.404: I/chromium(4027): [INFO:CONSOLE(0)] "Refused to display 'https://www.google.ru/maps/@?nogmmr=1' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.", source: about:blank (0)
寻找忽略x-frame-options
最佳答案
找到了路。在加载的框架上没有完整的交互,但是我只需要显示它即可。因此,Jsoup用于加载页面内容,但始终以标准方式加载脚本和图像,这就是所有的窍门:
private class ignoreXHeaderWebClient extends WebViewClient{
private static final String URL_MASK = "http";
//api < 21
@Override
public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
WebResourceResponse content = getsPageContent(url);
if (content != null) {
return content;
} else {
return super.shouldInterceptRequest(view, url);
}
}
// api >= 21
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
WebResourceResponse content = getsPageContent(request.getUrl().toString());
if (content != null) {
return content;
} else {
return super.shouldInterceptRequest(view, request);
}
}
private WebResourceResponse getsPageContent(String url){
if (( url.contains(URL_MASK)) // guess is better to create for ex map to check all extensions
&& !(( url.contains(".js"))
||( url.contains(".css"))
||( url.contains(".tiff"))
||( url.contains(".jpg"))
||( url.contains(".png"))
||( url.contains(".gif")))){
try {
return new WebResourceResponse(
"text/html",
"UTF-8",
new ByteArrayInputStream(
Jsoup.connect(url)
.get()
.toString()
.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
并申请新 class :
webWiev.setWebViewClient(ignoreXHeaderWebClient);