-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
103 lines (83 loc) · 2.5 KB
/
convert.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
import json
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
def get_dict_from_desynced_str(str_to_decode):
with open("DesyncedJavaScriptUtils/dsconvert.js", "r") as file:
javascript_code = file.read()
doc = """
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript">
""" + javascript_code + """
</script>
<div id="resultDiv"></div>
<script type="text/javascript">
function run(){
var result= DesyncedStringToObject(\"""" + str_to_decode + """\");
document.getElementById("resultDiv").innerHTML = JSON.stringify(result, null, 4);
}
run();
</script>
</body>
</html>
"""
wd = os.getcwd()
temp_file = os.path.join(wd, "TEMP.html")
assert not os.path.isfile(temp_file)
with open(temp_file, "w") as outfile:
outfile.write(doc)
# TODO allow to configure other browsers?
os.environ['MOZ_HEADLESS'] = '1'
driver = webdriver.Firefox()
driver.get("file://"+temp_file)
elem = driver.find_element(By.ID, "resultDiv")
json_str = elem.text
driver.close()
os.remove(temp_file)
return json.loads(json_str)
def get_desynced_str_from_dict(dict_to_convert):
as_str = json.dumps(dict_to_convert)
with open("DesyncedJavaScriptUtils/dsconvert.js", "r") as file:
javascript_code = file.read()
assert "'" not in as_str
# otherwise quotation will not work correctly
doc = """
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript">
""" + javascript_code + """
</script>
<div id="resultDiv"></div>
<script type="text/javascript">
function run(){
var jsonobj;
try { jsonobj = JSON.parse('""" + as_str + """'); } catch { }
var result= ObjectToDesyncedString();
var type = (jsonobj[0] ? 'C' : 'B');
result = ObjectToDesyncedString(jsonobj, type);
document.getElementById("resultDiv").innerHTML = result;
}
run();
</script>
</body>
</html>
"""
wd = os.getcwd()
temp_file = os.path.join(wd, "TEMP.html")
assert not os.path.isfile(temp_file)
with open(temp_file, "w") as outfile:
outfile.write(doc)
# TODO allow to configure other browsers?
os.environ['MOZ_HEADLESS'] = '1'
driver = webdriver.Firefox()
driver.get("file://"+temp_file)
elem = driver.find_element(By.ID, "resultDiv")
result_str = elem.text
driver.close()
os.remove(temp_file)
return result_str