浏览代码

试运行JS增加显示返回值功能,且可以试运行提取数据操作中的JS

naibo 1 年之前
父节点
当前提交
499c3f21b6

+ 51 - 3
ElectronJS/main.js

@@ -787,7 +787,41 @@ async function beginInvoke(msg, ws) {
                     let waitTime = parameters.waitTime;
                     let element = await driver.findElement(By.tagName("body"));
                     if (codeMode == 0) {
-                        await execute_js(code, element, waitTime);
+                        let result = await execute_js(code, element, waitTime);
+                        let level = "success";
+                        if (result == -1) {
+                            level = "info";
+                        }
+                        if (result != null) {
+                            notify_browser(
+                                "JavaScript操作返回结果:" + result,
+                                "JavaScript operation returns result: " + result,
+                                level
+                            );
+                        }
+                    } else if (codeMode == 2) { // 循环内的JS代码
+                        let parent_node = JSON.parse(msg.message.parentNode);
+                        let parent_xpath = parent_node.parameters.xpath;
+                        if (parent_node.parameters.loopType == 2) {
+                            parent_xpath = parent_node.parameters.pathList
+                                .split("\n")[0]
+                                .trim();
+                        }
+                        let elementInfo = {iframe: parameters.iframe, xpath: parent_xpath, id: -1};
+                        let element = await findElementAcrossAllWindows(
+                            elementInfo, notifyBrowser = false); //通过此函数找到元素并切换到对应的窗口
+                        let result = await execute_js(code, element, waitTime);
+                        let level = "success";
+                        if (result == -1) {
+                            level = "info";
+                        }
+                        if (result != null) {
+                            notify_browser(
+                                "JavaScript操作返回结果:" + result,
+                                "JavaScript operation returns result: " + result,
+                                level
+                            );
+                        }
                     } else if (codeMode == 8) {
                         //刷新页面
                         try {
@@ -860,8 +894,8 @@ async function beginInvoke(msg, ws) {
                 } else if (option == 11) {
                     //单个提取数据参数
                     notify_browser(
-                        "提示:提取数据操作只能试运行设置的JavaScript语句,且只针对第一个匹配的元素。",
-                        "Hint: can only test JavaScript  statement set in the data extraction operation, and only for the first matching element.",
+                        "提示:提取数据字段的试运行操作只能试设置的JavaScript语句,且只针对第一个匹配的元素。",
+                        "Hint: can only test JavaScript  statement set in the data extraction field operation, and only for the first matching element.",
                         "info"
                     );
                     let params = parameters.params; //所有的提取数据参数
@@ -885,6 +919,20 @@ async function beginInvoke(msg, ws) {
                     );
                     if (element != null) {
                         await execute_js(param.beforeJS, element, param.beforeJSWaitTime);
+                        if(param.contentType == 9){ //针对元素的JavaScript代码返回值
+                            let result = await execute_js(param.JS, element);
+                            let level = "success";
+                            if (result == -1) {
+                                level = "info";
+                            }
+                            if (result != null) {
+                                notify_browser(
+                                    "JavaScript操作返回结果:" + result,
+                                    "JavaScript operation returns result: " + result,
+                                    level
+                                );
+                            }
+                        }
                         await execute_js(param.afterJS, element, param.afterJSWaitTime);
                     }
                 }

+ 1 - 1
ElectronJS/src/taskGrid/FlowChart.html

@@ -243,6 +243,7 @@
                         <p><button type="button" data-toggle="modal" data-target="#myModal_XPath" @click="changeXPaths(params.parameters[paraIndex]['allXPaths'])" class="btn btn-primary" style="margin-top: 10px">Click here to view other equivalent XPath expressions</button></p>
                         <label>Final XPath of this field when the task is running:</label>
                         <textarea spellcheck="false" onkeydown="inputDelete(event)" class="form-control" rows="2" readonly style="background:ghostwhite">{{getFinalXPath(params.parameters[paraIndex]['relativeXPath'], params.parameters[paraIndex]['relative'])}}</textarea>
+                        <div style="margin-top: 10px"><a href="#" v-on:mousedown="trailParam(paraIndex)" style="text-decoration: none">Trail Run</a></div>
                         <p style="margin-top: 10px">
                             <a class="btn btn-primary" data-toggle="collapse" href="#elementAdvanced" role="button" aria-expanded="false" aria-controls="collapseExample">
                                 Click here to expand/collapse advanced operations
@@ -250,7 +251,6 @@
                         </p>
                         <div :class="{collapse: true, 'show': params.parameters[paraIndex]['beforeJS'].length!=0 || params.parameters[paraIndex]['afterJS'].length!=0}" id="elementAdvanced">
                             <div>
-                                <div><a href="#" v-on:mousedown="trailParam(paraIndex)" style="text-decoration: none">Trail Run</a></div>
                                 <label>Execute a JavaScript script <strong>before</strong> extracting data from this element: </label>
                                 <textarea spellcheck=false onkeydown="inputDelete(event)" class="form-control" rows="2"
                                           placeholder='The element should be represented by arguments[0]. Here is an example JavaScript code: arguments[0].innerText = arguments[0].innerText.replace("United States","US"). This code replaces occurrences of "United States" with "US" in the text of the element. Subsequently, when extracting data, you will obtain the replaced value.' v-model='params.parameters[paraIndex]["beforeJS"]'></textarea>

+ 2 - 2
ElectronJS/src/taskGrid/FlowChart.js

@@ -175,7 +175,7 @@ let app = new Vue({
                     this.nowNode["title"] = LANG("运行操作系统命令", "Run OS Command");
                     break;
                 case 2:
-                    this.nowNode["title"] = LANG("执行JavaScript", "Run JavaScript");
+                    this.nowNode["title"] = LANG("循环内元素执行JS", "Run JS in Loop");
                     break;
                 case 3:
                     this.nowNode["title"] = LANG("退出循环", "Exit Loop");
@@ -505,7 +505,7 @@ function elementDblClick(e) {
                 showInfo(LANG("试运行功能不适用于循环操作,请试运行循环内部的具体操作,如点击元素。", "The trial run function is not applicable to loop operations. Please try to run the specific operations in the loop, such as clicking elements."));
             }
         } else {
-            if (nodeType == 5 && (app._data.nowNode["parameters"]["codeMode"] != 0 && app._data.nowNode["parameters"]["codeMode"] != 8)) {
+            if (nodeType == 5 && (app._data.nowNode["parameters"]["codeMode"] != 0 && app._data.nowNode["parameters"]["codeMode"] != 2 && app._data.nowNode["parameters"]["codeMode"] != 8)) {
                 showInfo(LANG("试运行自定义操作功能只适用于执行JavaScript和刷新页面操作。", "The trial run custom action function is only applicable to run JavaScript and refresh page operations."));
             } else {
                 trailElement(app._data.nowNode, 1);

+ 1 - 1
ElectronJS/src/taskGrid/FlowChart_CN.html

@@ -243,6 +243,7 @@
                         <p><button type="button" data-toggle="modal" data-target="#myModal_XPath" @click="changeXPaths(params.parameters[paraIndex]['allXPaths'])" class="btn btn-primary" style="margin-top: 10px">点此查看其他等价的XPath</button></p>
                         <label>任务运行时最终定位的本字段XPath:</label>
                         <textarea spellcheck=false onkeydown="inputDelete(event)" class="form-control" rows="2" readonly style="background:ghostwhite">{{getFinalXPath(params.parameters[paraIndex]['relativeXPath'], params.parameters[paraIndex]['relative'])}}</textarea>
+                        <div style="margin-top: 10px"><a href="#" v-on:mousedown="trailParam(paraIndex)" style="text-decoration: none">试运行</a></div>
                         <p style="margin-top: 10px">
                             <a class="btn btn-primary" data-toggle="collapse" href="#elementAdvanced" role="button" aria-expanded="false" aria-controls="collapseExample">
                                 点此展开/折叠自定义操作
@@ -250,7 +251,6 @@
                         </p>
                         <div :class="{collapse: true, 'show': params.parameters[paraIndex]['beforeJS'].length!=0 || params.parameters[paraIndex]['afterJS'].length!=0}" id="elementAdvanced">
                             <div>
-                                <div><a href="#" v-on:mousedown="trailParam(paraIndex)" style="text-decoration: none">试运行</a></div>
                                 <label>提取该元素数据<strong>前</strong>针对该元素执行一段JavaScript脚本: </label>
                                 <textarea spellcheck=false onkeydown="inputDelete(event)" class="form-control" rows="2"
                                           placeholder='该元素用arguments[0]来表示,示例JS代码:arguments[0].innerText = arguments[0].innerText.replace("上海","Shanghai")即实现了将元素文字中的“上海”替换成”Shanghai“的功能,然后后续如提取数据时就会提取到替换后的值。' v-model='params.parameters[paraIndex]["beforeJS"]'></textarea>

+ 1 - 1
ElectronJS/src/taskGrid/taskList.html

@@ -167,7 +167,7 @@
             },
             modifyTask: function (index, row) {
                 let id = row.id;
-                let url = row.url;
+                let url = row.links.split("\n")[0];
                 console.log(index, row)
                 let message = { //显示flowchart
                     type: 1, //消息类型,传递链接

文件差异内容过多而无法显示
+ 0 - 0
ElectronJS/tasks/315.json


+ 1 - 1
ExecuteStage/.vscode/launch.json

@@ -12,7 +12,7 @@
             "justMyCode": false,
             //  "args": ["--ids", "[7]", "--read_type", "remote", "--headless", "0"]
             // "args": ["--ids", "[9]", "--read_type", "remote", "--headless", "0", "--saved_file_name", "YOUTUBE"]
-            "args": ["--ids", "[67]", "--headless", "0", "--user_data", "0", "--keyboard", "0",
+            "args": ["--ids", "[13]", "--headless", "0", "--user_data", "0", "--keyboard", "0",
         "--read_type", "remote"]
             // "args": "--ids '[97]' --user_data 1 --server_address http://localhost:8074 --config_folder '/Users/naibo/Documents/EasySpider/ElectronJS/' --headless 0 --read_type remote --config_file_name config.json --saved_file_name"
         }

部分文件因为文件数量过多而无法显示