summaryrefslogtreecommitdiff
path: root/crocoite/behavior.py
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2018-12-01 13:14:06 +0100
committerLars-Dominik Braun <lars@6xq.net>2018-12-01 13:56:44 +0100
commit22adde79940d32c5f094f26f3e18b7160e7ccafc (patch)
tree8fb59939a8281e33c2c05c140409430c30ed5c58 /crocoite/behavior.py
parent6176991ac7ff0e6dcb4612b43da89abd350e3aa5 (diff)
downloadcrocoite-22adde79940d32c5f094f26f3e18b7160e7ccafc.tar.gz
crocoite-22adde79940d32c5f094f26f3e18b7160e7ccafc.tar.bz2
crocoite-22adde79940d32c5f094f26f3e18b7160e7ccafc.zip
behavior: Move click script data to external file
First step of issue #3
Diffstat (limited to 'crocoite/behavior.py')
-rw-r--r--crocoite/behavior.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/crocoite/behavior.py b/crocoite/behavior.py
index 5f66538..8cc7ab4 100644
--- a/crocoite/behavior.py
+++ b/crocoite/behavior.py
@@ -30,6 +30,7 @@ from collections import OrderedDict
import pkg_resources
from html5lib.serializer import HTMLSerializer
+import yaml
from .util import randomString, getFormattedViewportMetrics, removeFragment
from . import html
@@ -107,7 +108,7 @@ class HostnameFilter:
class JsOnload (Behavior):
""" Execute JavaScript on page load """
- __slots__ = ('script', 'context')
+ __slots__ = ('script', 'context', 'options')
scriptPath = None
@@ -115,15 +116,32 @@ class JsOnload (Behavior):
super ().__init__ (loader, logger)
self.script = Script (self.scriptPath)
self.context = None
+ # options passed to constructor
+ self.options = {}
async def onload (self):
tab = self.loader.tab
yield self.script
+
+ # This is slightly awkward, since we cannot compile the class into an
+ # objectId and then reference it. Therefore the script must return a
+ # class constructor, which is then called with a generic options
+ # parameter.
+ # XXX: is there a better way to do this?
result = await tab.Runtime.evaluate (expression=str (self.script))
exception = result.get ('exceptionDetails', None)
result = result['result']
- assert result['type'] == 'object'
+ assert result['type'] == 'function', result
assert result.get ('subtype') != 'error', exception
+ constructor = result['objectId']
+
+ result = await tab.Runtime.callFunctionOn (
+ functionDeclaration='function(options){return new this(options);}',
+ objectId=constructor,
+ arguments=[{'value': self.options}])
+ result = result['result']
+ assert result['type'] == 'object', result
+ assert result.get ('subtype') != 'error', result
self.context = result['objectId']
async def onstop (self):
@@ -278,6 +296,11 @@ class Click (JsOnload):
name = 'click'
scriptPath = 'click.js'
+ def __init__ (self, loader, logger):
+ super ().__init__ (loader, logger)
+ with pkg_resources.resource_stream (__name__, os.path.join ('data', 'click.yaml')) as fd:
+ self.options['sites'] = list (yaml.load_all (fd))
+
class ExtractLinksEvent:
__slots__ = ('links', )