Skip to content

TextGlass/javascript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TextGlass Javascript Client

Releases

http://textglass.org/releases/javascript/

Example

http://textglass.org/trunk/javascript/

API

textglass.loadURLs(patternURL, attributeURL, patternPatchURL, attributePatchURL, readyCallback)

   patternURL - pattern file URL
   attributeURL - attribute file URL
   patternPatchURL - pattern patch file URL
   attributePatchURL - attribute patch file URL
   readyCallback - see textglass.readyCallback

   Type
     Function

   Description
     Loads a domain from URL. readyCallback is called when ready or on error.

   Return value
     None

textglass.readyCallback(state, msg, domain)

   state - state string, either 'error' or 'ready'
   msg - message string
   domain - domain name string

   Type
     Function

   Description
     This is the callback for textglass.loadURLs().

textglass.loadObjects(pattern, attribute, patternPatch, attributePatch)

   pattern - pattern object
   attribute - attribute object
   patternPatch - pattern patch object
   attributePatch - attribute patch object

   Type
     Function

   Description
     Loads a domain from Javascript JSON objects.

   Return value
     domain object

textglass.domains.[domain]

   name - domain name string
   version - domain version string
   classify - function, see textglass.domains.[domain].classify(input)
   error - boolean, set to true if textglass.loadObjects() fails
   msg - message string for textglass.loadObjects()

   Type
     Object

textglass.domains.[domain].classify(input)

   input - string to classify against domain

   Type
     Function

   Description
     Classifies a string against a domain.

   Return value
     Attribute map (object)

textglass.loaded

   Type
     Boolean

   Description
     True when textglass has been loaded.

Example Code

<script src="http://textglass.org/releases/javascript/latest/textglass.min.js"></script>

<script>
  var textglass = textglass || {};

  function load()
  {
    var osURL = 'http://textglass.org/releases/os/latest/patterns.min.json';
    var browserURL = 'http://textglass.org/releases/browser/latest/patterns.min.json';

    if(textglass.loaded)
    {
      textglass.loadURLs(osURL, undefined, undefined, undefined, domainReady);
      textglass.loadURLs(browserURL, undefined, undefined, undefined, domainReady);
    }
    else
    {
      alert('Error, couldn\'t load TextGlass Javascript client.');
    }
  }

  function domainReady(state, msg, domain)
  {
    if(state === 'error')
    {
      alert(msg);
    }
    else if(textglass.domains.browser && textglass.domains.os)
    {
      try
      {
          var browser = textglass.domains.browser.classify(navigator.userAgent);
          var os = textglass.domains.os.classify(navigator.userAgent);
      }
      catch(e) //fatal transformer error
      {
          alert(e);
          return;
      }

      var msg;

      if(browser.unknown)
      {
        msg = 'You are using an unknown browser.';
      }
      else
      {
        msg = 'Your browser is ' + browser.name + '.';
      }

      if(os.unknown)
      {
        msg += ' You are using an unknown operating system.';
      }
      else
      {
        msg += ' Your operating system is ' + os.name + '.';
      }

      alert(msg);
    }
  }

  load();
</script>