-->

How do you display a filesystem URL in a Chrome ap

2020-06-22 05:18发布

问题:

I have a Chrome packaged app that requires me to be able to view saved, offlined web pages. These pages are downloaded from my server when the user is online and saved to the HTML5 filesystem so that they can be viewed offline. This Chrome bug ticket seems to indicate that what I want to do is possible: https://code.google.com/p/chromium/issues/detail?id=343382

Based on the Chrome app 'webview' docs (https://developer.chrome.com/apps/tags/webview), this is what I came up with.

In my manifest:

"permissions": [
    {
        "fileSystem": ["write"]
    },
    "storage",
    "unlimitedStorage",
    "webview"
],
"webview": {
    "partitions": [
        {
            "name":"contentView",
            "accessible_resources":["*.html"]
        }
    ]
}

My webview tag:

<webview id="websiteWebview" minwidth="800" minheight="600" autosize="on" partition="contentView"></webview>

And then I'm adding the 'src' attribute through javascript:

var websiteWebview = document.querySelector('#websiteWebview');
websiteWebview.setAttribute('src', decodeURIComponent(srcUrl));

This results in the following in the console:

filesystem:chrome-extension://[appId]/[pathToFile]/index.html
Failed to load resource: net::ERR_FILE_NOT_FOUND
Not allowed to load local resource: filesystem:chrome-extension://[appId]/[pathToFile]/index.html

I'm not sure where to go from here. Thanks in advance for your help!

回答1:

It looks like the webview tag does not support filesystem URIs - Chromium Issue 321628. Bummer.

You could try reading the file to a data URL using a FileReader, then setting the webview's src attribute to the URL.

I'm getting around it by using a simple web server I created in NaCl which I've already got embedded in my app (data URLs are messing up my file encoding).



回答2:

You could work around this by including the web server for chorme library within your application. There are examples on the site for how to include it in your own chrome app. It would require you to use extra permissions in your manifest, namely some chrome.sockets permissions.