Data provided to your scripts for responses

GreasySpoon service provides following information to scripts applying on server responses:

  • requestheader : string containing HTTP request header, as generated by user's browser. This header cannot be modified and is only provided in order for your script to adapt processing based on initial request (for example, by changing the response based on the browser type and version provided in the response).
  • responseheader : string containing HTTP response header, as returned by the server.
  • httpresponse   :  string containing HTTP response body
  • user_id  :  string containing either user id (login) or user ip address. User ID will be available if authentication is activated on HTTP proxy.
  • user_group   :  string containing user group if available, or null if not. User group might be available if authentication is activated on HTTP proxy.
  • sharedcache :  a java hash table<String, Object> that is shared between all scripts

Like for requests scripts, information is provided as static variables so can be accessed/modified in any part of the script.
All information is provided as string to avoid casting on Script Engine side, except the shared cache which is transmitted as a Java hashtable. To use this hash table, standard Java methods must be used:

  • get(<string>key) to retrieve content associated to given key
  • put(<string>key, <object>value) to store a content in the table using given key

Any modification made on HTTP response header and/or body will be handled by GreasySpoon service and will be reflected to the final response provided to the user. Please note that HTTP request header is only provided for potential scripts usage and that changes made on it will be bounded to the script execution context.

In order to illustrate scripts possibilities in response mode, two basic examples are presented hereafter:

  • A first basic hello world script in EcmaScript
  • A more complex example that takes advantage of ruby libraries. This script demonstrates ruby Hpricot library capacity to manipulate HTML content in a simple way.

Hello world response script
//-------------------------------------------------------------------
//Hello World Ecmascript for GreasySpoon:
// -Apply script on any URL except for Sourceforge domain whatever the TLD (.com, .org, .net, etc.)
// - find and insert a message after body tag
// - use sharedcache to store a request counter
//-------------------------------------------------------------------
//==ServerScript==
//@name helloworld
//@status on
//@description Simple HelloWorld Javascript demo
//@include .*
//@exclude http://.*\.sourceforge\..{2,3}/.*
//@responsecode 200
//==/ServerScript==
//---------------
//GS variables used by the script:
//httpresponse : (String)HTTP response body
//sharedcache : (hashtable<String, Object>) shared table between all scripts
//user_id : (String)user id (login or user ip address)
//user_group : (String)user group or user fqdn
//---------------
i = sharedcache.get("counter");//Get or create a counter for responses
i++;
 
//Locate HTML <BODY ...> tag
bodypos = httpresponse.indexOf(">",httpresponse.toLowerCase().indexOf("<body")) +1;
 
//Insert Hello World just after the body tag
httpresponse=httpresponse.substring(0,bodypos )
   +"<h1>Hello World message n°"+i+" !!! You're identified on GreasySpoon with following parameters:</h1>"
   +"<h2>User ID:"+ user_id+"</h2>"
   +"<h2>User Group:"+user_group+"</h2>"
   +httpresponse.substring(bodypos);
 
//Store new counter value in shared cache, so value will be available for next requests
sharedcache.put("counter",i);
 
DOM manipulation with Ruby and Hpricot
#------------------------------------------------------------
#Simple ruby script to demonstrate DOM manipulation
#To use it, you need jruby and hpricot ruby library installed
#------------------------------------------------------------
#==ServerScript==
#@name CustomizePage
#@description Customize test page by replacing/changing content
#@status on
#@include http://www\.google\..{2,3}/.* Apply script on target domain only
#==/ServerScript==
#---------------
#Libraries used by script
require 'hpricot'
require 'open-uri'
 
#Content modification function
def process(httpresponse)
   doc=Hpricot(httpresponse)
 
   #change background color
   (doc/"body").set("style","background: rgb(0, 0, 0) none repeat scroll 0% 0%;")
 
   # Change a search link to google for example
   (doc/"#search").set("href","http://www.google.com")
   #Load content from another web server and prepend part of it to current page
   anotherdoc
=Hpricot(open('http://www.anotherwebsite.com',:proxy=>'http://myproxy:3128'))
   doc/"#res").prepend((anotherdoc/"#div_id").to_html)
   return
"#{doc}"
end
 
#Main method: just call modification function to adapt response
$httpresponse=process($httpresponse)