Load local file in Chrome App Webview

旧街凉风 提交于 2019-11-30 15:18:33

问题


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 use iframe, because iframe wont support external resources (scripts, images, whatever).


回答1:


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.]




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/23374184/load-local-file-in-chrome-app-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!