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) |
Usage:
js = JSCaller(frame)
js.someJsMethod({"arg1":'test'}, "Hello World")