![](/img/trans.png)
[英]ERROR : "Not allowed to load local resource: chrome://extensions" while using iframe
[英]QtWebEngine: “Not allowed to load local resource” for iframe, how to disable web security?
我正在將我的應用程序從WebKit移植到WebEngine(似乎有一個更好的渲染angular-basad html)。 我面臨的問題是我無法啟用QtWebEngine來加載本地iframe,盡管事實上我設置了所有可能的設置:
來自mainwindow.cpp的代碼
view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
view->page()->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
view->page()->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, true);
view->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, true);
view->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
最簡單的例子是采用基於WebEngine的FancyBrowser(\\ Examples \\ Qt-5.4 \\ webenginewidgets \\ fancybrowser)並嘗試加載本地html文件,如下所示:
index.html的:
<html>
<head>
<title>Hi there</title>
</head>
<body>
This is a page
a simple page
<iframe id="some_idrame" width="0" height="0" style="border: none" src="some_iframe.html" name="target" sandbox="allow-scripts"></iframe>
</body>
</html>
some_iframe.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>La-la-la</title>
</head>
<body>
Lalala
</body>
</html>
如果你將env var QTWEBENGINE_REMOTE_DEBUGGING設置為某個端口,那么你可以打開127.0.0.1:port並在控制台中看到這個錯誤:
"Not allowed to load local resource".
我真的不知道現在如何解決這個問題......應該有一些方法可以傳遞給WebEngine,比如“--disable-web-security”......
謝謝你的幫助!
這個Qt論壇鏈接可以幫到你。 你應該將參數傳遞給應用程序“--disable-web-security” https://forum.qt.io/topic/60691/not-allowed-to-load-local-resource-for-iframe-how-to-disable -web安全/ 4
如果需要通過WebEngine加載本地資源,則需要將--disable-web-security
參數傳遞給QApplication,例如:
char ARG_DISABLE_WEB_SECURITY[] = "--disable-web-security";
int newArgc = argc+1+1;
char** newArgv = new char*[newArgc];
for(int i=0; i<argc; i++) {
newArgv[i] = argv[i];
}
newArgv[argc] = ARG_DISABLE_WEB_SECURITY;
newArgv[argc+1] = nullptr;
QApplication myApplication(newArgc, newArgv);
另一種選擇是從文件系統加載原始頁面。 我在從Qt的資源系統加載圖像時遇到問題,所以我將QWebEngineView子類化並創建了這個函數:
void WebEngineView::setLocalHtml(const QString &html)
{
if(html.isEmpty())
{
setHtml(QString());
return;
}
// Save html to a local file
QString filePath;
{
QTemporaryFile tempFile(QDir::toNativeSeparators(QDir::tempPath() + "/ehr_temp.XXXXXX.html"));
tempFile.setAutoRemove(false);
tempFile.open();
QTextStream out(&tempFile);
out << html;
filePath = tempFile.fileName();
}
// delete the file after it has been loaded
QMetaObject::Connection * const conn = new QMetaObject::Connection;
*conn = connect(this, &WebEngineView::loadFinished, [filePath, conn](){
disconnect(*conn);
delete conn;
QFile::remove(filePath);
});
load(QUrl::fromLocalFile(filePath));
}
由於主頁面也是本地文件,因此解決了CORS安全問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.