“安装触发器”没有定义

发布于 2025-01-01 02:24:32 字数 557 浏览 2 评论 0原文

在我的 html 页面中,我有类似这样的代码,其中仅当浏览器是 Firefox 时我才安装扩展:

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
{
  //relevant code
  InstallTrigger.install(InstallXPI);
}

它在每个浏览器中都可以正常工作。但是当通过 htmlunit 框架使用同一页面并在 webclient 中使用 browserversion.FIREFOX_3_6 参数时。它在那里显示一个错误:

com.gargoylesoftware.htmlunit.ScriptException: Wrapped 
com.gargoylesoftware.htmlunit.ScriptException: Wrapped 
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "InstallTrigger" is not defined.

对此有什么想法吗?

In my html page i have code something like this, where i have installed an extension only if the browser is Firefox:

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
{
  //relevant code
  InstallTrigger.install(InstallXPI);
}

It works fine in every browser. But when the same page is used through htmlunit framework and using browserversion.FIREFOX_3_6 argument in webclient. It shows an error there:

com.gargoylesoftware.htmlunit.ScriptException: Wrapped 
com.gargoylesoftware.htmlunit.ScriptException: Wrapped 
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "InstallTrigger" is not defined.

Any idea about this?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2025-01-08 02:24:32

这是给您的提醒:不要使用浏览器检测,使用功能检测。您的代码存在问题:

  • InstallTrigger 是 Gecko 引擎的功能,而不是 Firefox 的功能。然而,您在用户代理字符串中明确查找“Firefox”,并且可能会排除其他基于 Gecko 引擎的浏览器(例如 SeaMonkey、K-Meleon、Camino)。
  • 用户代理字符串可以被欺骗,这显然是 htmlunit 正在做的事情 - 尽管使用不同的浏览器引擎,但它声称是 Firefox。那么你的代码就会遇到麻烦。

以下是正确的做法:

if ("InstallTrigger" in window)
{
  // Gecko platform, InstallTrigger available
  InstallTrigger.install(InstallXPI);
}

This is a reminder for you: don't use browser detection, use feature detection. The issues with your code:

  • InstallTrigger is a feature of the Gecko engine, not Firefox. You are explicitly looking for "Firefox" in the user agent string however and might exclude other browsers based on the Gecko engine (there are e.g. SeaMonkey, K-Meleon, Camino).
  • User agent strings can be spoofed which is apparently what htmlunit is doing - it claims to be Firefox despite using a different browser engine. Your code will run into trouble then.

Here is how you would do it properly:

if ("InstallTrigger" in window)
{
  // Gecko platform, InstallTrigger available
  InstallTrigger.install(InstallXPI);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文