[Android]通過js方法回調部分native報錯 Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'

來源:互聯網
上載者:User

標籤:

在android4.2以前,注入步驟如下:

 

webview.getSetting().setJavaScriptEnable(true);class JsObject {    public String toString() { return "injectedObject"; } } webView.addJavascriptInterface(new JsObject(), "injectedObject");

Android4.2及以後,注入步驟如下:

webview.getSetting().setJavaScriptEnable(true);class JsObject {    @JavascriptInterface    public String toString() { return "injectedObject"; } } webView.addJavascriptInterface(new JsObject(), "injectedObject");

發現區別沒?4.2之前向webview注入的對象所暴露的介面toString沒有備註陳述式@JavascriptInterface,而4.2及以後的則多了備註陳述式@JavascriptInterface

 

經過查官方文檔所知,因為這個介面允許JavaScript 控制宿主應用程式,這是個很強大的特性,但同時,在4.2的版本前存在重大安全隱患,因為JavaScript 可以使用反射訪問注入webview的java對象的public fields,在一個包含不信任內容的WebView中使用這個方法,會允許攻擊者去篡改宿主應用程式,使用宿主應用程式的許可權執行java代碼。因此4.2以後,任何為JS暴露的介面,都需要加

@JavascriptInterface

注釋,這樣,這個Java對象的fields 將不允許被JS訪問。

 

官方文檔說明:

 

 

From the Android 4.2 documentation:

Caution: If you‘ve set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

註:如果將targetSdkVersion 設定為17或者更高,但卻沒有給暴露的js介面加@JavascriptInterface注釋,則logcat會報如下輸出:

E/Web Console: Uncaught TypeError: Object [object Object] has no method ‘toString‘

 

public void addJavascriptInterface (Object object, String name)Added in API level 1

Injects the supplied Java object into this WebView. The object is injected into the JavaScript context of the main frame, using the supplied name. This allows the Java object‘s methods to be accessed from JavaScript. For applications targeted to API level JELLY_BEAN_MR1 and above, only public methods that are annotated with JavascriptInterface can be accessed from JavaScript. For applications targeted to API level JELLY_BEAN or below, all public methods (including the inherited ones) can be accessed, see the important security note below for implications.

Note that injected objects will not appear in JavaScript until the page is next (re)loaded. For example:

 class JsObject {    @JavascriptInterface    public String toString() { return "injectedObject"; } } webView.addJavascriptInterface(new JsObject(), "injectedObject"); webView.loadData("", "text/html", null); webView.loadUrl("javascript:alert(injectedObject.toString())");

IMPORTANT:

  • This method can be used to allow JavaScript to control the host application. This is a powerful feature, but also presents a security risk for applications targeted to API level JELLY_BEAN or below, because JavaScript could use reflection to access an injected object‘s public fields. Use of this method in a WebView containing untrusted content could allow an attacker to manipulate the host application in unintended ways, executing Java code with the permissions of the host application. Use extreme care when using this method in a WebView which could contain untrusted content.
  • JavaScript interacts with Java object on a private, background thread of this WebView. Care is therefore required to maintain thread safety.
  • The Java object‘s fields are not accessible.

 

Parameters
object the Java object to inject into this WebView‘s JavaScript context. Null values are ignored.
name the name used to expose the object in JavaScript

 

 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

[Android]通過js方法回調部分native報錯 Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.