QtWebEngine:“;不允许加载本地资源“;对于iframe,如何禁用web安全
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 QTWEBONGINE_REMOTE_DEBUGGING设置为某个端口,那么您可以打开127.0.0.1:port并在控制台中看到此错误:
"Not allowed to load local resource".
我现在真的不知道该怎么解决这个问题。。。应该有某种方式将类似"--禁用网络安全"的内容传递给WebEngine。。。
谢谢你的帮助!
此Qt论坛链接可帮助您。您应该将参数传递给应用程序"--disable web security"https://forum.qt.io/topic/60691/not-allowed-to-load-local-resource-for-iframe-how-to-disable-web-security/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安全问题。
- 防止Iframe窗体在新窗口中打开
- Javascript:selenium Web驱动程序isDisplayed()不工作
- 将样式表插入iframe
- web部件页SharePoint 2013中的Iframe
- 如何使用javascript(或jQuery)将youtube视频添加到iframe(基于web的RTE)
- 如何在没有iFrame的情况下加载Monocle Web电子书/ePub阅读器
- iFrame - 在 Web 应用程序中实现 Iframe 的成本
- 在通过 flask Web 服务器呈现的 html 页面中显示 iframe 中文本文件的内容
- CRM 2011:使用 javascript 将值传递给 IFRAME/Web 资源
- Web 浏览器何时请求动态创建的 iframe 的 src 内容
- 在股票浏览器中打开链接,而不是从 iframe 打开 Web 视图
- QtWebEngine:“;不允许加载本地资源“;对于iframe,如何禁用web安全
- 使用javascript(dojo或jquery)在iframe中实现Web自动化-点击iframe内的元素
- 如何从web浏览器查看iframe中javascript生成的源代码并将其复制到剪贴板
- 如何防止用户在iframe外打开我的web应用程序?
- Web内容没有在iframe中加载
- 如何从谷歌新的Iframe删除谷歌Web元素
- 使用带量角器的phantomjs访问iframe内的web元素
- 单页应用程序不会显示在asp.net web应用程序的iframe中
- Chrome扩展:识别web请求的背景页面iframe