-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathcustom_command.py
More file actions
90 lines (70 loc) · 3.09 KB
/
Copy pathcustom_command.py
File metadata and controls
90 lines (70 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""This file aims to demonstrate how to write custom commands in OpenWPM
Steps to have a custom command run as part of a CommandSequence
1. Create a class that derives from BaseCommand
2. Implement the execute method
3. Append it to the CommandSequence
4. Execute the CommandSequence
"""
import logging
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from openwpm.commands.types import BaseCommand
from openwpm.config import BrowserParams, ManagerParams
from openwpm.socket_interface import ClientSocket
class LinkCountingCommand(BaseCommand):
"""This command logs how many links it found on any given page"""
def __init__(self) -> None:
self.logger = logging.getLogger("openwpm")
# While this is not strictly necessary, we use the repr of a command for logging
# So not having a proper repr will make your logs a lot less useful
def __repr__(self) -> str:
return "LinkCountingCommand"
# Have a look at openwpm.commands.types.BaseCommand.execute to see
# an explanation of each parameter
def execute(
self,
webdriver: Firefox,
browser_params: BrowserParams,
manager_params: ManagerParams,
extension_socket: ClientSocket,
) -> None:
current_url = webdriver.current_url
link_count = len(webdriver.find_elements(By.TAG_NAME, "a"))
self.logger.info("There are %d links on %s", link_count, current_url)
def get_screen_resolution(webdriver: Firefox) -> list[int]:
"""Returns the screen resolution [width, height] as seen by the page."""
return webdriver.execute_script("return [screen.width, screen.height];")
class SetResolution(BaseCommand):
"""Sets the browser window to a realistic resolution.
Many crawls run headless or in a virtual framebuffer where the default
window size is unusual and easy to fingerprint. Setting a common, realistic
resolution makes the browser blend in better with ordinary visitors.
"""
# A common desktop resolution; override per-crawl as needed.
def __init__(self, width: int = 1920, height: int = 1080) -> None:
self.logger = logging.getLogger("openwpm")
self.width = width
self.height = height
def __repr__(self) -> str:
return f"SetResolution({self.width}, {self.height})"
def execute(
self,
webdriver: Firefox,
browser_params: BrowserParams,
manager_params: ManagerParams,
extension_socket: ClientSocket,
) -> None:
self.logger.info(
"Setting window resolution to %d x %d", self.width, self.height
)
webdriver.set_window_size(self.width, self.height)
screen_width, screen_height = get_screen_resolution(webdriver)
if self.width > screen_width or self.height > screen_height:
self.logger.warning(
"Requested window resolution (%d x %d) exceeds the available "
"screen resolution (%d x %d); the window may be clamped",
self.width,
self.height,
screen_width,
screen_height,
)