Recently, I need to detect the source code of a web page. There is no direct interface to obtain the source code of the web page in Android Webview. The traditional addJavascriptInterface method has security risks, so I studied the security interaction between Java and Javascript. Android Webview Vulnerability Android Webview has two very well-known vulnerabilities:
Safe interaction between Java and Javascript First of all, a few points: 1. It is easy to call Js methods from Java in Android Webview. loadUrl("javascript:isOk()") can call the isOk Js method, but the return result of the Js method cannot be directly obtained.
2. In the traditional method, Js can obtain Java information in the following ways:
3. When there is a hyperlink jump in the web page, the shouldOverrideUrlLoading method of WebClient will be called. If WebViewClient is set and the method returns true, it means that the URL is processed by the application code and WebView does not, which can achieve the effect of intercepting the jump. Understanding the above points, we can summarize a relatively safe way for Java and Js to interact : We can learn from the idea of Android Intent. Java and Js define a URL format such as js://_. Java calls the Js method. In the Js method, window.location.href='js://_?key=value#key1=value1' is used to simulate the jump. It is captured by Java's shouldOverrideUrlLoading. The return value of the function can be placed in the URL parameter. (The principle of Js calling Java methods is the same) This interaction method is asynchronous. What if you want to know whether calling a Js method returns a value? Generally, Java calls the Js method in the onPageFinished method, and obtains the Js return value in the shouldOverrideUrlLoading method. The two methods have a common parameter webview, so you can first call webview.setTag(false). If the return result is captured, then call webview.setTag(true). After a short time, such as 300 milliseconds, call webview.getTag() to check whether there is any change. |
<<: Changsi Advertising won the "Golden Coordinate" 2014 Best Mobile Advertising Platform Award
>>: Which three “mobile industry chains” has WeChat created?
gossip "If the child doesn't want to tak...
In this article, we will continue to talk about h...
What is conversion Conversion includes two things...
The most enjoyable thing about summer is eating i...
Nowadays, various new brands have emerged. Young ...
[[265644]] According to the 21st Century Business...
[[183880]] First, if you encounter application la...
Many people understand that I often change my ide...
In 1100 AD, Su Shi was pardoned by the imperial c...
In nature, plants are always at a disadvantage, a...
Just now, according to the latest results from th...
The booming live streaming market and internet ce...
When people mention Sony, they will think of many...
For the smart TV industry, 2014 was undoubtedly a...
Muquan 4+3 Postpartum Body Shaping and Weight Los...