X
X
xaiponews2020-12-16 22:09:10
Android
xaiponews, 2020-12-16 22:09:10

How to use javascript:(function()) in a WebView or how to display a single element of a web page?

Here I want to discuss a well-known method for hiding elements of a web page viewed in a WebView. The working version is

webview.setWebViewClient(new WebViewClient(){

 @Override
        public void onLoadResource(WebView view, String url) {
            view.loadUrl("javascript:(function() {" +
                    "document.getElementsByClassName('nameclass')[0].style.display='none';" + "})()");
    }
});

Why onLoadResource? so that the hiding of elements occurs imperceptibly for the user. But that's not the point.
There are many ways on the internet to hide web page elements and they are all good if you need to hide one or two elements. If you need to hide a large number of elements, then you end up with a lot of code that may not work well. Hence the question - is there a way / code, so that from the entire html code of the loaded web page, specify only one element to be displayed, but at the same time so that it is fully functional, as if it were displayed with all the page code. For example, so that it looks like this:

Yandex before processing
5fda57ef90967563678188.png

Yandex after processing
5fda5814cc52d401846571.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2020-12-16
@rPman

No, because the display of elements is inherited in a tree-like system of elements, without the ability to easily change the order (especially if you need 'everything to work as it should' universally).
p.s. javascript in the browser is very fast and 99.999% of the brakes are dom manipulations i.e. the code for finding all the necessary elements on the page will spend a matter of milliseconds of time, and then almost a second hiding them, and this most likely cannot be accelerated in any way.
pps if you still need to speed up, first try modifications that do not change the dimensional structure of the document (so that the elements do not change their size and the position of others when hiding their content)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question