-
Notifications
You must be signed in to change notification settings - Fork 64
/
处理js代码中定位function代码的部分.py
82 lines (71 loc) · 2.94 KB
/
处理js代码中定位function代码的部分.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
# 直接执行即可测试
# 功能:快速定位复杂 js 代码中的函数部分的字符串
# 因为代码很短,复制使用即可,按照测试的写法写即可。
# 开发于 python3 ,是对已经存在库的内部函数的挂钩式处理后增加了自己想要的功能
# 依赖, pip install pyjsparser
# 如果你安装过了 js2py ,那么这个库就已经安装了。
import pyjsparser
class PyJsHooker:
Identifier = 3 # pyjsparser.pyjsparserdata.Token.Identifier
func_stack = []
func_count = {}
func_local = []
def _expect_hook(self, value):
index = len(PyJsHooker.func_stack)
if self.lookahead['value'] == '{':
if index not in PyJsHooker.func_count:
PyJsHooker.func_count[index] = 1
else:
PyJsHooker.func_count[index] += 1
if self.lookahead['value'] == '}':
PyJsHooker.func_count[index] -= 1
if index in PyJsHooker.func_count and PyJsHooker.func_count[index] == 0 and self.lookahead['value'] == '}':
if PyJsHooker.func_stack:
(name, start), end = PyJsHooker.func_stack.pop(), self.lookahead
name = name['value'] if name['type'] == PyJsHooker.Identifier else '[Anonymous function]'
PyJsHooker.func_local.append((name, start['start'], end['end']))
return PyJsHooker._bak_expect(self, value)
def _expectKeyword_hook(self, w):
orFunc = self.lookahead
isFunc = True if self.lookahead['value'] == 'function' else False
r = PyJsHooker._bak_expectKeyword(self, w)
crFunc = self.lookahead
if isFunc:
name = crFunc if crFunc['type'] == PyJsHooker.Identifier else None
PyJsHooker.func_stack.append([crFunc, orFunc])
return r
_bak_expect = pyjsparser.PyJsParser.expect
_bak_expectKeyword = pyjsparser.PyJsParser.expectKeyword
pyjsparser.PyJsParser.expect = _expect_hook
pyjsparser.PyJsParser.expectKeyword = _expectKeyword_hook
def __init__(self):
self.parser = pyjsparser.PyJsParser()
def parse(self, script):
PyJsHooker.func_stack = []
PyJsHooker.func_count = {}
PyJsHooker.func_local = []
self.parser.parse(script)
return PyJsHooker.func_local
if __name__ == '__main__':
script = '''// test_code
function func(a,b){
function ffff(){
var sadf = "12312{31}23";
var fffasdf = "123123123", aaa;
var qqq = {"123123":123123};
return 133;
}
(function(){
console.log(ffff);
})();
return a+b;
}
var a = 123;'''
s = PyJsHooker()
for local in s.parse(script):
name, start, end = local
v = script[start:end]
print('===================================')
print('function name:{}, start:{}, end:{}'.format(name, start, end))
print('-----------------------------------')
print(v)