使用GWT为Google Chrome生成浏览器敏感代码

使用GWT为Google Chrome生成浏览器敏感代码,gwt,google-chrome,google-chrome-extension,Gwt,Google Chrome,Google Chrome Extension,我正在为Google Chrome扩展写一个链接器。在gwt.xml文件中,我需要添加一行,说明用户代理属性。对于firefox,模式如下所示: <set-property name="user.agent" value="gecko1_8"/> 但是,我无法为Google Chrome特定代码找到相应的值名称。以下是示例。令人困惑的是,chrome的名字是“Safari”。(这可能是因为这两种浏览器都基于webkit,但我可能错了。) GWT将Chrome和Safari视为相

我正在为Google Chrome扩展写一个链接器。在gwt.xml文件中,我需要添加一行,说明用户代理属性。对于firefox,模式如下所示:

<set-property name="user.agent" value="gecko1_8"/>

但是,我无法为Google Chrome特定代码找到相应的值名称。

以下是示例。令人困惑的是,chrome的名字是“Safari”。(这可能是因为这两种浏览器都基于webkit,但我可能错了。)


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”,其值将由加载应用程序时
块中的Javascript代码确定(此代码内联到GWT启动序列中)

第二部分,即
,展示了如何定义对新属性的值敏感的替换规则。这(以及任何其他类似的规则)将导致GWT编译器创建一个额外的代码排列,该排列与safari版本基本相同,但带有您的chrome定制


这篇文章是我在这方面找到的最好的文章之一:

可能与Alt有关,尽管他们没有提供google chrome代码。谢谢!这个答案真的很全面,很有帮助。我的应用程序现在可以使用:)
  <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>