Skip to content

Instantly share code, notes, and snippets.

@uhunkler
Created April 27, 2013 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uhunkler/5473742 to your computer and use it in GitHub Desktop.
Save uhunkler/5473742 to your computer and use it in GitHub Desktop.
browser communication object - JSTalk
// handle the browser communication
var browser =
{
name : "",
app : null,
win : null,
tab : null,
/**
* Initialise the browser object
* @param {string} name the browser name
*/
init : function( name )
{
if( typeof name === 'undefined' )
name = "Safari";
this.name = name;
this.connect();
},
/**
* Establish the connection to the browser
* @required {string} this.name the browser name
*/
connect : function()
{
if( this.name === "" )
return null;
this.app = SBApplication.application( this.name );
this.win = this.app.windows()[0];
// get the active tab
if( this.name === "Safari" || this.name === "WebKit" )
{
this.tab = this.win.currentTab();
}
else if( this.name === "Google Chrome" )
{
this.tab = this.win.activeTab();
}
},
/**
* Execute JavaScript in the browser
*
* @param {string} js JavaScript string
* @required {string} this.name the browser name
* @required {object} this.app the reference to the browser
* @required {object} this.tab the reference to the active tab
* @return {mixed} return value from JavaScript
*/
doJavaScript : function( js )
{
result = '';
if( this.app === null )
return result;
if( this.name === "Safari" || this.name === "WebKit" )
{
result = this.app.doJavaScript_in( js, this.tab );
}
else if( this.name === "Google Chrome" )
{
result = this.tab.executeJavascript( js );
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment