Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating browser sensitive code for Google Chrome using GWT

I am writing a linker for a Google Chrome Extension. In the gwt.xml file, I need to add a line stating about the user agent property. For firefox, the pattern is as follows:

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

However, I am not able to find a corresponding value name for Google Chrome specific code.

like image 852
Saurabh Agarwal Avatar asked Apr 02 '26 04:04

Saurabh Agarwal


1 Answers

GWT treats Chrome and Safari the same, so there is only a "safari" agent value that covers both. So you can't do this just using user agent configurations.

However GWT's deferred binding mechanisms do have a way for you to tailor your code to properties that are only sniffed at runtime, by creating "property providers". This is basically how you would do this in your .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>

What the top part of the above does is define a new property "is.really.chrome" whose value will be determined by the Javascript code in the <property-provider> block when your application is loaded (this code gets inlined into the GWT startup sequence.)

The second part, the <replace-with>, shows how you would define a replacement rule that was sensitive to the value of this new property. This (and any other rules like it) will cause the GWT compiler to create an additional permutation of the code which is mostly the same as the safari version but with your chrome customizations.

This article is one of the best I've found on the subject: http://css.dzone.com/news/understanding-gwt-compiler

like image 52
Mark Allerton Avatar answered Apr 04 '26 15:04

Mark Allerton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!