# Copyright (c) 2017–2018 crocoite contributors # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ Behavior scripts (i.e. subclasses of Behavior) are a powerful method to manipulate websites loaded into Chrome. They are executed by the controller after the page started loading (onload), after it has been idle for a while (onstop) and after loading was stopped (onfinish). The script’s excercise their power either through DevTools API calls or by injecting JavaScript into the page context. Thus they can manipulate both, the browser itself (DevTools; modify resolution, get DOM snapshot) as well as the page (JavaScript; trigger JavaScript events, call web API’s). They also emit (yield) data processable by any consumer registered to the controller. This allows storing captured screenshots inside WARC files, for instance. """ import asyncio, json, os.path from base64 import b64decode from collections import OrderedDict import pkg_resources from html5lib.serializer import HTMLSerializer from yarl import URL import yaml from .util import getFormattedViewportMetrics from . import html from .html import StripAttributeFilter, StripTagFilter, ChromeTreeWalker from .devtools import Crashed, TabException class Script: """ A JavaScript resource """ __slots__ = ('path', 'data') datadir = 'data' def __init__ (self, path=None, encoding='utf-8'): self.path = path if path: self.data = pkg_resources.resource_string (__name__, os.path.join (self.datadir, path)).decode (encoding) def __repr__ (self): return f'