2022-07-01 14:28:39 +04:00
|
|
|
"""Parsing and executing JavaScript code"""
|
|
|
|
|
2022-01-06 19:57:26 +04:00
|
|
|
import base64
|
2022-08-22 09:55:08 +04:00
|
|
|
import regex
|
|
|
|
import js2py
|
|
|
|
|
2022-03-17 10:24:58 +04:00
|
|
|
# Thanks to http://regex.inginf.units.it/
|
|
|
|
arrowexp = regex.compile(r'\w[^\}]*+')
|
2022-02-09 13:07:24 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
|
|
|
|
def to_ecma5_function(f: str) -> str:
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
"""Converts a ECMA6 function
|
|
|
|
to ECMA5 format (without arrow expressions)
|
|
|
|
|
|
|
|
Args:
|
|
|
|
f (str): ECMA6 function
|
2022-06-23 15:13:56 +04:00
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Returns:
|
|
|
|
ECMA5 function
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
2022-08-22 09:55:08 +04:00
|
|
|
f = regex.sub(r'/\*.+?\*/', '', f)
|
2022-06-23 15:13:56 +04:00
|
|
|
match = arrowexp.search(f)
|
|
|
|
conv = '(function(){' + match.group(0) + '})()'
|
|
|
|
return regex.sub(
|
|
|
|
r'(?:s|\(s\)) => s.split\([\'"]{2}\).reverse\(\).join\([\'"]{2}\)',
|
|
|
|
'function(s){return s.split(\'\').reverse().join(\'\')}',
|
|
|
|
conv
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def atob(s: str) -> str:
|
2022-07-01 14:28:39 +04:00
|
|
|
"""Decodes base64 string
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
s (str): Encoded data
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Decoded string
|
2022-07-01 14:28:39 +04:00
|
|
|
"""
|
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
return base64.standard_b64decode(str(s)).decode('utf-8')
|
|
|
|
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
def exec_js(f: str) -> js2py.EvalJs:
|
2022-06-23 15:13:56 +04:00
|
|
|
"""Executes a JavaScript function
|
|
|
|
|
MkDocs, Readme, Files API, Automated session saving, v2.0.1
MkDocs: sphinx docstrings rewritten to google, improved config, written the major part of how-to.
Readme: centered title + logo, added badges, features list, updated changelog.
Improved Files API, added automatical session saving and restoring to Client.
Some changes in makefile and gitignore.
License Notice now refers to all contributors.
2022-08-26 16:14:07 +04:00
|
|
|
Args:
|
|
|
|
f (str): ECMA6 function
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
JavaScript interpreter context
|
2022-06-23 15:13:56 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
ctx = js2py.EvalJs({'atob': atob})
|
|
|
|
ctx.execute('window.document = { };')
|
|
|
|
ctx.execute('window.Map = function(_i){ };')
|
|
|
|
ctx.execute('window.setTimeout = function(_f,_t){ };')
|
|
|
|
ctx.execute('window.setInterval = function(_f,_t){ };')
|
|
|
|
ctx.execute('window.encodeURIComponent = function(_s){ };')
|
|
|
|
ctx.execute(to_ecma5_function(f))
|
|
|
|
return ctx
|