-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs.js
92 lines (84 loc) · 3.04 KB
/
js.js
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
var Acorn = require('acorn')
var Walk = require('acorn-walk')
var Escodegen = require('escodegen')
function InitVariableRewrites(node) {
node.declarations = node.declarations.map(declaration => {
//console.log(declaration)
if (declaration.init) {
switch(declaration.init.type) {
case 'Literal':
return declaration;
break;
case "MemberExpression":
var exp = declaration.init.object
exp.type = 'CallExpression'
exp.callee = {type: 'MemberExpression', object: {type: 'Identifier', name: '$Rhodium'}, property: {type: 'Identifier', name: 'get'}}
exp.arguments = [{type: 'MemberExpression', object: {type: 'Identifier', name: exp.object.name||'amongus'}, property: {type: 'Identifier', name: exp.object.property?exp.object.property.name:'sus'}}]
if (!exp.object.property) delete exp.arguments[0].property
declaration.init.object = exp
return declaration;
break;
}
} else {
}
})
return node
}
function InitCalculatorRewrites(node) {
if (node.expression) {
var exp = node.expression
console.log(exp)
if (exp.left&&exp.left.object&&exp.left.object.object&&(exp.left.object.object.name=='window'||exp.left.object.object.name=='self'||exp.left.object.object.name=='this')) {
exp = exp.left
var obj = exp.object
exp.object = {}
var dxp = exp
exp = exp.object
exp.type = 'CallExpression'
dxp.property = {type: 'Identifier', name: dxp.object.name}
exp.callee = {type:'MemberExpression', object: {type: 'Identifier', name: '$Rhodium'}, property: {name: 'get', type: 'Identifier'}}
exp.arguments = [{type: 'MemberExpression', object: {type: 'Identifier', name: obj.object.name}, property: {name: obj.property.name, type: 'Identifier'}}]
dxp.object = exp
node.expression.left = dxp
}
if (exp.right&&exp.right.object&&exp.right.object.name=='window') {
}
}
return node
}
function InitExpressionRewrites(node) {
if (node.expression) {
var exp = node.expression.object
if (node.type=='ExpressionStatement') return InitCalculatorRewrites(node)
if (exp.object&&(exp.object.name!=='window'||exp.object.name!=='document')) return node
var newProxyObject = {
type: 'MemberExpression',
object: { type: 'Identifier', name: '$Rhodium' },
property: { type: 'Identifier', name: 'get' }
}
exp.callee = newProxyObject
exp.arguments = []
delete exp.object
delete exp.property
delete exp.start
delete exp.end
exp.type = 'CallExpression'
node.expression.object = exp
}
return node
}
function rewriteJS(js) {
js = js.toString()
const ast = Acorn.parse(js, {ecmaVersion: 2020})
Walk.simple(ast, {
ExpressionStatement(node) {
node = InitExpressionRewrites(node)
},
VariableDeclaration(node) {
node = InitVariableRewrites(node)
}
})
return Escodegen.generate(ast, {comment: true})
}
console.log((rewriteJS('var amongus = window.location.href')))
module.exports = rewriteJS