Skip to content

Instantly share code, notes, and snippets.

@NathanW2
Last active August 29, 2015 13:57
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 NathanW2/9632296 to your computer and use it in GitHub Desktop.
Save NathanW2/9632296 to your computer and use it in GitHub Desktop.

Usage:

js = JSCaller(frame)
js.someJsMethod({"arg1":'test'}, "Hello World")
class JSCaller(object):
"""
Convenience class to allows calling JS methods as Python attributes.
"""
def __init__(self, frame):
self.frame = frame
def __getattr__(self, item):
return functools.partial(self._jscall, item)
def _jscall(self, method, *args):
def formatargs(args):
for arg in args:
if isinstance(arg, dict):
arg = json.dumps(arg)
elif isinstance(arg, basestring):
arg = '"{}"'.format(arg)
yield arg
argsformated = ",".join(formatargs(args)).lstrip(',')
call = "{method}({args});".format(method=method, args=argsformated)
logger.debug("jscall: {}".format(call))
return self.frame.evaluateJavaScript(call)
@NathanW2
Copy link
Author

Usage:

js = JSCaller(frame)
js.someJsMethod({"arg1":'test'}, "Hello World")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment