簡體   English   中英

如何訪問cordova WKWebview ios應用程序的iframe中的元素?

[英]How can i access elements in a iframe of a cordova WKWebview ios app?

我想訪問嵌入在我們 ios 應用程序 iframe 中的網站元素。 它與文件訪問具有相同的問題。 SecurityError:阻止了一個帶有“file://”源的框架訪問跨源框架......

通過這個插件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 調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM