Qt WebView-拦截JS/CSS库的加载以加载本地库 [英] Qt WebView - intercept loading of JS/CSS Libraries to load local ones

查看:524
本文介绍了Qt WebView-拦截JS/CSS库的加载以加载本地库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找文档,以寻找一种方法来完成此任务,但尚未成功.基本思想是,我有一部分通过Qt的webview加载的html.相同的内容可以导出到单个html文件中.

I've been looking for a while through documentation to find a way to accomplish this and haven't been successful yet. The basic idea is, that I have a piece of html that I load through Qt's webview. The same content can be exported to a single html file.

此文件使用诸如Bootstrap和jQuery之类的库.目前,我是通过CDN加载它们的,而CDN可以在联机时正常工作.但是,我的应用程序也需要脱机运行.因此,我正在寻找一种方法来拦截Qt中库的加载并提供本地保存的文件.我曾尝试安装https QWebEngineUrlSchemeHandler,但似乎从未触发过它的requestStarted方法.

This file uses Libraries such as Bootstrap and jQuery. Currently I load them through CDN which works when online just fine. However, my application also needs to run offline. So I'm looking for a way to intercept loading of the Libraries in Qt and serve a locally saved file instead. I've tried installing a https QWebEngineUrlSchemeHandler, but that never seems to trigger the requestStarted method on it.

(PyQT example follows)
QWebEngineProfile.defaultProfile().installUrlSchemeHandler(b'https', self)

如果我对方案使用其他文本并将其嵌入到页面中,则它可以工作,因此我认为它不起作用,因为Qt已经为其注册了默认处理程序.但是这种不同的方案将导致文件导出失败.

If I use a different text for the scheme and embed that into the page it works, so my assumption is that it doesn't work as Qt has a default handler for it already registered. But that different scheme would fail in the file export.

无论如何,回到核心问题;有没有办法拦截库的加载,或者仅在Qt内专门更改url方案?

Anyway, back to the core question; Is there a way to intercept loading of libraries, or to change the url scheme specifically within Qt only?

进一步使用QWebEngineUrlRequestInterceptor,现在将https请求重定向到我自己的具有uri处理程序的uri.但是,该请求永远不会到达该请求,因为:重定向位置'conapp://webresource/bootstrap.min.css'不允许跨域请求. 如何将自己的conuri方案列入白名单?

Got Further with QWebEngineUrlRequestInterceptor, now redirecting https requests to my own uri, which has a uri handler. However, the request never gets through to it, because: Redirect location 'conapp://webresource/bootstrap.min.css' has a disallowed scheme for cross-origin requests. How do I whitelist my own conapp uri scheme?

出于完整性考虑,当我最初提出该问题时,事实证明,由于其中的错误,使用PySide 5.11无法完成.我当时报告的错误现已标记为固定(我认为是5.12.1),因此现在应该可以使用Qt方法再次完成此操作,但是对于我自己的项目,我现在将坚持使用jinja,这已成为解决方案还有很多其他问题.

For completeness sake, it turns out back when I originally stated the question, it was impossible to accomplish with PySide 5.11 due to bugs in it. The bug I reported back then is nowadays flagged as fixed (5.12.1 I believe) so it should now be possible to accomplish this again using Qt methods, however for my own project I'll stick to jinja for now which has become a solution for many other problems.

推荐答案

因此,我最后使用的解决方案是首先介绍jinja模板.然后,使用这些模板,将根据导出或内部使用情况设置变量和块,并且从那里不再需要拦截器.

So, the solution I went with in the end was, first, introduce jinja templates. Then, using those the template would have variables and blocks set based on export or internal use and from there I did not need the interceptor anymore.

这篇关于Qt WebView-拦截JS/CSS库的加载以加载本地库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆