即使对于没有任何 Javascript 的网页,Javascript 注入也应该起作用吗?

发布于 2024-12-02 17:12:01 字数 1691 浏览 1 评论 0原文

我的应用程序中的方法之一是使用 此线程在渲染后捕获网页的整个 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 有两个典型答案:

  1. 使用 webView.getSettings().setDomStorageEnabled(true);
  2. "通常,当错误的形式为 Cannot call method 'X' of 未定义,这意味着您尝试调用的任何对象X from 不存在。”

好吧,在我的例子中,问题的原因不能是 #1 (setDomStorageEnabled),因为我已经正确设置了该设置在我的代码中!

所以唯一可能的解释是我尝试调用 processHTML() 的对象不存在,该对象名为 HTMLOUT 。在创建 WebView 对象并使用 setWebViewClient() 初始化它之后,在活动的 onCreate() 中创建:

webView.addJavascriptInterface(new JavascriptInterface(this, webView), "HTMLOUT");

这使我怀疑 JavascriptInterface 对象实例化由于某种原因失败,

这就是我的理解停止的地方 开始:

  1. 。我的问题 到目前为止我所描述的有道理吗?
  2. 如果是这样,为什么 JavascriptInterface 对象实例化会失败?
  3. 失败的原因是否可能是不存在? 原始 HTML 中有任何 Javascript 吗?
  4. 可以采取什么措施来解决这个问题?

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:

  1. Use webView.getSettings().setDomStorageEnabled(true);
  2. "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:

  1. Does what I have been describing so far make sense?
  2. If so, why would the JavascriptInterface object instantiation fail?
  3. Is it possible that the reason for the failure is the non-existence
    of any Javascript in the original HTML?
  4. What can be done to work around this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文