优化js代码注入
This commit is contained in:
1 parent
d15e8185bb
commit
6ba9ed637b
3 files changed
+176
-64
No files matched your search
+58
-31
@@ -1,36 +1,63 @@
|
||||
|
||||
let wpsInstance = null;
|
||||
|
||||
function InvokeFromXbot(param) {
|
||||
let jsonObj = (typeof (param) == 'string' ? JSON.parse(param) : param)
|
||||
let func = jsonObj.Func;
|
||||
let args = jsonObj.Args;
|
||||
args = JSON.parse(args);
|
||||
console.log(func);
|
||||
console.log(args);
|
||||
switch (func) {
|
||||
case "getDocumentName": {
|
||||
const doc = wps.WpsApplication().ActiveDocument;
|
||||
alert(doc.Name);
|
||||
if (!doc) {
|
||||
return { currentDoc: null }
|
||||
}
|
||||
return { currentDoc: doc.Name }
|
||||
}
|
||||
|
||||
case "evalCode": {
|
||||
// 1. 注入 WPS 对象
|
||||
// 2. 执行函数, 获取返回值
|
||||
let code = args?.code;
|
||||
console.log("start eval code");
|
||||
console.log(code);
|
||||
// var result = eval(code);
|
||||
// const tmpCode = `return ${code}`; // 为了获取返回值,需要在代码前面加上 return
|
||||
const result = new Function(code)(); // 执行并获取返回值
|
||||
console.log(result);
|
||||
console.log("success eval code");
|
||||
return { message: "ok", data: result }
|
||||
}
|
||||
// 1. 解析参数, 如果不是字符串, 则转换为字符串, 并用json解析, 解析失败直接retrun
|
||||
if (typeof param !== 'string') {
|
||||
param = JSON.stringify(param);
|
||||
}
|
||||
try {
|
||||
param = JSON.parse(param);
|
||||
} catch (e) {
|
||||
return { msg: '参数解析失败', code: -1 };
|
||||
}
|
||||
|
||||
return { message: "ok" }
|
||||
// 2. 判断是否有method字段, 如果没有直接return
|
||||
if (!param.method) {
|
||||
return { msg: '参数错误', code: -2 };
|
||||
}
|
||||
|
||||
// 3. 获取 method 字段对应的方法和 args 参数
|
||||
let method = param.method;
|
||||
|
||||
let args = null;
|
||||
|
||||
try {
|
||||
args = JSON.parse(param.args);
|
||||
} catch (e) {
|
||||
args = {};
|
||||
}
|
||||
|
||||
|
||||
console.log("method:", method);
|
||||
console.log("args:", args);
|
||||
// confsole.log("args", typeof args);
|
||||
|
||||
// 4. 判断 method 是否为 inject, 如果是则执行注入方法, 其他走默认方法执行调用函数
|
||||
if (method === 'inject') {
|
||||
let code = args.code;
|
||||
console.log("start eval code");
|
||||
console.log(code);
|
||||
console.log(new Function(code)());
|
||||
wpsInstance = new Function(code)(); // 执行并获取返回值
|
||||
console.log("success eval code");
|
||||
return { message: "inject ok", code: 0 }
|
||||
}
|
||||
|
||||
// 5. 判断是否有 wpsInstance, 如果没有直接return
|
||||
if (!wpsInstance) {
|
||||
return {msg: '未注入', code: -3};
|
||||
}
|
||||
|
||||
// 6. 判断 wpsInstance 是否有 method 方法, 如果没有直接return
|
||||
if (!wpsInstance[method]) {
|
||||
return {msg: '方法不存在', code: -4};
|
||||
}
|
||||
|
||||
// 7. 执行 wpsInstance 的 method 方法, 并返回结果
|
||||
try {
|
||||
const result = wpsInstance[method](args ?? null);
|
||||
return {msg: '执行成功', code: 0, data: result};
|
||||
} catch (e) {
|
||||
return {msg: '执行失败', code: -5};
|
||||
}
|
||||
}
|
||||
+45
-6
@@ -16,7 +16,7 @@ def get_version():
|
||||
conn = http.client.HTTPConnection("127.0.0.1", 58890)
|
||||
conn.request("POST", "/version", body="{}", headers={})
|
||||
response = conn.getresponse()
|
||||
data = response.read()
|
||||
response.read()
|
||||
conn.close()
|
||||
return response.status == 200
|
||||
except:
|
||||
@@ -27,8 +27,8 @@ def invoke_wps_func(func, args):
|
||||
|
||||
# 可能触发是否允许访问
|
||||
invoke_param = {
|
||||
"Func": func,
|
||||
"Args": json.dumps(args),
|
||||
"method": func,
|
||||
"args": json.dumps(args),
|
||||
"AppType": "wps",
|
||||
}
|
||||
|
||||
@@ -74,10 +74,49 @@ def invoke_wps_func(func, args):
|
||||
if __name__ == "__main__":
|
||||
start_http_server()
|
||||
|
||||
print(get_version())
|
||||
# print(get_version())
|
||||
path = r"c:\Users\32075\OneDrive\文档\Code\RPA_Script\wps-js-extension\XbotWps\python\wps.js"
|
||||
with open(path, "r", encoding="utf8") as f:
|
||||
code = f.read()
|
||||
|
||||
func = "getDocumentName"
|
||||
func = "evalCode"
|
||||
args = {"code": "wps.WpsApplication().ActiveDocument.Name"}
|
||||
func = "inject"
|
||||
args = {"code": code}
|
||||
res = invoke_wps_func(func, args)
|
||||
print(res)
|
||||
|
||||
func = "setText"
|
||||
args = {"text": "123"}
|
||||
res = invoke_wps_func(func, args)
|
||||
print(res)
|
||||
|
||||
# func = "addPicture"
|
||||
# image_path = r"C:\Users\32075\Downloads\Snipaste_2024-11-05_12-33-28.png"
|
||||
# args = {"path": image_path}
|
||||
# res = invoke_wps_func(func, args)
|
||||
# print(res)
|
||||
|
||||
# func = "replace"
|
||||
# args = {"text": "1", "replacement": "2", "code": code}
|
||||
# res = invoke_wps_func(func, args)
|
||||
# print(res)
|
||||
|
||||
# func = "save"
|
||||
# args = {}
|
||||
# res = invoke_wps_func(func, args)
|
||||
# print(res)
|
||||
|
||||
# func = "close"
|
||||
# args = {}
|
||||
# res = invoke_wps_func(func, args)
|
||||
# print(res)
|
||||
|
||||
# func = "getText"
|
||||
# args = {}
|
||||
# res = invoke_wps_func(func, args)
|
||||
# print(res)
|
||||
|
||||
# func = "insertHyperlink"
|
||||
# args = {"link": "https://www.baidu.com", "text": "百度"}
|
||||
# res = invoke_wps_func(func, args)
|
||||
# print(res)
|
||||
+73
-27
@@ -1,29 +1,75 @@
|
||||
const wps = {
|
||||
app: wps.WpsApplication(),
|
||||
replace: () => {
|
||||
Selection.SetRange(16, 16);
|
||||
Selection.Find.Wrap = wdFindContinue;
|
||||
(obj => {
|
||||
obj.Text = "1";
|
||||
obj.Forward = true;
|
||||
obj.Wrap = wdFindContinue;
|
||||
obj.MatchCase = false;
|
||||
obj.MatchByte = true;
|
||||
obj.MatchWildcards = false;
|
||||
obj.MatchWholeWord = false;
|
||||
obj.MatchFuzzy = false;
|
||||
obj.Replacement.Text = "2";
|
||||
})(Selection.Find);
|
||||
(obj => {
|
||||
obj.Style = "";
|
||||
obj.Highlight = wdUndefined;
|
||||
(obj => {
|
||||
obj.Style = "";
|
||||
obj.Highlight = wdUndefined;
|
||||
})(obj.Replacement);
|
||||
})(Selection.Find);
|
||||
Selection.Find.Execute(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, wdReplaceAll, undefined, undefined, undefined, undefined);
|
||||
Selection.Find.Replacement.Text = "2";
|
||||
}
|
||||
const wpsInstance = {
|
||||
app: wps.WpsApplication().Application,
|
||||
// 启动Word
|
||||
|
||||
// Word导出PDF
|
||||
|
||||
// 读取Word文本
|
||||
getText: () => {
|
||||
const activeDocument = wpsInstance.app.ActiveDocument;
|
||||
return activeDocument.Content.Text;
|
||||
},
|
||||
// 写入文本至Word中
|
||||
setText: (args) => {
|
||||
let text = args.text;
|
||||
const doc = wpsInstance.app.ActiveDocument;
|
||||
doc.Range(wpsInstance.app.Selection.Start, wpsInstance.app.Selection.End).Text = text;
|
||||
|
||||
},
|
||||
// 定位Word光标
|
||||
setCursor: (args) => {
|
||||
const activeDocument = wpsInstance.app.ActiveDocument;
|
||||
const range = activeDocument.Content;
|
||||
range.SetRange(args.start, args.end);
|
||||
},
|
||||
// 移动Word光标
|
||||
|
||||
// 替换Word文本
|
||||
replace: (args) => {
|
||||
const text = args.text;
|
||||
const replacement = args.replacement;
|
||||
const find = wpsInstance.app.Selection.Find;
|
||||
const activeDocument = wpsInstance.app.ActiveDocument;
|
||||
|
||||
activeDocument.Content.Select();
|
||||
find.Text = text;
|
||||
find.MatchByte = false;
|
||||
find.Replacement.Text = replacement;
|
||||
find.Execute(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 2, undefined, undefined, undefined, undefined);
|
||||
},
|
||||
// 读取Word表格
|
||||
|
||||
// 插入Word表格
|
||||
addTable: (args) => {
|
||||
const rows = args.rows;
|
||||
const cols = args.cols;
|
||||
const doc = wpsInstance.app.ActiveDocument;
|
||||
const range = wpsInstance.app.Selection.Range;
|
||||
|
||||
doc.Tables.Add(range, rows, cols);
|
||||
},
|
||||
// 插入图片
|
||||
addPicture: (args) => {
|
||||
let path = args.path;
|
||||
let doc = wpsInstance.app.ActiveDocument;
|
||||
doc.InlineShapes.AddPicture(FileName = path, Range = wpsInstance.app.Selection.Range);
|
||||
},
|
||||
// 插入超链接
|
||||
insertHyperlink: (args) => {
|
||||
let link = args.link;
|
||||
let text = args.text;
|
||||
let doc = wpsInstance.app.ActiveDocument;
|
||||
|
||||
doc.Hyperlinks.Add(wpsInstance.app.Selection.Range, link, undefined, undefined, text, undefined);
|
||||
},
|
||||
// 保存Word
|
||||
save: () => {
|
||||
wpsInstance.app.ActiveDocument.Save();
|
||||
},
|
||||
// 关闭Word
|
||||
close: () => {
|
||||
wpsInstance.app.ActiveDocument.Close();
|
||||
}
|
||||
}
|
||||
|
||||
return wpsInstance;
|
||||
Reference in new issue
Block a user