-
Notifications
You must be signed in to change notification settings - Fork 1
/
libchrome.py
276 lines (235 loc) · 8.99 KB
/
libchrome.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import json
import os
import shutil
import socket
import subprocess
import time
import traceback
from datetime import datetime
from pathlib import Path
from random import randint
from typing import Any, Optional
import userpaths
from colorama import init
from liblogger import log_err, log_inf
from libwebsocket import WebSocketServer
CUR_DIR = str(Path(__file__).parent.absolute())
TEMP_DIR = os.path.join(CUR_DIR, "temp")
EXTENSION_DIR = os.path.join(CUR_DIR, "ext")
class ChromeElem:
def __init__(self, selector: Optional[str] = None):
self.selector = selector
class Chrome:
def __init__(
self,
init_url: str = "http://example.com",
width: int = 0,
height: int = 0,
block_image: bool = False,
user_data_dir: Optional[str] = None,
):
self.__init_url = init_url
self.__width = width
self.__height = height
self.__block_image = block_image
self.__user_data_dir = user_data_dir
self.__process = None
self.__client_unit = None
def __find_port(self) -> int:
with socket.socket() as s:
s.bind(("", 0)) # Bind to a free port provided by the host
return s.getsockname()[1] # Return the assigned port number
def __send_command(self, msg: str, payload: Optional[str] = None) -> Any:
ret = None
try:
if self.__client_unit != None:
if payload != None:
self.__client_unit.send(
json.dumps(
{
"msg": msg,
"payload": payload,
}
)
)
else:
self.__client_unit.send(
json.dumps(
{
"msg": msg,
}
)
)
resp = self.__client_unit.recv()
if resp != None:
js_res = json.loads(resp)["result"]
if js_res != "<undefined>":
ret = js_res
else:
log_err("resp is none")
time.sleep(0.5)
else:
log_err("client_unit is none")
except:
traceback.print_exc()
return ret
def start(self):
chrome_path = ""
chrome_est_paths = [
userpaths.get_local_appdata() + "\\Google\\Chrome\\Application\\Chrome.exe",
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\Chrome.exe",
"C:\\Program Files\\Google\\Chrome\\Application\\Chrome.exe",
]
for chrome_est_path in chrome_est_paths:
if os.path.isfile(chrome_est_path):
chrome_path = chrome_est_path
break
if os.path.isfile(chrome_path):
# find free port
port = self.__find_port()
# start socket server
websocket_server = WebSocketServer("127.0.0.1", port)
websocket_server.start()
# copy extension to temp folder
ext_dir = os.path.join(TEMP_DIR, f"ext_{datetime.now().timestamp()}")
shutil.copytree(EXTENSION_DIR, ext_dir, dirs_exist_ok=True)
# set extension port number
background_js_path = os.path.join(ext_dir, "background.js")
with open(background_js_path, "r") as f:
background_js_content = f.read()
with open(background_js_path, "w") as f:
f.write(background_js_content.replace("{PORT}", f"{port}"))
# remove old profile folder
if self.__user_data_dir == None:
self.__user_data_dir = os.path.join(TEMP_DIR, "profile")
# start chrome
cmd = [chrome_path]
cmd.append(f"--user-data-dir={self.__user_data_dir}")
if os.path.isdir(ext_dir):
cmd.append(f"--load-extension={ext_dir}")
if self.__width * self.__height != 0:
cmd.append(f"--window-size={self.__width},{self.__height}")
if self.__block_image:
cmd.append("--blink-settings=imagesEnabled=false")
if self.__init_url != "":
cmd.append(self.__init_url)
user_agent = f"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{randint(90, 120)}.{randint(0, 100)}.{randint(0, 100)}.{randint(0, 100)} Safari/537.{randint(0, 100)} Edg/{randint(90, 120)}.0.{randint(1000, 2000)}.{randint(0, 100)}"
cmd.append(f"--user-agent={user_agent}")
self.__process = subprocess.Popen(cmd)
# accept
self.__client_unit = websocket_server.accept()
log_inf("client connected")
else:
log_err("chrome.exe not found")
def run_script(self, script: str) -> Optional[str]:
return self.__send_command("runScript", script)
def url(self) -> Optional[str]:
return self.run_script("location.href")
def goto(self, url2go: str, wait_timeout: float = 30.0, wait_elem_selector: Optional[str] = None) -> bool:
ret = False
try:
timeout = False
old_url = self.url()
if old_url == url2go:
self.run_script("location.reload()")
else:
self.run_script(f"location.href='{url2go}'")
# wait for url changed
start_tstamp = datetime.now().timestamp()
while True:
if old_url != self.url():
break
if datetime.now().timestamp() - start_tstamp > wait_timeout:
log_err("timeout")
timeout = True
break
# wait for element valid
if not timeout:
if wait_elem_selector != None:
start_tstamp = datetime.now().timestamp()
while True:
wait_elem = self.select_one(wait_elem_selector)
if wait_elem != None:
break
if datetime.now().timestamp() - start_tstamp > wait_timeout:
log_err("timeout")
timeout = True
break
if not timeout:
ret = True
except:
traceback.print_exc()
return ret
def cookie(self, domain: str) -> Any:
return self.__send_command("getCookie", domain)
def clear_cookie(self):
return self.__send_command(
"clearCookie",
)
def head(self) -> Optional[str]:
self.run_script("document.head")
def body(self) -> Optional[str]:
self.run_script("document.body")
def select(self, selector: str) -> list[ChromeElem]:
ret = []
jres = self.run_script(
"""
function getSelector(elm) {
if (elm.tagName === 'BODY') return 'BODY';
const names = [];
while (elm.parentElement && elm.tagName !== 'BODY') {
if (elm.id) {
names.unshift('#' + elm.getAttribute('id'));
break;
} else {
let c = 1, e = elm;
for (; e.previousElementSibling; e = e.previousElementSibling, c++);
names.unshift(elm.tagName + ':nth-child(' + c + ')');
}
elm = elm.parentElement;
}
return names.join('>');
}
var selectors = [];
var elemList = document.querySelectorAll('"""
+ selector
+ """');
for (var elem in elemList) {
if (elem == elem * 1) {
var selector = getSelector(elemList[elem]);
selectors.push(selector);
}
}
selectors;"""
)
if jres != None:
for jitem in jres:
ret.append(ChromeElem(jitem))
return ret
def select_one(self, selector: str) -> Optional[ChromeElem]:
elems = self.select(selector=selector)
if len(elems) > 0:
return elems[0]
else:
return None
def quit(self):
if self.__process != None:
self.__process.terminate()
self.__process = None
if self.__client_unit != None:
self.__client_unit.close()
self.__client_unit = None
self.__width = 0
self.__height = 0
self.__block_image = True
if __name__ == "__main__":
chrome = Chrome(init_url="https://google.com")
chrome.start()
for i in range(60):
time.sleep(1)
print(chrome.run_script("location.href"))
chrome.clear_cookie()
for i in range(300):
time.sleep(1)
print(".", end="")
chrome.quit()