From 445288d5e76fbc287b582ab2bfb2f9e243e052cc Mon Sep 17 00:00:00 2001 From: Eldon Date: Thu, 13 Feb 2014 01:00:39 -0500 Subject: [PATCH 1/3] First few behaviors --- umbra/behaviors.json | 20 ++++++++++++++++++++ umbra/behaviors.py | 13 +++++++++++++ umbra/umbra.py | 32 ++++++++++++++++++++++++++------ 3 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 umbra/behaviors.json create mode 100644 umbra/behaviors.py diff --git a/umbra/behaviors.json b/umbra/behaviors.json new file mode 100644 index 0000000..2c15565 --- /dev/null +++ b/umbra/behaviors.json @@ -0,0 +1,20 @@ +[ + { + "scripts": [ + "setInterval(function() { window.scrollBy(10000,10000); }, 1000);" + ], + "site": ".*" + }, + { + "scripts": [ + "setInterval(function() { a = document.evaluate( '//a[(@href = \"#\" and @role = \"button\" and contains(.,\"more comments\")) or starts-with(@href, \"/browse/likes\")]', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null ); f = a.iterateNext(); f.click();}, 1000);" + ], + "site": ".*facebook.com.*" + }, + { + "scripts": [ + "" + ], + "site": ".*flickr.com.*" + } +] diff --git a/umbra/behaviors.py b/umbra/behaviors.py new file mode 100644 index 0000000..fffc714 --- /dev/null +++ b/umbra/behaviors.py @@ -0,0 +1,13 @@ +from json import dumps, load +from time import sleep +import os, re + +behaviors_file = os.path.sep.join(__file__.split(os.path.sep)[:-1] + ['behaviors.json']) +def execute(url, ws, command_id): + sleep(5) + with open(behaviors_file) as js: + behaviors = load(js) + for behavior in behaviors: + if re.match(behavior['site'], url): + for script in behavior['scripts']: + ws.send(dumps(dict(method="Runtime.evaluate", params={"expression": script}, id=next(command_id)))) diff --git a/umbra/umbra.py b/umbra/umbra.py index a5f4959..47cc741 100755 --- a/umbra/umbra.py +++ b/umbra/umbra.py @@ -20,10 +20,30 @@ class Umbra: self.producer_lock = threading.Lock() self.consume_amqp() - def get_message_handler(self, url, url_metadata): + def watchdog(self, command_id): + def wrapped(): + timer = None + while True: + ws = yield + if timer: + self.logger.info("Cancelling") + timer.cancel() + def go(): + close_exp = "window.open('', '_self', ''); window.close(); " + ws.send(dumps(dict(method="Runtime.evaluate", params={"expression": close_exp}, id=next(command_id)))) + self.logger.info("Going") + ws.close() + timer = threading.Timer(10, go) + timer.start() + result = wrapped() + next(result) + return result + + def get_message_handler(self, url, url_metadata, command_id): + this_watchdog = self.watchdog(command_id) def handle_message(ws, message): + this_watchdog.send(ws) message = loads(message) - self.logger.info(message) if "method" in message.keys() and message["method"] == "Network.requestWillBeSent": to_send = {} to_send.update(message['params']['request']) @@ -47,19 +67,19 @@ class Umbra: def fetch_url(self, body, message): url, metadata = body['url'], body['metadata'] + command_id = count(1) def send_websocket_commands(ws): - command_id = count(1) ws.send(dumps(dict(method="Network.enable", id=next(command_id)))) ws.send(dumps(dict(method="Page.navigate", id=next(command_id), params={"url": url}))) - #from umbra import behaviors - #behaviors.execute(url, ws, command_id) + from umbra import behaviors + behaviors.execute(url, ws, command_id) message.ack() with Chrome(*self.chrome_args) as websocket_url: websock = websocket.WebSocketApp(websocket_url) - websock.on_message = self.get_message_handler(url, metadata) + websock.on_message = self.get_message_handler(url, metadata, command_id) websock.on_open = send_websocket_commands websock.run_forever() From af01fcbcfe20d1b9affb14289a1a93637f504308 Mon Sep 17 00:00:00 2001 From: Eldon Date: Thu, 13 Feb 2014 13:32:34 -0500 Subject: [PATCH 2/3] Add more flickr behavior --- umbra/behaviors.json | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/umbra/behaviors.json b/umbra/behaviors.json index 2c15565..360c014 100644 --- a/umbra/behaviors.json +++ b/umbra/behaviors.json @@ -1,20 +1,27 @@ [ { "scripts": [ - "setInterval(function() { window.scrollBy(10000,10000); }, 1000);" + "setInterval(function() {}, 1000);" ], "site": ".*" }, { "scripts": [ - "setInterval(function() { a = document.evaluate( '//a[(@href = \"#\" and @role = \"button\" and contains(.,\"more comments\")) or starts-with(@href, \"/browse/likes\")]', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null ); f = a.iterateNext(); f.click();}, 1000);" + "setTimeout(function() { setInterval(function() { a = document.evaluate( '//a[(@href = \"#\" and @role = \"button\" and contains(.,\"more comments\")) or starts-with(@href, \"/browse/likes\")]', document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null ); f = a.iterateNext(); f.click();}, 1000);}, 5000);" ], "site": ".*facebook.com.*" }, { "scripts": [ - "" + "setTimeout(function() { a = document.evaluate( \"//a[contains(@class, 'sn-ico-slideshow')]\", document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null ); f = a.iterateNext(); f.click();}, 5000);" + ], + "site": ".*flickr.com.*" + }, + { + "scripts": [ + "setTimeout(function() { a = document.evaluate( \"//a[contains(@data-track, 'photo-click')]\", document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null ); setInterval(function() { f = a.iterateNext(); f.click();}, 5000)}, 5000);" ], "site": ".*flickr.com.*" } + ] From fe15932c26ae2d45bbc7c7820ebc88c5b8d59cb3 Mon Sep 17 00:00:00 2001 From: Eldon Date: Thu, 13 Feb 2014 13:37:08 -0500 Subject: [PATCH 3/3] Click on photos in gallery behavior --- umbra/behaviors.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/umbra/behaviors.json b/umbra/behaviors.json index 360c014..095491c 100644 --- a/umbra/behaviors.json +++ b/umbra/behaviors.json @@ -1,7 +1,7 @@ [ { "scripts": [ - "setInterval(function() {}, 1000);" + "setInterval(function() { window.scrollBy(10000,10000); }, 1000);" ], "site": ".*" },