简体   繁体   English

android studio webview 使用 javascript 作为链接

[英]android studio webview to use javascript for the link

this question is how to apply java script by link to webview https://dl.dropboxusercontent.com/s/lmibwymtkebspij/background.js after the page is fully loaded the background should turn green here is a sample code for loading the page这个问题是如何通过链接到webview https://dl.dropboxusercontent.com/s/lmibwymtkebspij/background.js应用java脚本页面完全加载后背景应该变成绿色这里是加载页面的示例代码

 webView = findViewById(R.id.Web);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyWebChromeClient());

thank you in advance先感谢您

maybe someone will come in handy load the text of the script into the script variable using get request to link the address of the script like this:也许有人会派上用场,使用 get 请求将脚本的文本加载到脚本变量中以链接脚本的地址,如下所示:

     @SuppressLint("StaticFieldLeak")
class ProgressTask extends AsyncTask<String, Void, String> {
    @Override
    public String doInBackground(String... path) {
        try {
            content = getContent(path[0]);
        } catch (IOException ex) {
            content = ex.getMessage();
        }

        return content;

    }

    @Override
    public void onPostExecute(String content) {
        scriptbg = content;

        Log.d("debug", scriptbg);

    }

    public String getContent(String path) throws IOException {
        BufferedReader reader = null;
        try {
            URL url = new URL(path);
            HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setReadTimeout(10000);
            c.connect();
            reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
            StringBuilder buf = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                buf.append(line + "\n");
            }
            return (buf.toString());
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

then we apply the script to our webView when the page is fully loaded like this:然后我们在页面完全加载时将脚本应用到我们的 webView,如下所示:

public void onPageFinished(WebView view, String url) {
        webView.loadUrl("javascript:" + Script);
        Log.d("debug", "finish");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM