Skip to content

Commit fe55227

Browse files
bkrumnowvringar
authored andcommitted
Add screen size and window position
1 parent c9ef5c4 commit fe55227

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,22 @@ OpenWPM<sub>hide</sub> introduces two new parameters to the BrowserParams object
3232
Using both parameters works as follows:
3333

3434
```javascript
35-
NUM_BROWSERS = 2
36-
browser_params = [BrowserParams(display_mode="native") for _ in range(NUM_BROWSERS)]
37-
for browser_param in browser_params:
38-
browser_param.resolution = (1520, 630)
39-
browser_param.position = (50, 100)
35+
from hide_commands import (SetResolution, SetPosition)
36+
...
37+
command_sequence = CommandSequence(...)
38+
39+
command_sequence.append_command(SetResolution(width=1600, height=800), timeout=10)
40+
command_sequence.append_command(SetPosition(x=50, y=200), timeout=10)
4041
```
4142

4243
### Hardened JavaScript instrument
4344
Set _stealth_js_instrument_ to _True_ to activate the hardened version (similar as above):
4445

4546
```javascript
46-
NUM_BROWSERS = 2
47-
browser_params = [BrowserParams(display_mode="native") for _ in range(NUM_BROWSERS)]
48-
for browser_param in browser_params:
49-
browser_param.stealth_js_instrument = True
47+
NUM_BROWSERS = 2
48+
browser_params = [BrowserParams(display_mode="native") for _ in range(NUM_BROWSERS)]
49+
for browser_param in browser_params:
50+
browser_param.stealth_js_instrument = True
5051
```
5152

5253
Use the [settings.js](https://github.com/bkrumnow/OpenWPM/blob/stealth_extension/Extension/firefox/stealth.js/settings.js) file to

hide_commands/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .commands import (SetResolution, SetPosition)

hide_commands/commands.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
""" This file aims to demonstrate how to write custom commands in OpenWPM
2+
3+
Steps to have a custom command run as part of a CommandSequence
4+
5+
1. Create a class that derives from BaseCommand
6+
2. Implement the execute method
7+
3. Append it to the CommandSequence
8+
4. Execute the CommandSequence
9+
10+
"""
11+
import logging
12+
13+
from tkinter import ttk
14+
15+
from openwpm.commands.types import BaseCommand
16+
from openwpm.config import BrowserParams, ManagerParams
17+
from openwpm.socket_interface import ClientSocket
18+
19+
from selenium.webdriver import Firefox
20+
21+
22+
def get_screen_resolution(driver):
23+
return driver.execute_script("return [screen.width, screen.height];")
24+
25+
26+
class SetResolution(BaseCommand):
27+
""" Sets the browser window resolution """
28+
def __init__(self, width, height) -> None:
29+
self.logger = logging.getLogger("openwpm")
30+
self.width = width
31+
self.height = height
32+
33+
def __repr__(self) -> str:
34+
return "SetResolution"
35+
36+
def execute(
37+
self,
38+
driver: Firefox,
39+
browser_params: BrowserParams,
40+
manager_params: ManagerParams,
41+
extension_socket: ClientSocket,
42+
):
43+
44+
self.logger.info(f"Setting window resolution to {self.width} x {self.height} ")
45+
driver.set_window_size(self.width, self.height)
46+
47+
resolution = get_screen_resolution(driver)
48+
if resolution[0] <= self.width or resolution[1] <= self.height:
49+
self.logger.warn(
50+
f"Browser window resolution ({self.width} x {self.height}) exceeds " +
51+
f"screen resolution ({resolution[0]} x {resolution[1]})")
52+
53+
54+
55+
class SetPosition(BaseCommand):
56+
""" Sets the browser window position """
57+
def __init__(self, x, y) -> None:
58+
self.logger = logging.getLogger("openwpm")
59+
self.x = x
60+
self.y = y
61+
62+
def __repr__(self) -> str:
63+
return "SetPosition"
64+
65+
def execute(
66+
self,
67+
driver: Firefox,
68+
browser_params: BrowserParams,
69+
manager_params: ManagerParams,
70+
extension_socket: ClientSocket,
71+
):
72+
73+
driver.set_window_position(self.x, self.y, windowHandle='current')

0 commit comments

Comments
 (0)