gpt4 book ai didi

javascript - 如何在 WebView 中保持 CSS 持久化

转载 作者:太空宇宙 更新时间:2023-11-03 11:25:49 24 4
gpt4 key购买 nike

这是我要遵循的代码;我正在使用 onPageFinished(WebView view, String url) 将自己的 css 注入(inject)外部站点。不幸的是,对于 Webview,它首先呈现页面然后应用 CSS。我怎样才能等待页面呈现,然后在显示之前应用 javascript?

WebClient HTTP Get 是否有更简单的方法并将 CSS 重定向到本地?

提前感谢您的建议!

com.webkitdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebKitDemo extends Activity
{
final Activity activity = this;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);

//addition final webview

final WebView webView = (WebView)findViewById(R.id.web_engine);
/* JavaScript must be enabled if you want it to work, obviously */
webView.getSettings().setJavaScriptEnabled(true);




webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);

if(progress == 100)
activity.setTitle(R.string.app_name);
}
});

webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}

//additions

@Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript:(function() { " +
"document.getElementsByTagName('body')[0].style.color = 'green'; " +
"})()");
}
//

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
webView.loadUrl(url);
return true;
}



});
webView.loadUrl("http://code.google.com");
}}

最佳答案

在您的样式更改代码之前添加它。这样它只会在页面加载完成时执行。

var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = yourfunctionhere();
}
else
{
window.onload = function()
{
yourfunctionhere();
oldonload();
}
}

关于javascript - 如何在 WebView 中保持 CSS 持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com