我正在为 Google Chrome 扩展程序编写链接器。在 gwt.xml 文件中,我需要添加一行来说明用户代理属性。对于Firefox,模式如下:
<set-property name="user.agent" value="gecko1_8"/>
但是,我无法为 Google Chrome 特定代码找到相应的值名称。
我正在为 Google Chrome 扩展程序编写链接器。在 gwt.xml 文件中,我需要添加一行来说明用户代理属性。对于Firefox,模式如下:
<set-property name="user.agent" value="gecko1_8"/>
但是,我无法为 Google Chrome 特定代码找到相应的值名称。
这是支持的用户代理列表。令人困惑的是,chrome 的一个是“Safari”。(这可能是因为这两个浏览器都是基于 webkit 的,但我可能是错的。)
<set-property name="user.agent" value="safari"/>
GWT 将 Chrome 和 Safari 视为相同,因此只有一个涵盖两者的“safari”代理值。所以你不能仅仅使用用户代理配置来做到这一点。
然而,GWT 的延迟绑定机制确实有一种方法可以让您通过创建“属性提供程序”来将代码定制为仅在运行时被嗅探的属性。这基本上就是您在 .gwt.xml 中执行此操作的方式:
<define-property name="is.really.chrome" values="false,true"/>
<property-provider name="is.really.chrome"><![CDATA[
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("applewebkit") != -1) {
if (ua.indexOf("chrome") != -1) {
return true;
}
}
return false;
]]></property-provider>
<replace-with
class="some.implementation.of.interface">
<when-type-is class="some.interface.used.with.GWT.create"/>
<when-property-is name="user.agent" value="safari"/>
<when-property-is name="is.really.chrome" value="true"/>
</replace-with>
上面的部分所做的是定义一个新属性“is.really.chrome”,其值将由<property-provider>
加载应用程序时块中的 Javascript 代码确定(此代码内联到 GWT 启动序列中。)
第二部分,<replace-with>
说明如何定义对这个新属性的值敏感的替换规则。这(以及任何其他类似的规则)将导致 GWT 编译器创建代码的额外排列,该排列与 safari 版本基本相同,但具有您的 chrome 自定义。
这篇文章是我在这个主题上找到的最好的文章之一:http: //css.dzone.com/news/understanding-gwt-compiler