Load local file in Chrome App Webview

前端 未结 4 1492
死守一世寂寞
死守一世寂寞 2021-01-02 17:23

In Chrome packaged apps you can use to load external pages inside the app. Is there a way to make them load a local file (an html file inside the packaged app)? I can\'t us

相关标签:
4条回答
  • 2021-01-02 18:10

    You may try loading the file as a Blob via an XMLHttpRequest, then creating an object url to set it as webview's src attribute:

    window.onload = function() {
        var wv = document.getElementById("wv");
    
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'local_file.html', true);
        xhr.responseType = 'blob';
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var blob = new Blob([this.response], {type: 'text/html'});
                wv.src = window.URL.createObjectURL(blob);
            }
        };
    
        xhr.send();
    };
    

    Here's a working example: chrome_app_webview_test.

    0 讨论(0)
  • Don't have any code to show, but try this: Assuming you can read the local file (must use chrome.fileSystem.chooseEntry or have a retained entry on the file or its containing directory) and get a FileEntry object, you can then create a FileReader to get the file as a data URL. Then you can use that data URL directly in a webview. (Must have webview permission, in addition to the permissions needed to access the FileEntry.)

    [Above is from memory while I'm eating breakfast. Might have some API names slightly off, but I hope you get the general idea.]

    0 讨论(0)
  • 2021-01-02 18:20

    The local-resources sample has an example of loading an html file in a webview. You need to specify the files in the webview.partitions section of manifest.json.

    0 讨论(0)
  • 2021-01-02 18:25

    Not exactly sure what you are looking for, but I have successfully used a <webview> tag pointing to a local .html file (including image and video resources) within the directory structure of an UNpackaged Chrome App. As its URL I simply use window.location.origin +'/files/my.html'. I leave my App unpackaged so I can dynamically generate the .html files. I guess you can pack the app if the content is static, but I have not tried.

    0 讨论(0)
提交回复
热议问题