From c9ec2101f377a3ad3f55b801b1cdd6a7c34b1b44 Mon Sep 17 00:00:00 2001 From: bkrumnow Date: Tue, 1 Nov 2022 19:06:59 +0100 Subject: [PATCH] Add screen size and window position --- README.md | 19 +++++----- hide_commands/__init__.py | 1 + hide_commands/commands.py | 73 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 hide_commands/__init__.py create mode 100644 hide_commands/commands.py diff --git a/README.md b/README.md index 52375a2b5..aea7cbf09 100644 --- a/README.md +++ b/README.md @@ -32,21 +32,22 @@ OpenWPMhide introduces two new parameters to the BrowserParams object Using both parameters works as follows: ```javascript -NUM_BROWSERS = 2 -browser_params = [BrowserParams(display_mode="native") for _ in range(NUM_BROWSERS)] -for browser_param in browser_params: - browser_param.resolution = (1520, 630) - browser_param.position = (50, 100) +from hide_commands import (SetResolution, SetPosition) +... +command_sequence = CommandSequence(...) + +command_sequence.append_command(SetResolution(width=1600, height=800), timeout=10) +command_sequence.append_command(SetPosition(x=50, y=200), timeout=10) ``` ### Hardened JavaScript instrument Set _stealth_js_instrument_ to _True_ to activate the hardened version (similar as above): ```javascript - NUM_BROWSERS = 2 - browser_params = [BrowserParams(display_mode="native") for _ in range(NUM_BROWSERS)] - for browser_param in browser_params: - browser_param.stealth_js_instrument = True +NUM_BROWSERS = 2 +browser_params = [BrowserParams(display_mode="native") for _ in range(NUM_BROWSERS)] +for browser_param in browser_params: + browser_param.stealth_js_instrument = True ``` Use the [settings.js](https://github.com/bkrumnow/OpenWPM/blob/stealth_extension/Extension/firefox/stealth.js/settings.js) file to diff --git a/hide_commands/__init__.py b/hide_commands/__init__.py new file mode 100644 index 000000000..f36d7748f --- /dev/null +++ b/hide_commands/__init__.py @@ -0,0 +1 @@ +from .commands import (SetResolution, SetPosition) \ No newline at end of file diff --git a/hide_commands/commands.py b/hide_commands/commands.py new file mode 100644 index 000000000..f23090a2c --- /dev/null +++ b/hide_commands/commands.py @@ -0,0 +1,73 @@ +""" 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 tkinter import ttk + +from openwpm.commands.types import BaseCommand +from openwpm.config import BrowserParams, ManagerParams +from openwpm.socket_interface import ClientSocket + +from selenium.webdriver import Firefox + + +def get_screen_resolution(driver): + return driver.execute_script("return [screen.width, screen.height];") + + +class SetResolution(BaseCommand): + """ Sets the browser window resolution """ + def __init__(self, width, height) -> None: + self.logger = logging.getLogger("openwpm") + self.width = width + self.height = height + + def __repr__(self) -> str: + return "SetResolution" + + def execute( + self, + driver: Firefox, + browser_params: BrowserParams, + manager_params: ManagerParams, + extension_socket: ClientSocket, + ): + + self.logger.info(f"Setting window resolution to {self.width} x {self.height} ") + driver.set_window_size(self.width, self.height) + + resolution = get_screen_resolution(driver) + if resolution[0] <= self.width or resolution[1] <= self.height: + self.logger.warn( + f"Browser window resolution ({self.width} x {self.height}) exceeds " + + f"screen resolution ({resolution[0]} x {resolution[1]})") + + + +class SetPosition(BaseCommand): + """ Sets the browser window position """ + def __init__(self, x, y) -> None: + self.logger = logging.getLogger("openwpm") + self.x = x + self.y = y + + def __repr__(self) -> str: + return "SetPosition" + + def execute( + self, + driver: Firefox, + browser_params: BrowserParams, + manager_params: ManagerParams, + extension_socket: ClientSocket, + ): + + driver.set_window_position(self.x, self.y, windowHandle='current') \ No newline at end of file