@flypig had given me a heads up for the more restrictive CORS rules as of 4.4. To keep it to the point, to load local resources in a WebView:
WebView {
/* This will probably be required from 4.4 on. */
Component.onCompleted: {
/* disable ALL
WebEngineSettings.setPreference("security.disable_cors_checks", true, WebEngineSettings.BoolPref)
OR */
WebEngineSettings.setPreference("security.fileuri.strict_origin_policy", false, WebEngineSettings.BoolPref)
}
id: webView
anchors.fill: parent
url: "../mah/index.html"
}
Er, ‘my’ nice python example was cribbed from @martyone 's python example (which happened to implement a thread model) and is, of course, mostly your code and props to @nephros !
I’ve always admired you clear and comprehensive deep dives in the newsletters and have benefited from them, a great deal. I’m just really happy if I’ve contributed to that! Thanks!
Nono, I have a backend which spits out a HTML string. That HTML contains regular <img src="foo.png" /> elements.
The image files lie in ~/.cache/foo/bar.
I use WebView::loadHtml to display the html string which works fine. Only the images are not displayed, no matter what I do.
Using MOZ_LOG magic I can see the images are found, it begins to load them, and finally
That’s not necessary CORS though. IIUC you are loading local resources from a disk / memory, thus the engine enables protection against attacks that try to load resources from Internet in localhost/file contexts.
I get a string, from a QML plugin. That string is HTML. This I pass to Sailfish.WebView, calling its loadHtml() method.
WebView renders the page (sans images).
I didn’t write the QML plugin, merely using its output.
Basically
CustomThing {
onContentChanged: web.loadHtml(content)
}
WebView { id: web
}
As a workaround I now (i.e. the current git code) fall back to loading the images from an online source.
Makes them work but the app is kinda supposed to work offline mostly.
Not sure whether the CSP meta line is required for this, but AFAICS it’s not wrong.
I still would prefer accessing the local files instead, and as I said no amount of trickery seems to enable this.
Maybe I’ll hack the backend to read the image files and insert data: URIs instead of file names, but this seems unnecessarily complicated and superfluous.
It ‘could’ be that the images are not being found. You could load an html shim to make sure that images relative to the ‘document root’ are found. I have one example that looks like this:
ham/index.html all assets relative paths and javascript loader:
href="assets/app/
and same directory
<style>@font-face{font-family:Kulim Park;font-style:normal;font-weight:400;src:url(kulim-park-v4-latin-ext_latin-regular.328c0ac02dbbd95a.eot);