英文:
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("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:
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:" + Script);
Log.d("debug", "finish");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论