Created
April 27, 2013 16:45
-
-
Save uhunkler/5473742 to your computer and use it in GitHub Desktop.
browser communication object - JSTalk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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