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?
How much does it cost to join the Zhuzhou Mechani...
The "Notice on the Management of Mobile Game...
【51CTO.com Quick Translation】I believe many of yo...
Argentine writer Borges once said: "If there...
Preface With the richness of hybrid applications,...
Recently, Ford Europe CEO Jim Farley will meet wi...
Content from : Gu Zhuowei Shanghai Renji Hospital...
The emergence of diseases is sometimes closely re...
Haval H8 sold 128 units per month, WEY P8 sold 50...
Recently, the epidemic situation in my country is...
[[225212]] Editor's note: Does Google's r...
When eating apples, would you choose to eat the w...
Do you hope that the event will become a hit? Tha...
The China Passenger Car Association recently rele...