wujie-event-scope 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +112 -42
- package/dist/index.es.js +112 -42
- package/dist/types/babelParse.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/babelParse.ts +58 -16
- package/src/index.ts +47 -17
package/dist/index.cjs.js
CHANGED
|
@@ -6577,6 +6577,29 @@ function getMemberExpressionPath(node) {
|
|
|
6577
6577
|
: property?.name ?? property?.value ?? "";
|
|
6578
6578
|
return `${objectName}.${propertyName}`;
|
|
6579
6579
|
}
|
|
6580
|
+
function getCallExpression(node, args, results) {
|
|
6581
|
+
if (node.type === "Identifier") {
|
|
6582
|
+
results.add({
|
|
6583
|
+
ast: node.name,
|
|
6584
|
+
start: node.start,
|
|
6585
|
+
end: node.end,
|
|
6586
|
+
});
|
|
6587
|
+
if (Array.isArray(args) && args.length > 0) {
|
|
6588
|
+
args.forEach((node) => {
|
|
6589
|
+
getCallExpression(node, node.arguments, results);
|
|
6590
|
+
});
|
|
6591
|
+
}
|
|
6592
|
+
}
|
|
6593
|
+
else if (node.type === 'MemberExpression') {
|
|
6594
|
+
const memberName = getMemberExpressionPath(node);
|
|
6595
|
+
results.add({
|
|
6596
|
+
ast: memberName,
|
|
6597
|
+
start: node.start,
|
|
6598
|
+
end: node.end,
|
|
6599
|
+
});
|
|
6600
|
+
}
|
|
6601
|
+
return results;
|
|
6602
|
+
}
|
|
6580
6603
|
function findIdentifiers(code) {
|
|
6581
6604
|
const results = new Set();
|
|
6582
6605
|
const ast = parse(code, {
|
|
@@ -6590,21 +6613,40 @@ function findIdentifiers(code) {
|
|
|
6590
6613
|
const callee = node.callee;
|
|
6591
6614
|
if (!callee)
|
|
6592
6615
|
return;
|
|
6593
|
-
|
|
6594
|
-
|
|
6595
|
-
|
|
6596
|
-
|
|
6597
|
-
|
|
6598
|
-
|
|
6599
|
-
}
|
|
6600
|
-
if (
|
|
6601
|
-
|
|
6602
|
-
|
|
6603
|
-
|
|
6604
|
-
|
|
6605
|
-
|
|
6606
|
-
|
|
6607
|
-
}
|
|
6616
|
+
getCallExpression(callee, node.arguments, results);
|
|
6617
|
+
// if (callee.type === "Identifier") {
|
|
6618
|
+
// results.add({
|
|
6619
|
+
// ast: callee.name,
|
|
6620
|
+
// start: callee.start,
|
|
6621
|
+
// end: callee.end,
|
|
6622
|
+
// });
|
|
6623
|
+
// if (Array.isArray(node.arguments) && node.arguments.length > 0) {
|
|
6624
|
+
// node.arguments.forEach((node: any) => {
|
|
6625
|
+
// if (node.type === 'Identifier') {
|
|
6626
|
+
// results.add({
|
|
6627
|
+
// ast: node.name,
|
|
6628
|
+
// start: node.start,
|
|
6629
|
+
// end: node.end,
|
|
6630
|
+
// });
|
|
6631
|
+
// } else if (node.type === 'MemberExpression') {
|
|
6632
|
+
// const memberName = getMemberExpressionPath(callee);
|
|
6633
|
+
// results.add({
|
|
6634
|
+
// ast: memberName,
|
|
6635
|
+
// start: node.start,
|
|
6636
|
+
// end: node.end,
|
|
6637
|
+
// });
|
|
6638
|
+
// }
|
|
6639
|
+
// })
|
|
6640
|
+
// }
|
|
6641
|
+
// }
|
|
6642
|
+
// if (callee.type === "MemberExpression") {
|
|
6643
|
+
// const memberName = getMemberExpressionPath(callee);
|
|
6644
|
+
// results.add({
|
|
6645
|
+
// ast: memberName,
|
|
6646
|
+
// start: callee.start,
|
|
6647
|
+
// end: callee.end,
|
|
6648
|
+
// });
|
|
6649
|
+
// }
|
|
6608
6650
|
},
|
|
6609
6651
|
AssignmentExpression(node) {
|
|
6610
6652
|
const left = node.left;
|
|
@@ -6627,36 +6669,64 @@ function findIdentifiers(code) {
|
|
|
6627
6669
|
}
|
|
6628
6670
|
},
|
|
6629
6671
|
});
|
|
6630
|
-
return Array.from(results);
|
|
6672
|
+
return Array.from(results).sort((a, b) => a.start - b.start);
|
|
6631
6673
|
}
|
|
6632
6674
|
|
|
6633
|
-
function
|
|
6634
|
-
const
|
|
6635
|
-
|
|
6636
|
-
|
|
6637
|
-
|
|
6638
|
-
const
|
|
6639
|
-
|
|
6640
|
-
|
|
6641
|
-
|
|
6642
|
-
const { ast, start, end } = astInfo;
|
|
6643
|
-
if (!ast.includes('window')) {
|
|
6644
|
-
replaceHTMLClickEventStr += `${originHTMLClickEventStr.slice(lastSliceEnd, start)}window.${ast}`;
|
|
6645
|
-
lastSliceEnd = end;
|
|
6646
|
-
}
|
|
6647
|
-
});
|
|
6648
|
-
replaceHTMLClickEventStr += `${originHTMLClickEventStr.slice(lastSliceEnd)}`;
|
|
6649
|
-
console.log("====事件函数处理后字符", replaceHTMLClickEventStr);
|
|
6650
|
-
const fnBodyStr = replaceHTMLClickEventStr.match(fnBody)?.[1];
|
|
6651
|
-
console.log("====匹配后字符", fnBodyStr);
|
|
6652
|
-
if (fnBodyStr) {
|
|
6653
|
-
element.onclick = (function (window) {
|
|
6654
|
-
return function (event) {
|
|
6655
|
-
const fn = new Function(fnBodyStr);
|
|
6656
|
-
fn.call(this);
|
|
6657
|
-
};
|
|
6658
|
-
})();
|
|
6675
|
+
function dealAttributesOCPatch(targetString) {
|
|
6676
|
+
const targetAttrLists = findIdentifiers(targetString);
|
|
6677
|
+
let lastSliceEnd = 0;
|
|
6678
|
+
let replaceAttributeEventStr = '';
|
|
6679
|
+
targetAttrLists.forEach(astInfo => {
|
|
6680
|
+
const { ast, start, end } = astInfo;
|
|
6681
|
+
if (!ast.includes('window')) {
|
|
6682
|
+
replaceAttributeEventStr += `${targetString.slice(lastSliceEnd, start)}window.${ast}`;
|
|
6683
|
+
lastSliceEnd = end;
|
|
6659
6684
|
}
|
|
6685
|
+
});
|
|
6686
|
+
replaceAttributeEventStr += `${targetString.slice(lastSliceEnd)}`;
|
|
6687
|
+
console.log("====事件函数处理后字符", replaceAttributeEventStr);
|
|
6688
|
+
return replaceAttributeEventStr;
|
|
6689
|
+
}
|
|
6690
|
+
function dealHTMLOCPatch(targetString) {
|
|
6691
|
+
console.log("====事件函数原字符", targetString);
|
|
6692
|
+
const targetAttrLists = findIdentifiers(targetString);
|
|
6693
|
+
let lastSliceEnd = 0;
|
|
6694
|
+
let replaceHTMLClickEventStr = '';
|
|
6695
|
+
targetAttrLists.forEach(astInfo => {
|
|
6696
|
+
const { ast, start, end } = astInfo;
|
|
6697
|
+
if (!ast.includes('window')) {
|
|
6698
|
+
replaceHTMLClickEventStr += `${targetString.slice(lastSliceEnd, start)}window.${ast}`;
|
|
6699
|
+
lastSliceEnd = end;
|
|
6700
|
+
}
|
|
6701
|
+
});
|
|
6702
|
+
replaceHTMLClickEventStr += `${targetString.slice(lastSliceEnd)}`;
|
|
6703
|
+
console.log("====事件函数处理后字符", replaceHTMLClickEventStr);
|
|
6704
|
+
const fnBodyStr = replaceHTMLClickEventStr.match(fnBody)?.[1];
|
|
6705
|
+
return fnBodyStr || '';
|
|
6706
|
+
}
|
|
6707
|
+
function patchElementHook(element) {
|
|
6708
|
+
const targetHaveGetAttribute = typeof element.getAttribute === 'function';
|
|
6709
|
+
const originHTMLClickEvent = element.onclick;
|
|
6710
|
+
const originAttributeEventStr = targetHaveGetAttribute ? element.getAttribute('onclick') : '';
|
|
6711
|
+
if (targetHaveGetAttribute && element.getAttribute('data-test-scope')) {
|
|
6712
|
+
// @ts-ignore
|
|
6713
|
+
console.log('===子应用window', window.__WUJIE.proxy);
|
|
6714
|
+
console.log('===HTML事件绑定', originHTMLClickEvent);
|
|
6715
|
+
console.log('===attribute获取事件', originAttributeEventStr);
|
|
6716
|
+
}
|
|
6717
|
+
let result = '';
|
|
6718
|
+
if (originAttributeEventStr) {
|
|
6719
|
+
result = dealAttributesOCPatch(originAttributeEventStr);
|
|
6720
|
+
}
|
|
6721
|
+
else if (typeof originHTMLClickEvent === 'function') {
|
|
6722
|
+
result = dealHTMLOCPatch(originHTMLClickEvent.toString());
|
|
6723
|
+
}
|
|
6724
|
+
if (result) {
|
|
6725
|
+
console.log("====执行字符串", result);
|
|
6726
|
+
element.setAttribute('onclick', `(function(window) {
|
|
6727
|
+
${result}
|
|
6728
|
+
})(window.__WUJIE.proxy)`);
|
|
6729
|
+
console.log("===替换之后的结果", element.getAttribute('onclick'));
|
|
6660
6730
|
}
|
|
6661
6731
|
}
|
|
6662
6732
|
var index = {
|
package/dist/index.es.js
CHANGED
|
@@ -6575,6 +6575,29 @@ function getMemberExpressionPath(node) {
|
|
|
6575
6575
|
: property?.name ?? property?.value ?? "";
|
|
6576
6576
|
return `${objectName}.${propertyName}`;
|
|
6577
6577
|
}
|
|
6578
|
+
function getCallExpression(node, args, results) {
|
|
6579
|
+
if (node.type === "Identifier") {
|
|
6580
|
+
results.add({
|
|
6581
|
+
ast: node.name,
|
|
6582
|
+
start: node.start,
|
|
6583
|
+
end: node.end,
|
|
6584
|
+
});
|
|
6585
|
+
if (Array.isArray(args) && args.length > 0) {
|
|
6586
|
+
args.forEach((node) => {
|
|
6587
|
+
getCallExpression(node, node.arguments, results);
|
|
6588
|
+
});
|
|
6589
|
+
}
|
|
6590
|
+
}
|
|
6591
|
+
else if (node.type === 'MemberExpression') {
|
|
6592
|
+
const memberName = getMemberExpressionPath(node);
|
|
6593
|
+
results.add({
|
|
6594
|
+
ast: memberName,
|
|
6595
|
+
start: node.start,
|
|
6596
|
+
end: node.end,
|
|
6597
|
+
});
|
|
6598
|
+
}
|
|
6599
|
+
return results;
|
|
6600
|
+
}
|
|
6578
6601
|
function findIdentifiers(code) {
|
|
6579
6602
|
const results = new Set();
|
|
6580
6603
|
const ast = parse(code, {
|
|
@@ -6588,21 +6611,40 @@ function findIdentifiers(code) {
|
|
|
6588
6611
|
const callee = node.callee;
|
|
6589
6612
|
if (!callee)
|
|
6590
6613
|
return;
|
|
6591
|
-
|
|
6592
|
-
|
|
6593
|
-
|
|
6594
|
-
|
|
6595
|
-
|
|
6596
|
-
|
|
6597
|
-
}
|
|
6598
|
-
if (
|
|
6599
|
-
|
|
6600
|
-
|
|
6601
|
-
|
|
6602
|
-
|
|
6603
|
-
|
|
6604
|
-
|
|
6605
|
-
}
|
|
6614
|
+
getCallExpression(callee, node.arguments, results);
|
|
6615
|
+
// if (callee.type === "Identifier") {
|
|
6616
|
+
// results.add({
|
|
6617
|
+
// ast: callee.name,
|
|
6618
|
+
// start: callee.start,
|
|
6619
|
+
// end: callee.end,
|
|
6620
|
+
// });
|
|
6621
|
+
// if (Array.isArray(node.arguments) && node.arguments.length > 0) {
|
|
6622
|
+
// node.arguments.forEach((node: any) => {
|
|
6623
|
+
// if (node.type === 'Identifier') {
|
|
6624
|
+
// results.add({
|
|
6625
|
+
// ast: node.name,
|
|
6626
|
+
// start: node.start,
|
|
6627
|
+
// end: node.end,
|
|
6628
|
+
// });
|
|
6629
|
+
// } else if (node.type === 'MemberExpression') {
|
|
6630
|
+
// const memberName = getMemberExpressionPath(callee);
|
|
6631
|
+
// results.add({
|
|
6632
|
+
// ast: memberName,
|
|
6633
|
+
// start: node.start,
|
|
6634
|
+
// end: node.end,
|
|
6635
|
+
// });
|
|
6636
|
+
// }
|
|
6637
|
+
// })
|
|
6638
|
+
// }
|
|
6639
|
+
// }
|
|
6640
|
+
// if (callee.type === "MemberExpression") {
|
|
6641
|
+
// const memberName = getMemberExpressionPath(callee);
|
|
6642
|
+
// results.add({
|
|
6643
|
+
// ast: memberName,
|
|
6644
|
+
// start: callee.start,
|
|
6645
|
+
// end: callee.end,
|
|
6646
|
+
// });
|
|
6647
|
+
// }
|
|
6606
6648
|
},
|
|
6607
6649
|
AssignmentExpression(node) {
|
|
6608
6650
|
const left = node.left;
|
|
@@ -6625,36 +6667,64 @@ function findIdentifiers(code) {
|
|
|
6625
6667
|
}
|
|
6626
6668
|
},
|
|
6627
6669
|
});
|
|
6628
|
-
return Array.from(results);
|
|
6670
|
+
return Array.from(results).sort((a, b) => a.start - b.start);
|
|
6629
6671
|
}
|
|
6630
6672
|
|
|
6631
|
-
function
|
|
6632
|
-
const
|
|
6633
|
-
|
|
6634
|
-
|
|
6635
|
-
|
|
6636
|
-
const
|
|
6637
|
-
|
|
6638
|
-
|
|
6639
|
-
|
|
6640
|
-
const { ast, start, end } = astInfo;
|
|
6641
|
-
if (!ast.includes('window')) {
|
|
6642
|
-
replaceHTMLClickEventStr += `${originHTMLClickEventStr.slice(lastSliceEnd, start)}window.${ast}`;
|
|
6643
|
-
lastSliceEnd = end;
|
|
6644
|
-
}
|
|
6645
|
-
});
|
|
6646
|
-
replaceHTMLClickEventStr += `${originHTMLClickEventStr.slice(lastSliceEnd)}`;
|
|
6647
|
-
console.log("====事件函数处理后字符", replaceHTMLClickEventStr);
|
|
6648
|
-
const fnBodyStr = replaceHTMLClickEventStr.match(fnBody)?.[1];
|
|
6649
|
-
console.log("====匹配后字符", fnBodyStr);
|
|
6650
|
-
if (fnBodyStr) {
|
|
6651
|
-
element.onclick = (function (window) {
|
|
6652
|
-
return function (event) {
|
|
6653
|
-
const fn = new Function(fnBodyStr);
|
|
6654
|
-
fn.call(this);
|
|
6655
|
-
};
|
|
6656
|
-
})();
|
|
6673
|
+
function dealAttributesOCPatch(targetString) {
|
|
6674
|
+
const targetAttrLists = findIdentifiers(targetString);
|
|
6675
|
+
let lastSliceEnd = 0;
|
|
6676
|
+
let replaceAttributeEventStr = '';
|
|
6677
|
+
targetAttrLists.forEach(astInfo => {
|
|
6678
|
+
const { ast, start, end } = astInfo;
|
|
6679
|
+
if (!ast.includes('window')) {
|
|
6680
|
+
replaceAttributeEventStr += `${targetString.slice(lastSliceEnd, start)}window.${ast}`;
|
|
6681
|
+
lastSliceEnd = end;
|
|
6657
6682
|
}
|
|
6683
|
+
});
|
|
6684
|
+
replaceAttributeEventStr += `${targetString.slice(lastSliceEnd)}`;
|
|
6685
|
+
console.log("====事件函数处理后字符", replaceAttributeEventStr);
|
|
6686
|
+
return replaceAttributeEventStr;
|
|
6687
|
+
}
|
|
6688
|
+
function dealHTMLOCPatch(targetString) {
|
|
6689
|
+
console.log("====事件函数原字符", targetString);
|
|
6690
|
+
const targetAttrLists = findIdentifiers(targetString);
|
|
6691
|
+
let lastSliceEnd = 0;
|
|
6692
|
+
let replaceHTMLClickEventStr = '';
|
|
6693
|
+
targetAttrLists.forEach(astInfo => {
|
|
6694
|
+
const { ast, start, end } = astInfo;
|
|
6695
|
+
if (!ast.includes('window')) {
|
|
6696
|
+
replaceHTMLClickEventStr += `${targetString.slice(lastSliceEnd, start)}window.${ast}`;
|
|
6697
|
+
lastSliceEnd = end;
|
|
6698
|
+
}
|
|
6699
|
+
});
|
|
6700
|
+
replaceHTMLClickEventStr += `${targetString.slice(lastSliceEnd)}`;
|
|
6701
|
+
console.log("====事件函数处理后字符", replaceHTMLClickEventStr);
|
|
6702
|
+
const fnBodyStr = replaceHTMLClickEventStr.match(fnBody)?.[1];
|
|
6703
|
+
return fnBodyStr || '';
|
|
6704
|
+
}
|
|
6705
|
+
function patchElementHook(element) {
|
|
6706
|
+
const targetHaveGetAttribute = typeof element.getAttribute === 'function';
|
|
6707
|
+
const originHTMLClickEvent = element.onclick;
|
|
6708
|
+
const originAttributeEventStr = targetHaveGetAttribute ? element.getAttribute('onclick') : '';
|
|
6709
|
+
if (targetHaveGetAttribute && element.getAttribute('data-test-scope')) {
|
|
6710
|
+
// @ts-ignore
|
|
6711
|
+
console.log('===子应用window', window.__WUJIE.proxy);
|
|
6712
|
+
console.log('===HTML事件绑定', originHTMLClickEvent);
|
|
6713
|
+
console.log('===attribute获取事件', originAttributeEventStr);
|
|
6714
|
+
}
|
|
6715
|
+
let result = '';
|
|
6716
|
+
if (originAttributeEventStr) {
|
|
6717
|
+
result = dealAttributesOCPatch(originAttributeEventStr);
|
|
6718
|
+
}
|
|
6719
|
+
else if (typeof originHTMLClickEvent === 'function') {
|
|
6720
|
+
result = dealHTMLOCPatch(originHTMLClickEvent.toString());
|
|
6721
|
+
}
|
|
6722
|
+
if (result) {
|
|
6723
|
+
console.log("====执行字符串", result);
|
|
6724
|
+
element.setAttribute('onclick', `(function(window) {
|
|
6725
|
+
${result}
|
|
6726
|
+
})(window.__WUJIE.proxy)`);
|
|
6727
|
+
console.log("===替换之后的结果", element.getAttribute('onclick'));
|
|
6658
6728
|
}
|
|
6659
6729
|
}
|
|
6660
6730
|
var index = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"babelParse.d.ts","sourceRoot":"","sources":["../../src/babelParse.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,OAAO;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;
|
|
1
|
+
{"version":3,"file":"babelParse.d.ts","sourceRoot":"","sources":["../../src/babelParse.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,OAAO;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AA0CD,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,aA6E3C"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAqCA,iBAAS,gBAAgB,CAAC,OAAO,EAAE,WAAW,QAuB7C;;;;AAED,wBAEE"}
|
package/package.json
CHANGED
package/src/babelParse.ts
CHANGED
|
@@ -24,6 +24,29 @@ function getMemberExpressionPath(node: any): string {
|
|
|
24
24
|
return `${objectName}.${propertyName}`;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
function getCallExpression(node: any, args: any, results: Set<AstInfo>) {
|
|
28
|
+
if (node.type === "Identifier") {
|
|
29
|
+
results.add({
|
|
30
|
+
ast: node.name,
|
|
31
|
+
start: node.start,
|
|
32
|
+
end: node.end,
|
|
33
|
+
});
|
|
34
|
+
if (Array.isArray(args) && args.length > 0) {
|
|
35
|
+
args.forEach((node: any) => {
|
|
36
|
+
getCallExpression(node, node.arguments ,results)
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
} else if (node.type === 'MemberExpression') {
|
|
40
|
+
const memberName = getMemberExpressionPath(node);
|
|
41
|
+
results.add({
|
|
42
|
+
ast: memberName,
|
|
43
|
+
start: node.start,
|
|
44
|
+
end: node.end,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return results
|
|
48
|
+
}
|
|
49
|
+
|
|
27
50
|
export function findIdentifiers(code: string) {
|
|
28
51
|
const results = new Set<AstInfo>();
|
|
29
52
|
|
|
@@ -40,22 +63,41 @@ export function findIdentifiers(code: string) {
|
|
|
40
63
|
|
|
41
64
|
if (!callee) return;
|
|
42
65
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
66
|
+
getCallExpression(callee, node.arguments, results)
|
|
67
|
+
// if (callee.type === "Identifier") {
|
|
68
|
+
// results.add({
|
|
69
|
+
// ast: callee.name,
|
|
70
|
+
// start: callee.start,
|
|
71
|
+
// end: callee.end,
|
|
72
|
+
// });
|
|
73
|
+
// if (Array.isArray(node.arguments) && node.arguments.length > 0) {
|
|
74
|
+
// node.arguments.forEach((node: any) => {
|
|
75
|
+
// if (node.type === 'Identifier') {
|
|
76
|
+
// results.add({
|
|
77
|
+
// ast: node.name,
|
|
78
|
+
// start: node.start,
|
|
79
|
+
// end: node.end,
|
|
80
|
+
// });
|
|
81
|
+
// } else if (node.type === 'MemberExpression') {
|
|
82
|
+
// const memberName = getMemberExpressionPath(callee);
|
|
83
|
+
// results.add({
|
|
84
|
+
// ast: memberName,
|
|
85
|
+
// start: node.start,
|
|
86
|
+
// end: node.end,
|
|
87
|
+
// });
|
|
88
|
+
// }
|
|
89
|
+
// })
|
|
90
|
+
// }
|
|
91
|
+
// }
|
|
50
92
|
|
|
51
|
-
if (callee.type === "MemberExpression") {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
93
|
+
// if (callee.type === "MemberExpression") {
|
|
94
|
+
// const memberName = getMemberExpressionPath(callee);
|
|
95
|
+
// results.add({
|
|
96
|
+
// ast: memberName,
|
|
97
|
+
// start: callee.start,
|
|
98
|
+
// end: callee.end,
|
|
99
|
+
// });
|
|
100
|
+
// }
|
|
59
101
|
},
|
|
60
102
|
AssignmentExpression(node: any) {
|
|
61
103
|
const left = node.left;
|
|
@@ -81,5 +123,5 @@ export function findIdentifiers(code: string) {
|
|
|
81
123
|
},
|
|
82
124
|
});
|
|
83
125
|
|
|
84
|
-
return Array.from(results);
|
|
126
|
+
return Array.from(results).sort((a, b) => a.start - b.start);
|
|
85
127
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,32 +1,62 @@
|
|
|
1
1
|
import { fnBody } from "./regx"
|
|
2
2
|
import { findIdentifiers } from "./babelParse"
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
|
|
4
|
+
function dealAttributesOCPatch(targetString: string) {
|
|
5
|
+
const targetAttrLists = findIdentifiers(targetString);
|
|
6
|
+
let lastSliceEnd = 0;
|
|
7
|
+
let replaceAttributeEventStr: string = '';
|
|
8
|
+
targetAttrLists.forEach(astInfo => {
|
|
9
|
+
const { ast, start, end } = astInfo
|
|
10
|
+
if (!ast.includes('window')) {
|
|
11
|
+
replaceAttributeEventStr += `${targetString.slice(lastSliceEnd, start)}window.${ast}`
|
|
12
|
+
lastSliceEnd = end
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
replaceAttributeEventStr += `${targetString.slice(lastSliceEnd)}`
|
|
16
|
+
console.log("====事件函数处理后字符", replaceAttributeEventStr)
|
|
17
|
+
return replaceAttributeEventStr
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function dealHTMLOCPatch(targetString: string) {
|
|
21
|
+
console.log("====事件函数原字符", targetString)
|
|
22
|
+
const targetAttrLists = findIdentifiers(targetString);
|
|
9
23
|
let lastSliceEnd = 0;
|
|
10
24
|
let replaceHTMLClickEventStr: string = '';
|
|
11
25
|
targetAttrLists.forEach(astInfo => {
|
|
12
26
|
const { ast, start, end } = astInfo
|
|
13
27
|
if (!ast.includes('window')) {
|
|
14
|
-
replaceHTMLClickEventStr += `${
|
|
28
|
+
replaceHTMLClickEventStr += `${targetString.slice(lastSliceEnd, start)}window.${ast}`
|
|
15
29
|
lastSliceEnd = end
|
|
16
30
|
}
|
|
17
31
|
})
|
|
18
|
-
replaceHTMLClickEventStr += `${
|
|
32
|
+
replaceHTMLClickEventStr += `${targetString.slice(lastSliceEnd)}`
|
|
19
33
|
console.log("====事件函数处理后字符", replaceHTMLClickEventStr)
|
|
20
34
|
const fnBodyStr = replaceHTMLClickEventStr.match(fnBody)?.[1];
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
return fnBodyStr || ''
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function patchElementHook(element: HTMLElement) {
|
|
39
|
+
const targetHaveGetAttribute = typeof element.getAttribute === 'function'
|
|
40
|
+
const originHTMLClickEvent = element.onclick;
|
|
41
|
+
const originAttributeEventStr = targetHaveGetAttribute ? element.getAttribute('onclick') : ''
|
|
42
|
+
if (targetHaveGetAttribute && element.getAttribute('data-test-scope')) {
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
console.log('===子应用window', window.__WUJIE.proxy)
|
|
45
|
+
console.log('===HTML事件绑定', originHTMLClickEvent)
|
|
46
|
+
console.log('===attribute获取事件', originAttributeEventStr)
|
|
47
|
+
}
|
|
48
|
+
let result = '';
|
|
49
|
+
if (originAttributeEventStr) {
|
|
50
|
+
result = dealAttributesOCPatch(originAttributeEventStr)
|
|
51
|
+
} else if (typeof originHTMLClickEvent === 'function') {
|
|
52
|
+
result = dealHTMLOCPatch(originHTMLClickEvent.toString())
|
|
53
|
+
}
|
|
54
|
+
if (result) {
|
|
55
|
+
console.log("====执行字符串", result)
|
|
56
|
+
element.setAttribute('onclick', `(function(window) {
|
|
57
|
+
${result}
|
|
58
|
+
})(window.__WUJIE.proxy)`)
|
|
59
|
+
console.log("===替换之后的结果", element.getAttribute('onclick'))
|
|
30
60
|
}
|
|
31
61
|
}
|
|
32
62
|
|