From b3158687ac64f6b9e274749fff02779c65040afb Mon Sep 17 00:00:00 2001 From: Chris Talib Date: Tue, 25 Jun 2024 16:51:25 +0200 Subject: [PATCH] first --- .gitignore | 1 + Dockerfile | 9 +++++++++ prepare_script.py | 29 +++++++++++++++++++++++++++++ run_js.py | 31 +++++++++++++++++++++++++++++++ test2.js | 10 ++++++++++ 5 files changed, 80 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 prepare_script.py create mode 100644 run_js.py create mode 100644 test2.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a2a8dea --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3a72d6d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM selenium/standalone-chrome:4.0.0 + +ENV SELENIUM_HOST=localhost \ + SELENIUM_PORT=4444 + +EXPOSE 4444 + +# docker run -d -p 4444:4444 --shm-size="4g" -t selenium +# docker logs -f hash \ No newline at end of file diff --git a/prepare_script.py b/prepare_script.py new file mode 100644 index 0000000..97b4695 --- /dev/null +++ b/prepare_script.py @@ -0,0 +1,29 @@ +import re +import sys + +def add_console_log_to_js(js_file_path): + with open(js_file_path, 'r') as file: + lines = file.readlines() + + return_pattern = re.compile(r'\s*return\s+(.*);') + + modified_lines = [] + for i, line in enumerate(lines): + match = return_pattern.search(line) + if match: + return_expression = match.group(1) + indentation = len(line) - len(line.lstrip()) + log_statement = f'{" " * indentation}console.log("Returning from function at line {i + 1}: ", {return_expression});\n' + modified_lines.append(log_statement) + modified_lines.append(line) + + with open(js_file_path, 'w') as file: + file.writelines(modified_lines) + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python prepare_script.py ") + sys.exit(1) + + js_file_path = sys.argv[1] + add_console_log_to_js(js_file_path) \ No newline at end of file diff --git a/run_js.py b/run_js.py new file mode 100644 index 0000000..e02cfc8 --- /dev/null +++ b/run_js.py @@ -0,0 +1,31 @@ +import sys +from selenium import webdriver + +def run_js_in_browser(js_file_path): + with open(js_file_path, 'r') as file: + js_code = file.read() + + options = webdriver.ChromeOptions() + options.set_capability('goog:loggingPrefs', {'browser': 'ALL'}) + + driver = webdriver.Remote( + command_executor='http://localhost:4444/wd/hub', + options=options, + ) + + driver.get("about:blank") + res = driver.execute_script(js_code) + print(res) + + for entry in driver.get_log('browser'): + print(entry) + + driver.quit() + +if __name__ == "__main__": + if len(sys.argv) < 2: + print("Usage: python run_js.py ") + sys.exit(1) + + js_file_path = sys.argv[1] + run_js_in_browser(js_file_path) \ No newline at end of file diff --git a/test2.js b/test2.js new file mode 100644 index 0000000..9b8b1fe --- /dev/null +++ b/test2.js @@ -0,0 +1,10 @@ +var a = "Hello from the other side"; +console.log(a); + +function greet() { + var b = "Hello darkness my old friend"; + console.log("Returning from function at line 6: ", b); + return b; +} + +greet();