How to clear the (CSS) visited history of an Andro

2019-03-15 01:50发布

I try to reuse an existing WebView by clearing any private data the previous user left behind:

CookieManager.getInstance().removeAllCookie();
webview.clearHistory();
webview.clearFormData();
webview.clearCache(true);

clearHistory seems only to clear the back/forward list, accessible via API, but not the internal list used for coloring links inside the web content.

I even tried the following, suggested by another stackoverflow answer:

deleteDatabase("webview.db");
deleteDatabase("webviewCache.db");

I still have no luck: CSS :visited selectors still work after reloading the page.

An alternative would be to use the API level 11 private browsing feature (new constructor argument), but then I cannot benefit from visited links at all; and can no longer target older versions.

Maybe someone has a solution for this issue? Thanks for your help.

Summary of the answers I got so far:

I tried these two answers, but the first seems to clear HTML5 data storage and the latter seems to be specific to the built-in browser:

WebStorage.getInstance().deleteAllData();
Browser.clearHistory(getContentResolver());

WebChromeClient.getVisitedHistory(ValueCallback<String[]> callback) is only called after the first time I create a new WebView in a recently installed application.

I tried to remove the WebView from view hierachy and create a new one, but unfortunately the visited history seems to be stored for the whole application.

4条回答
劳资没心,怎么记你
2楼-- · 2019-03-15 02:14

Override WebChromeClient and WebViewClient... Damn that was hidden.

I actually had to dig up a bit to find this out.

WebView webView = (WebView)findViewById(R.id.myWebView);
WebChromeClient myWebChromeClient = new WebChromeClient(){
        @Override
        public void getVisitedHistory(ValueCallback<String[]> callback) {
// called during webview initialization, original implementation does strictly nothing 
// and defaults to the native method WebViewCore.nativeProvideVisitedHistory()
            String[] myUserHistory = getVisitedUrlsFromMyOwnDatabase(userId);
            callback.onReceiveValue(myUserHistory);
        }
    };
WebViewClient myWebViewClient = new WebViewClient(){
    @Override
public void doUpdateVisitedHistory(WebView view, String url,
        boolean isReload) {
// called whenever there is a new link being visited
        insertIfNotExistVisitedUrlIntoMyOwnDatabaseForUser(userId);
        super(view, url, isReload);
}
}
webView.setWebViewClient(myWebViewClient);
webView.setChromeClient(myWebChromeClient);
webView.getSettings().etc(whatever)...

I think I'm "almost there". Here's the part I managed: what it does so far is remove css history altogether, so we're halfway there. I can't get the browser to recognize the url format I'm providing in "myUserHistory", so in effect the only feature this code does is reset css history altogether, but it's only called once when the WebView is instanciated (or created, didn't check), so for a true multiuser experience you'd need to recreate the webview at each login.

My problem now is that I can't manage to load the urlHistory properly. My Honeycomb Xoom webview seems to ignore my data. Ah well, I hope it works for you. For me just calling callback.onReceiveValue(new String[]{}); in getVisitedHistory() will be good enough.

EDIT: I just put twenty more minutes into it because I'm curious. This method is what delegates to the WebChromeClient (mCallbackProxy = WebChromeClient).

protected void populateVisitedLinks() {
     ValueCallback callback = new ValueCallback<String[]>() {
         public void onReceiveValue(String[] value) {
             sendMessage(EventHub.POPULATE_VISITED_LINKS, (Object)value);
         }
     };
     mCallbackProxy.getVisitedHistory(callback);
 }

It's protected in WebViewCore, which is a private attribute of WebView with no accessor. The sendMessage delegates to EventHub which is private, and WebViewCore is filled with private native methods, and one of these seems to be the one actually calling the populateVisitedLinks() method during the initialization.

Unless someone at Google adds a public method to WebView to trigger the repopulation, I'm afraid it's practically impossible to achieve your goal. Sorry :(

As a side note, all these native visited history handling really makes me wonder: why do hardware manufacturers care so much about which urls we visited? ;) <<< sarcasm

查看更多
做个烂人
3楼-- · 2019-03-15 02:15

Does WebStorage.clearAllData() have the desired effect? Unfortunately, the documentation on this class is very sparse compared to WebView and doesn't say whether it applies to WebViews.

The exact time you're calling clearHistory() may also have an effect. Clearing it and then navigating to a new page may still keep the first page in history, and you have to call the method after the new page has loaded.

Personally, if privacy is a real issue, I would create a new set of objects from scratch for this new session if possible.

查看更多
霸刀☆藐视天下
4楼-- · 2019-03-15 02:25

As an alternate solution, you could try adding your own CSS with the same base colors the default CSS has and switch the CSS by another one (with same color for both "types" of links) when you want to reset the visited links.

A:link{color: "#990000"; text-decoration: none;}
A:visited{color: "#990000"; text-decoration: none;}
A:hover{color: "#ff0000"; text-decoration: none;}
查看更多
混吃等死
5楼-- · 2019-03-15 02:35

If you can obtain a Browser instance (maybe you can set a WebChromeClient to WebView) you can use its clearHistory() method.

查看更多
登录 后发表回答