即使对于没有任何 Javascript 的网页,Javascript 注入也应该起作用吗?
我的应用程序中的方法之一是使用 此线程在渲染后捕获网页的整个 HTML:
// This call inject JavaScript into the page which just finished rendering.
browser.loadUrl(
"javascript:window.HTMLOUT.processHTML(
document.getElementsByTagName('html')[0].innerHTML);");
它可以工作——即使对于非常丰富的网页——但是当它尝试以相同的方式处理没有 任何 Javascript 的页面,它会生成以下错误:
ERROR/Web Console(1335): Uncaught TypeError:
Cannot call method 'processHTML' of undefined at :1
在我搜索可能的根本原因或解释时,我发现了很多描述 的帖子未捕获的类型错误:无法调用未定义的方法 *** at :1
有两个典型答案:
- 使用 webView.getSettings().setDomStorageEnabled(true);
- "通常,当错误的形式为 Cannot call method 'X' of 未定义,这意味着您尝试调用的任何对象X from 不存在。”
好吧,在我的例子中,问题的原因不能是 #1 (setDomStorageEnabled),因为我已经正确设置了该设置在我的代码中!
所以唯一可能的解释是我尝试调用 processHTML()
的对象不存在,该对象名为 HTMLOUT
。在创建 WebView 对象并使用 setWebViewClient() 初始化它之后,在活动的 onCreate() 中创建:
webView.addJavascriptInterface(new JavascriptInterface(this, webView), "HTMLOUT");
这使我怀疑 JavascriptInterface 对象实例化由于某种原因失败,
这就是我的理解停止的地方 开始:
- 。我的问题 到目前为止我所描述的有道理吗?
- 如果是这样,为什么 JavascriptInterface 对象实例化会失败?
- 失败的原因是否可能是不存在? 原始 HTML 中有任何 Javascript 吗?
- 可以采取什么措施来解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论