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?
I find myself constantly worrying about retain cy...
Preface This article mainly introduces how to use...
Users are at hand, but it is not easy to monetize...
In the fiercely competitive beauty industry, majo...
During the just-concluded Spring Festival holiday...
Cowan's Mantella, scientific name Mantella co...
According to statistics and analysis by the China...
How much does it cost to be an agent of Jinchang ...
Recently, the Market Supervision and Administrati...
introduce This article mainly introduces some met...
Recently, another piece of news related to food a...
Mercedes-Benz reportedly expects to have a dedica...
At present, the automobile industry is facing unp...
Have you ever had the experience of being woken u...
The African Mercenary's Happy Birthday video ...