2012-06-26 36 views
3

我正在为Google Chrome扩展程序编写链接程序。在gwt.xml文件中,我需要添加一行说明用户代理属性的行。对于Firefox,模式如下:使用GWT为Google Chrome生成浏览器敏感代码

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

但是,我无法找到谷歌浏览器特定的代码对应的名。

+1

可能与http://stackoverflow.com/questions/7992678/what-are-the-possible-user-agent -values-in-gwt-xml,尽管它们不提供谷歌浏览器代码。 – David

回答

3

GWT对待Chrome和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来决定代码在<property-provider>块当你的应用程序被加载(此代码被内联到GWT启动顺序。)

的第二部分,<replace-with>,表明你将如何定义,这是这个新的价值敏感的替代规则属性。这个(以及任何其他规则)将导致GWT编译器创建一个额外的代码排列,它与safari版本基本相同,但是使用chrome自定义。

这篇文章是我关于这个问题找到了最好的一个:http://css.dzone.com/news/understanding-gwt-compiler

+0

谢谢!这个答案非常全面和有帮助。我的申请现在可以运作 –

3

这是list of supported useragents。令人困惑的是,铬合金是“Safari”。 (这可能是由于这两种浏览器是基于WebKit的,但我可能是错的。)

<set-property name="user.agent" value="safari"/> 
相关问题