Android Webview,正在资源文件夹中加载javascript文件

|| 我已经看到这个问题了很多次,但是仍然无法使我的代码正常工作。 我希望我的Web视图加载一些URL(例如www.google.com),然后应用存储在
assets/jstest.js
中的一些javascript,其中包含以下内容:
function test(){
document.bgColor=\"#00FF00\"; //turns to green the background color
}
这是我尝试加载JS的位置:
@Override  
public void onPageFinished(WebView view, String url){
    view.loadUrl(\"javascript:(function() { \"
                + \" document.bgColor=\'#FF0000\';\" //turns to red the background color
                + \" var script=document.createElement(\'script\'); \"
                + \" script.setAttribute(\'type\',\'text/javascript\'); \"
                + \" script.setAttribute(\'src\', \'file:///android_asset/jstest.js\'); \"
                + \" script.onload = function(){ \"
                + \"     test(); \"
                + \" }; \"
                + \" document.getElementsByTagName(\'head\')[0].appendChild(script); \"
                + \"})()\"); 
} 
我知道这里的javascript可以正常工作,因为背景颜色实际上变成了红色,但是由于某种原因,它不会加载
jstest.js
。我认为问题可能出在文件路径中(我确定javascript代码的其他所有行都是正确的),但对我来说似乎正确。文件位于正确的文件夹中。 我想念什么? 编辑: 由于
WebResourceResponse
类仅在API级别11中可用,这就是我最后得出的结论。
public void onPageFinished(WebView view, String url){
        String jscontent = \"\";
        try{
            InputStream is = am.open(\"jstest.js\"); //am = Activity.getAssets()
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            String line;
            while (( line = br.readLine()) != null) {
                jscontent += line;
            }
            is.close(); 
        }
        catch(Exception e){}
        view.loadUrl(\"javascript:(\" + jscontent + \")()\"); 
    } 
jstest.js
只包含:
function() {
    document.bgColor=\"#00FF00\";
}
    
已邀请:
我尝试了同样的事情,将一个书签(您的loadUrl()调用中的javascript代码)加载到第三方页面中。我的小书签还依赖于其他资源(JavaScript和CSS文件),这些资源不会通过file:/// android_asset URL加载。 那是因为页面的安全上下文仍然是例如http://www.google.com的安全上下文,并且不允许访问文件URL。如果提供/覆盖WebChromeClient.onConsoleMessage(),则应该能够看到错误。 我最终陷入困境,在这里我将bookmarklet的资产引用更改为伪造的URL方案,例如:
asset:foo/bar/baz.js
并添加了一个WebViewClient.shouldInterceptRequest()替代,以查找这些替代项并使用AssetManager.open()从资产中加载它们。 我不喜欢这种麻烦的一件事是,asset:方案对我的视图加载的任何页面上的任何第三方HTML / Java脚本都是开放的,从而使他们可以访问我的应用程序的资产。 我没有尝试过的一种替代方法是使用数据:URL将子资产嵌入到书签中,但是这样做可能很麻烦。 如果有一种方法可以操作我正在loadUrl()中加载的JS小书签的安全上下文,我会更喜欢它,但是我找不到类似的东西。 这是一个片段:
import android.webkit.WebResourceResponse;
...
    private final class FooViewClient extends WebViewClient
    {
    private final String bookmarklet;
    private final String scheme;

    private FooViewClient(String bookmarklet, String scheme)
        {
        this.bookmarklet = bookmarklet;
        this.scheme = scheme;
        }

    @Override
    public void onPageFinished(WebView view, String url)
        {
        view.loadUrl(bookmarklet);
        }

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url)
        {
        if (url.startsWith(scheme))
            try
                {
                return new WebResourceResponse(url.endsWith(\"js\") ? \"text/javascript\" : \"text/css\", \"utf-8\",
                        Foo.this.getAssets().open(url.substring(scheme.length())));
                }
            catch (IOException e)
                {
                Log.e(getClass().getSimpleName(), e.getMessage(), e);
                }

        return null;
        }
    }
    
我认为Cordova的Iceam Cream Webview客户可以完成您想做的事情。 如果将此文件记录在某处会很好,但据我所知,事实并非如此。 看看cordova的android github: https://github.com/apache/incubator-cordova-android/blob/master/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java     
这就是我最终做到的方式。我使用了Content://协议并设置了contentprovider来处理将文件描述符返回到系统 这是我的fileContentProvider:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;


import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;

public class FileContentProvider extends ContentProvider {
    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) {

        Log.d(\"FileContentProvider\",\"fetching: \" + uri);

        ParcelFileDescriptor parcel = null;

        String fileNameRequested = uri.getLastPathSegment();
        String[] name=fileNameRequested.split(\"\\\\.\");
        String prefix=name[0];
        String suffix=name[1];
       // String path = getContext().getFilesDir().getAbsolutePath() + \"/\" + uri.getPath();
        //String path=file:///android_asset/\"+Consts.FILE_JAVASCRIPT+\"

/*check if this is a javascript file*/

        if(suffix.equalsIgnoreCase(\"js\")){
        InputStream is = null;
        try {
            is = getContext().getAssets().open(\"www/\"+Consts.FILE_JAVASCRIPT);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        File file = stream2file(is,prefix,suffix);
        try {
            parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        } catch (FileNotFoundException e) {
            Log.e(\"FileContentProvider\", \"uri \" + uri.toString(), e);
        }
        }
        return parcel;
    }

    /*converts an inputstream to a temp file*/

    public File stream2file (InputStream in,String prefix,String suffix) {
        File tempFile = null;
        try {
            tempFile = File.createTempFile(prefix, suffix);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tempFile.deleteOnExit();

            FileOutputStream out = null;
            try {
                out = new FileOutputStream(tempFile);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

            try {
                IOUtils.copy(in, out);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        return tempFile;
    }


    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public int delete(Uri uri, String s, String[] as) {
        throw new UnsupportedOperationException(\"Not supported by this provider\");
    }

    @Override
    public String getType(Uri uri) {
        throw new UnsupportedOperationException(\"Not supported by this provider\");
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentvalues) {
        throw new UnsupportedOperationException(\"Not supported by this provider\");
    }

    @Override
    public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
        throw new UnsupportedOperationException(\"Not supported by this provider\");
    }

    @Override
    public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
        throw new UnsupportedOperationException(\"Not supported by this provider\");
    }
}
在清单中,我定义了提供者:
<provider android:name=\"com.example.mypackage.FileContentProvider\"
          android:authorities=\"com.example.fileprovider\"
        />
这是注入Web视图的JavaScript:
webView.loadUrl(\"javascript:(function() { \"

           + \"var script=document.createElement(\'script\'); \"
           + \" script.setAttribute(\'type\',\'text/javascript\'); \"
           + \" script.setAttribute(\'src\', \'content://com.example.fileprovider/myjavascriptfile.js\'); \"
      /*      + \" script.onload = function(){ \"
           + \"     test(); \"
           + \" }; \"
      */     + \"document.body.appendChild(script); \"
           + \"})();\");
这是myjavascriptfile.js(作为示例):
   function changeBackground(color) {
       document.body.style.backgroundColor = color;
   }
    
也许您可以使用\'html / javascript模板\'作为资产。您可以结合使用这些文本源和字符串逻辑中的不同内容,以组成所需的html,以将其加载到WebViewer中。然后,您使用.loadData而不是.loadUrl 我正在单独使用它,它似乎运行得很好。 希望能帮助到你!     
给出以下两个条件: minSdkVersion 21 targetSdkVersion 28 我可以通过以下Java代码成功加载任何本地资产(js,png,css)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    Uri uri = request.getUrl();
    if (uri.getHost().equals(\"assets\")) {
        try {
            return new WebResourceResponse(
                URLConnection.guessContentTypeFromName(uri.getPath()),
                \"utf-8\",
                MainActivity.this.getAssets().open(uri.toString().substring(15)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
在HTML代码中,我可以使用
<link rel=\"stylesheet\" href=\"https://assets/material.min.css\">
<script src=\"https://assets/material.min.js\"></script>
<script src=\"https://assets/moment-with-locales.min.js\"></script> 
<img src=\"https://assets/stackoverflow.png\">
在Java中,以下内容也可以使用(您还需要在资产中添加
favicon.ico
webView.loadUrl(\"https://assets/example.html\");
使用“ 18”作为方案,使我可以从通过HTTPS服务的页面加载本地资产,而不会由于混合内容而导致安全问题。 这些都不需要设置:
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
webSettings.setDomStorageEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);    
    

要回复问题请先登录注册