Is there a method to load html without external resources?
-
I got html reply from website using qNetworkAccessManager. while I want to take advantage of the webkit to parse the html element but I do not want to take more time and bandwidth to load more things like css or js or imgs. I saw the method setHtml() or setContent from QWebView or QWebFrame both load external resources async. Is there a method to load html without external resources that can parse the HTML element? Thanks!
-
As you have seen it is possible to load HTML from a string inside a "QWebView":http://qt-project.org/doc/qt-5/qwebview.html using its method "setHtml":http://qt-project.org/doc/qt-5/qwebview.html#setHtml. If the HTML does not contain any external resources this method should load it immediately.
-
Thanks for suggestion. I know the setHtml would load my HTML sync, but it also load external resources like css or js.
One idea came out in my mind is to subclass the QNetworkAccessManager used by QWebView and overwrite the createRequest method to block all the request loaded automatically by QWebView, so that the QWebView can not access external resources. Is this the best way to do it or is there a lightweight way? -
Another option that you might consider is to strip all links to external resources from the HTML before loading it inside the QWebView.
-
[quote author="qqiq" date="1390596214"]That looks more complicated. I have deal with css, js, and embedded js. [/quote]
OK, it all depends on the exact case that you have so it is up to you :)
-
You can rewrite QNetworkAccessManager::createRequest function to skip some urls, see: "http://code.google.com/p/simple-media-player/source/browse/trunk/MyPlayer/src/adblock.py?spec=svn15&r=15":http://code.google.com/p/simple-media-player/source/browse/trunk/MyPlayer/src/adblock.py?spec=svn15&r=15