android studio webview 将 javascript 用于 link
android studio webview to use javascript for the link
这个问题是如何将 link 的 java 脚本应用到 webview https://dl.dropboxusercontent.com/s/lmibwymtkebspij/background.js 页面完全加载后背景应该变成绿色这里是加载页面的示例代码
webView = findViewById(R.id.Web);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyWebChromeClient());
提前致谢
也许有人会派上用场,使用对 link 脚本地址的 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");
}
这个问题是如何将 link 的 java 脚本应用到 webview https://dl.dropboxusercontent.com/s/lmibwymtkebspij/background.js 页面完全加载后背景应该变成绿色这里是加载页面的示例代码
webView = findViewById(R.id.Web);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyWebChromeClient());
提前致谢
也许有人会派上用场,使用对 link 脚本地址的 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");
}