如何访问cordova WKWebview ios应用程序iframe中的元素



我想访问我们网站的元素,这些元素嵌入到我们ios应用程序的iframe中。它和文件访问有同样的问题。SecurityError:阻止了一个具有原点的帧";文件://";从访问跨原点帧。。。

文件访问问题通过这个插件解决https://github.com/globules-io/cordova-plugin-ios-xhr.但是iframe中的访问并没有得到解决。

如何访问cordova WKWebview ios应用程序iframe中的元素?

我在使用WkWebView的Cordova应用程序中遇到了类似的问题。你需要让iframe在"同源";您的应用程序。这是我找到的唯一解决方法。它涉及到直接将内容写入iframe,而不是给它一个src="&";。通过这种方式,iframe和父级在同一原点上运行。(通过src="file://..."加载的任何内容在WkWebView中都被视为唯一来源,因此会阻止任何跨帧JavaScript。(

function frameEl_injectHtml(frameEl, injectHtml) {
// frameEl must exist in DOM before its contentWindow is accessible.
if (!frameEl.contentWindow) { alert("frameInjectHtml() but frameEl not (yet or anymore) in DOM."); return; }
frameEl.contentWindow.document.open('text/htmlreplace');
frameEl.contentWindow.document.write(injectHtml);
frameEl.contentWindow.document.close();
}
// Create <frame>, insert into DOM, and inject content.
var frameHtml = "<html><head></head>" +
"<body style='background-color:#eee; color:#000;'>" +
"iframe body" +
"<script>window.parent.alert('iframe even same-origin as parent.');</script>" +
"</body></html>";
var frameEl = document.createElement('iframe');
frameEl.src = "about:blank";
document.body.appendChild(frameEl);
frameEl_injectHtml(frameEl, frameHtml);

您将不得不以某种方式加载该变通方法之外的网站页面的内容,例如使用ajax调用。

相关内容

最新更新