2022-03-17 10:24:58 +04:00
|
|
|
import regex
|
2022-01-06 19:57:26 +04:00
|
|
|
import base64
|
2022-02-09 13:07:24 +04:00
|
|
|
import js2py
|
2022-03-18 18:38:36 +04:00
|
|
|
from typing import Any
|
2022-02-09 13:07:24 +04:00
|
|
|
|
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
|
|
|
|
|
|
|
def to_ecma5_function(f:str) -> str:
|
2022-06-17 12:30:58 +04:00
|
|
|
|
|
|
|
"""Converts a ECMA6 function to ECMA5 format (without arrow expressions)
|
|
|
|
|
|
|
|
:param f: ECMA6 function
|
|
|
|
:type f: str
|
|
|
|
:return: ECMA5 function
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
|
2022-03-17 10:24:58 +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
|
|
|
|
)
|
2022-01-06 19:57:26 +04:00
|
|
|
|
2022-03-17 10:24:58 +04:00
|
|
|
def atob(s:str) -> str:
|
2022-01-06 19:57:26 +04:00
|
|
|
return base64.standard_b64decode(str(s)).decode('utf-8')
|
|
|
|
|
2022-02-09 13:07:24 +04:00
|
|
|
def exec(f:str) -> Any:
|
2022-06-17 12:30:58 +04:00
|
|
|
|
|
|
|
"""Executes a JavaScript function
|
|
|
|
|
|
|
|
:param f: ECMA6 function
|
|
|
|
:type f: str
|
|
|
|
:return: JavaScript interpreter context
|
|
|
|
:rtype: Any
|
|
|
|
"""
|
|
|
|
|
2022-01-06 19:57:26 +04:00
|
|
|
ctx = js2py.EvalJs({'atob': atob})
|
2022-05-13 17:44:00 +04:00
|
|
|
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){ };')
|
2022-01-06 19:57:26 +04:00
|
|
|
ctx.execute(to_ecma5_function(f))
|
|
|
|
return ctx
|