Android Studio中的WebView使用JavaScript进行链接处理。

huangapple go评论80阅读模式
英文:

android studio webview to use javascript for the link

问题

这个问题是如何通过链接将 JavaScript 应用到 WebView https://dl.dropboxusercontent.com/s/lmibwymtkebspij/background.js,在页面完全加载后,背景应该变成绿色。以下是加载页面的示例代码:

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

提前感谢您。

英文:

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 = findViewById(R.id.Web);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyWebChromeClient());

thank you in advance

答案1

得分: 0

也许会有人用得上,将脚本的文本通过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();
            }
        }
    }
}

然后,当页面完全加载完成时,我们将该脚本应用于我们的 WebView,就像这样:

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

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:

     @SuppressLint(&quot;StaticFieldLeak&quot;)
class ProgressTask extends AsyncTask&lt;String, Void, String&gt; {
@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(&quot;debug&quot;, scriptbg);
}
public String getContent(String path) throws IOException {
BufferedReader reader = null;
try {
URL url = new URL(path);
HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
c.setRequestMethod(&quot;GET&quot;);
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 + &quot;\n&quot;);
}
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:

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

huangapple
  • 本文由 发表于 2020年9月25日 23:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64067142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定