-
Notifications
You must be signed in to change notification settings - Fork 0
/
rayso.py
229 lines (196 loc) · 6.77 KB
/
rayso.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
# <one line to give the program's name and a brief idea of what it does.>
# Copyright (C) <2021> <coolrc>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import platform
import unittest
import threading
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from browser import new_web_driver
# from browser import new_remote_web_driver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
class RaySo:
def __init__(self):
# self.webdriver = new_web_driver(http_proxy="127.0.0.1:7890")
self.webdriver = new_web_driver()
self.lock = threading.Lock()
def connect(self):
self.lock.acquire()
# self.webdriver.get("https://ray.so/")
#
# self.webdriver = new_remote_web_driver('http://127.0.0.1:5566/wd/hub')
self.webdriver.get("https://rayso-proxy.coolrc.workers.dev") # 反代地址
# 隐藏拖动控制 和 工具栏
self.webdriver.execute_script(
"""
let elem = document.querySelector(".drag-control-points")
elem.style.display='none';
elem = document.querySelector("section.controls")
elem.style.display='none';
elem = document.querySelector(".CodeMirror-vscrollbar")
elem.style.overflow="-moz-hidden-unscrollable"
elem = document.querySelector(".title")
elem.textContent=""
elem.style.color="#aaaaaa"
"""
)
self.lock.release()
def capture(self, content, font="Fira code", padding=26, title="", size=1):
self.lock.acquire()
try:
self.set_content(content)
self.set_font(font)
self.set_padding(padding)
self.set_title(title)
self.set_size(size)
box = self.webdriver.find_element_by_xpath('//*[@id="frame"]')
finally:
self.lock.release()
return box.screenshot_as_base64
def capture_img(self, content, font="Fira code", padding=26, title="", size=1):
self.lock.acquire()
try:
self.set_content(content)
self.set_font(font)
self.set_padding(padding)
self.set_title(title)
self.set_size(size)
box = self.webdriver.find_element_by_xpath('//*[@id="frame"]')
return box.screenshot("screenshot.png")
finally:
self.lock.release()
return False
def set_content(self, content: str):
lock = threading.Lock()
lock.acquire()
"""
填入内容
"""
ActionChains(self.webdriver).send_keys("r").perform() # 随机更换主题
# Chrome需要激活textarea才能开始输入
textarea = WebDriverWait(self.webdriver, 10).until(
ec.element_to_be_clickable(
(
By.CSS_SELECTOR,
"#frame > div.app-frame-container > div.app-frame > div.vue-codemirror.code-editor > div",
)
)
)
textarea.click()
textarea = self.webdriver.find_element_by_css_selector(
".CodeMirror > div > textarea"
)
textarea.send_keys(Keys.CONTROL + "a")
textarea.send_keys(Keys.BACKSPACE)
self.sendKeysWithEmojis(textarea, content)
textarea.send_keys(Keys.ESCAPE)
lock.release()
def set_font(self, font: str):
lock = threading.Lock()
lock.acquire()
"""
修改代码块字体
"""
self.webdriver.execute_script(
f"""
let elem = document.getElementsByClassName("CodeMirror-code");
elem[0].style.fontFamily = "{font}";
"""
)
lock.release()
def set_padding(self, padding: int):
lock = threading.Lock()
lock.acquire()
"""
修改图片padding
:param padding: int, 单位px
"""
self.webdriver.execute_script(
f"""
let elem = document.querySelector("#frame")
elem.style.padding="{padding}px"
"""
)
lock.release()
def set_title(self, title: str):
lock = threading.Lock()
lock.acquire()
"""
设置代码块标题
"""
self.webdriver.execute_script(
f"""
let elem = document.querySelector(".title")
elem.textContent="{title}"
"""
)
lock.release()
def set_size(self, size: float):
lock = threading.Lock()
lock.acquire()
"""
缩放图片
:param size: 图片缩放倍数,1-2
"""
if size < 1 or size > 2:
size = 1
self.webdriver.execute_script(
f"""
let elem = document.querySelector("#frame")
elem.style.scale = {size}
"""
)
lock.release()
# def __del__(self):
# self.quit()
def sendKeysWithEmojis(self, element, text):
script = """var
elm = arguments[0], \
txt = arguments[1];
elm.value += txt;
elm.dispatchEvent(new
Event('keydown', {bubbles: true}));
elm.dispatchEvent(new
Event('keypress', {bubbles: true}));
elm.dispatchEvent(new
Event('input', {bubbles: true}));
elm.dispatchEvent(new
Event('keyup', {bubbles: true}));"""
self.webdriver.execute_script(script, element, text)
def quit(self):
try:
self.webdriver.quit()
except Exception as e:
print(e)
class TestRaySo(unittest.TestCase):
def test_capture(self):
rayso = RaySo()
rayso.connect()
base64 = rayso.capture("let a = 5")
# print(base64)
self.assertTrue(len(base64) > 1000)
def test_del(self):
RaySo()
sys = platform.system()
if sys != "Windows":
command = "taskkill /F /T /IM "
command = command + "firefox.exe"
os.system(command) # nosec
else:
command = "pkill gecko && pkill firefox"
os.system(command) # nosec