Files
FOFCompiler/js/JavaScript/CompilerJavaScriptCodeReplace.js
2025-08-14 15:43:15 +08:00

84 lines
5.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//用来进行JS命令的替换编译本方法不能独立使用通常用来配合JsCodeCompiler命令
//参数1【JSCode】为整体的JS代码
//参数2【JSCodeKeyword】为需要编译的关键字
//参数3【regCompilerCode】为需要替换编译为的内容
//参数4【ifendfofdianhaobenin】【只针对命令有效】,1需要加点编译【针对普通命令】;2常规编译,不加点;3代表不需要正则,直接替换
//参数5【ReplaceType】为编译类型1代表关键字2代表类3代表命令4代表静态命令【类.xxx】5代表全局静态命令
function JsCodeReplace(JSCode, JSCodeKeyword, regCompilerCode, ifendfofdianhaobenin, ReplaceType) {
let JSCodeStr = JSCode;
JSCodeStr = JSCodeStr + "\r\nBeginEnd" + CompilerIdentificationText;//给代码结尾加上标识符,防止因为结尾代码【代码最后面没有内容】而造成编译失效
if (ifendfofdianhaobenin == 3) {
JSCodeStr = JSCodeStr.replaceAll(JSCodeKeyword, regCompilerCode)
}
if (ReplaceType == 1 || ReplaceType == 2) {
//当编译类型为关键字或者类的时候;将点号进行一次替换,替换为一段不会重复的文本
JSCodeStr = JSCodeStr.replaceAll(Comendfofdianhaobenin, CompilerIdentificationText);
}
if (ifendfofdianhaobenin == 1 || ifendfofdianhaobenin == 2) {
if (ReplaceType == 3 && ifendfofdianhaobenin == 1) {
if (JSCodeStr.includes(Comendfofdianhaobenin)) {
JSCodeStr = JSCodeStr.replace(new RegExp(Comendfofdianhaobenin, 'gm'), "-endfofdianhaobenin");
}
}
if (JSCodeStr == regCompilerCode) {//判断是否是直接一样的内容,一样就直接匹配替换
JSCodeStr = regCompilerCode;
return JSCodeStr;
}
let JSCodeStrOriginal = JSCodeStr;
if (JSCodeStrOriginal.includes(JSCodeKeyword)) {
let RegExpObj = windowJsCodeReplace(JSCodeStrOriginal, JSCodeKeyword, regCompilerCode, ifendfofdianhaobenin, ReplaceType)
if (RegExpObj["JSCodeStrArr"]) {
if (RegExpObj["JSCodeStrArr"].length >= 1) {
regCompilerCode = RegExpObj["regCompilerCode"]
let regCompilerCodeArr = [StrCodeUuid(regCompilerCode), regCompilerCode];
JSCodeStr = JSCodeStr.replace(RegExpObj["RegExp"], regCompilerCodeArr[0]);
for (let index = 0; index < RegExpObj["JSCodeStrArr"].length; index++) {
let FirstText = RegExpObj["JSCodeStrArr"][index].substring(0, 1);
JSCodeStr = JSCodeStr.replace(regCompilerCodeArr[0], FirstText + regCompilerCodeArr[1]);
}
}
}
}
}
JSCodeStr = JSCodeStr.replaceAll("\r\nBeginEnd" + CompilerIdentificationText, "");//将添加上的结尾代码删除
if (ReplaceType == 1 || ReplaceType == 2) {
//将被替换的文本还原成点号
JSCodeStr = JSCodeStr.replaceAll(CompilerIdentificationText, Comendfofdianhaobenin);
}
if (ReplaceType == 3 && ifendfofdianhaobenin == 1) {
if (JSCodeStr.includes("-endfofdianhaobenin")) {
JSCodeStr = JSCodeStr.replace(new RegExp("-endfofdianhaobenin", 'gm'), Comendfofdianhaobenin);
}
}
return JSCodeStr;
}
function windowJsCodeReplace(JSCodeStrOriginal, JSCodeKeyword, regCompilerCode, ifendfofdianhaobenin, ReplaceType) {
let JSCodeStrArr = JSCodeStrOriginal.match(new RegExp("(?=[^0-9a-zA-Z\u4e00-\u9fa5_])[^0-9a-zA-Z\u4e00-\u9fa5_]" + JSCodeKeyword + "(?=[^0-9a-zA-Z\u4e00-\u9fa5_])", "g"));
if (JSCodeStrArr == null && ifendfofdianhaobenin == 2 && ReplaceType == 4) {
if (JSCodeStrOriginal.includes("浏览器窗口" + Comendfofdianhaobenin + JSCodeKeyword)) {
JSCodeStrArr = JSCodeStrOriginal.match(new RegExp("(?=[^0-9a-zA-Z\u4e00-\u9fa5_])[^0-9a-zA-Z\u4e00-\u9fa5_]" + "浏览器窗口" + Comendfofdianhaobenin + JSCodeKeyword + "(?=[^0-9a-zA-Z\u4e00-\u9fa5_])", "g"));
if (JSCodeStrArr != null) {
return {
"JSCodeStrArr": JSCodeStrArr,
"RegExp": new RegExp("(?=[^0-9a-zA-Z\u4e00-\u9fa5_])[^0-9a-zA-Z\u4e00-\u9fa5_]" + "浏览器窗口" + Comendfofdianhaobenin + JSCodeKeyword + "(?=[^0-9a-zA-Z\u4e00-\u9fa5_])", "g"),
"regCompilerCode": "浏览器窗口" + Comendfofdianhaobenin + regCompilerCode
}
}
}
if (JSCodeStrArr == null && ifendfofdianhaobenin == 2 && ReplaceType == 4) {
if (JSCodeStrOriginal.includes("window" + Comendfofdianhaobenin + JSCodeKeyword)) {
JSCodeStrArr = JSCodeStrOriginal.match(new RegExp("(?=[^0-9a-zA-Z\u4e00-\u9fa5_])[^0-9a-zA-Z\u4e00-\u9fa5_]" + "window" + Comendfofdianhaobenin + JSCodeKeyword + "(?=[^0-9a-zA-Z\u4e00-\u9fa5_])", "g"));
return {
"JSCodeStrArr": JSCodeStrArr,
"RegExp": new RegExp("(?=[^0-9a-zA-Z\u4e00-\u9fa5_])[^0-9a-zA-Z\u4e00-\u9fa5_]" + "window" + Comendfofdianhaobenin + JSCodeKeyword + "(?=[^0-9a-zA-Z\u4e00-\u9fa5_])", "g"),
"regCompilerCode": "window" + Comendfofdianhaobenin + regCompilerCode
}
}
}
}
return {
"JSCodeStrArr": JSCodeStrArr,
"RegExp": new RegExp("(?=[^0-9a-zA-Z\u4e00-\u9fa5_])[^0-9a-zA-Z\u4e00-\u9fa5_]" + JSCodeKeyword + "(?=[^0-9a-zA-Z\u4e00-\u9fa5_])", "g"),
"regCompilerCode": regCompilerCode
};
}