我注意到带有“下载”属性的链接不起作用。
<a href="/media/wysiwyg/pdf/systemaufbau-anleitungen.pdf" target="_blank" download="blizz-z_Systemaufbau_Anleitungen">
所以我试图注入一些JavaScript来将其删除,但是它不起作用:
// https://developer.android.com/reference/android/webkit/WebViewClient
myWebView.setWebViewClient(new WebViewClient() {
@Override
// Notify the host application that a page has finished loading.
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
// Hide/Show back button
backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());
if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}
blizzView.loadUrl(
"javascript:(function() {" +
"setInterval(function() {" +
"jQuery('a').each(function() {"
+ "jQuery(this).removeAttr('download');"
+ "jQuery('#myInput').css('background', '#'+(Math.random()*0xFFFFFF<<0).toString(16));"
+ "});"
+ "}, 1000);"
+ "});"
);
我正在每秒为我的站点的搜索栏提供一种随机颜色,以确认脚本已运行,但它不会更改颜色... 我在开发人员控制台的website上测试了脚本,该脚本可以运行,但不能从应用程序中运行。
答案 0 :(得分:0)
我必须写一个解决方法,因为这一定是Android webView中的错误。
我只是遍历每个a
标记并剥离了download
属性,现在它可以了。
代码:
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
website = "https://www.blizz-z.de";
myWebView = findViewById(R.id.blizzView);
WebSettings settings = blizzView.getSettings();
settings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
@Override
// Notify the host application that a page has finished loading.
public void onPageFinished(WebView view, String url)
{
js(myWebView, "jQuery(document).ready(function() {" +
"setInterval(function() {" +
+ "jQuery('#myInput').css('background', '#'+(Math.random()*0xFFFFFF<<0).toString(16));"
+ "}, 1000);"
+ "});"
);
}
}
...
}
public void js(WebView view, String code)
{
String javascriptCode = "javascript:" + code;
if (Build.VERSION.SDK_INT >= 19) {
view.evaluateJavascript(javascriptCode, new ValueCallback<String>() {
@Override
public void onReceiveValue(String response) {
Log.i("debug_log", response);
}
});
} else {
view.loadUrl(javascriptCode);
}
}
我创建了一个Issue。