Android webview inserting JavaScript

Source: Internet
Author: User

There is such a demand, webview from the Internet to load some pages, is someone else's page, we only need a part of the data in the page, but it is not possible to call someone to modify the page, so only to do it yourself, one way is to take the page when the string, analysis and change to add webview display, But this method seems a bit troublesome.

Talk about another way of thinking, Web page data is fixed, directly put the URL of the page into the webview load, but did not see the WebView method can be intercepted, but you can insert some JavaScript in the Web page to block the content does not need to display.

Example: Baidu Web page conversion http://gate.baidu.com/taken to the page, living in front of Baidu and behind each inserted a div, very annoying so, so to put it and compared to, the front of the div called class= "Trans", the Back is class= " Footer ". What we need is to insert JavaScript into the Web page and set the two div to be invisible. The code is as follows:


[Java]View Plaincopy
  1. Public class Test {
  2. private static final String Baidu_url = "http://gate.baidu.com/tc?from=opentc&src=";
  3. private String Willloadurl = "http://www.google.com.hk";
  4. private WebView WebView;
  5. //This is the JavaScript to inject, note: The preceding "javascript:" is required, followed by the statement to be injected
  6. private static final String Insertjavascript = "Javascript:window.onload=function () { Document.getelementsbyclassname (' footer ') [0].style.display= ' None '; Document.getelementsbyclassname (' trans ') [0].style.display= ' None ';} ";
  7. @Override
  8. public void OnCreate (Bundle savedinstancestate) {
  9. super.oncreate (savedinstancestate);
  10. Setcontentview (R.layout.testlistview);
  11. WebView = (WebView) Findviewbyid (R.id.webview);
  12. //enable JavaScript first
  13. Webview.getsettings (). setjavascriptenabled (true);
  14. //webview need to inject JavaScript to block Baidu's head and tail. Set Webviewclient
  15. Webview.setwebviewclient (new Mywebviewclient ());
  16. //This is the URL of the load page, the real page
  17. LoadWebPage (Baidu_url);
  18. }
  19. private void loadwebpage (String url) {
  20. Webview.loadurl (URL);
  21. }
  22. /** 
  23. * After testing, injecting scripts into the onpagefinished sometimes does not invoke JavaScript. It can be injected in onpagestarted.
  24. */
  25. public class Mywebviewclient extends Webviewclient {
  26. @Override
  27. public void onpagefinished (WebView view, String URL) {
  28. super.onpagefinished (view, URL);
  29. LOG.D ("hehe", "onpagefinished execute");
  30. }
  31. @Override
  32. public void onpagestarted (WebView view, String URL, Bitmap favicon) {
  33. super.onpagestarted (view, URL, favicon);
  34. //Injection
  35. View.loadurl (Insertjavascript);
  36. LOG.D ("hehe", "onpagestarted execute");
  37. }
  38. }
  39. }


Many online are in the onpagefinished when injected, but sometimes failure, do not know why, so changed into a onpagestarted

Android webview inserting JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.