One of the methods in my applications is using the Javascript injection trick described in this thread to capture the entire HTML of a web page after rendering:
// This call inject JavaScript into the page which just finished rendering.
browser.loadUrl(
"javascript:window.HTMLOUT.processHTML(
document.getElementsByTagName('html')[0].innerHTML);");
It works -- even for very rich web pages -- but when it tries to handle in the same manner pages that do not have any Javascript in them, it generates the following error:
ERROR/Web Console(1335): Uncaught TypeError:
Cannot call method 'processHTML' of undefined at :1
In my search for possible root cause or explanation, I found quite a few postings describing an Uncaught TypeError: Cannot call method *** of undefined at :1
with two typical answers:
- Use webView.getSettings().setDomStorageEnabled(true);
- "Usually, when the error is of the form Cannot call method 'X' of undefined, it means that whatever object you are attempting to call X from does not exist."
Well, the cause of the problem in my case cannot be #1 (setDomStorageEnabled), because I already have that set correctly in my code!
So the only other possible explanation is that the object from which I am trying to call processHTML()
does not exits. That object is named HTMLOUT
and it is created in the activity's onCreate(), after creating a WebView object and initializing it with setWebViewClient()
:
webView.addJavascriptInterface(new JavascriptInterface(this, webView), "HTMLOUT");
That leads me to suspect that the JavascriptInterface object instantiation fails for some reason.
This is where my understanding stops and my questions begin:
- Does what I have been describing so far make sense?
- If so, why would the 开发者_开发知识库JavascriptInterface object instantiation fail?
- Is it possible that the reason for the failure is the non-existence of any Javascript in the original HTML?
- What can be done to work around this problem?
精彩评论