JavaScript输入文字!!!

This commit is contained in:
naibo 2023-12-23 12:36:35 +08:00
parent ddb10c79bf
commit 13b51ffdcc
9 changed files with 46 additions and 8 deletions

View File

@ -741,6 +741,22 @@ async function beginInvoke(msg, ws) {
keyInfo = keyInfo.replace(/<enter>/gi, "");
enter = true;
}
// 如果返回值中包含JS
if (/JS\(/i.test(keyInfo)) {
// 创建一个新的正则表达式来匹配JS语句
let pattern = /JS\("(.+?)"\)/gi;
// 找出所有的匹配项
let matches = [...keyInfo.matchAll(pattern)];
// 处理每一个匹配项
for (let match of matches) {
// 执行 JS 代码并等待结果
let jsReplacedText = await execute_js(match[1], null, 0);
// 替换匹配到的 JS 语句
keyInfo = keyInfo.replace(match[0], jsReplacedText.toString());
}
}
if (keyInfo.indexOf("Field(") >= 0 || keyInfo.indexOf("eval(") >= 0) {
//两秒后通知浏览器
await new Promise((resolve) => setTimeout(resolve, 2000));
@ -1184,6 +1200,12 @@ wss.on("connection", function (ws) {
});
});
wss.on("error", function (err) {
dialog.showErrorBox("端口占用错误 Port Occupied Error", "端口" + websocket_port + "被占用大概率是重复打开了多个EasySpider程序导致小概率是其他程序占用了此端口请关闭所有已打开的EasySpider程序及其他占用此端口的程序或重启系统后再次尝试打开软件。\nPort " + websocket_port + " is occupied, it is most likely that multiple EasySpider programs are opened repeatedly, or other programs occupy this port. Please close all opened EasySpider programs and other programs that occupy this port, or restart the system and try to open the software again.");
//退出程序
app.quit();
});
console.log(process.platform);
async function runBrowser(lang = "en", user_data_folder = "", mobile = false) {

View File

@ -349,8 +349,8 @@
</div>
<div v-if='!useLoop'>
<label>Input value (Use Field["FieldName"] to input the latest extracted/returned value of a field or custom operation. Use &lt;enter&gt; or &lt;ENTER&gt; to simulate pressing the Enter key):</label>
<input spellcheck=false onkeydown="inputDelete(event)" class="form-control" v-model='nowNode["parameters"]["value"]'></input>
<label>Input value (Use Field["FieldName"] to input the latest extracted/returned value of a field or custom operation. Use &lt;enter&gt; or &lt;ENTER&gt; to simulate pressing the Enter key, use JS("return JS code") and eval("Python code") to replace with the return values of JavaScript and Python code:</label>
<textarea spellcheck=false onkeydown="inputDelete(event)" class="form-control" v-model='nowNode["parameters"]["value"]'></textarea>
</div>
<label>XPath: <span style="font-size: 30px!important;" title="Relative XPATH writing: start with /, e.g. the loop item XPATH is /html/body/div[1], your input is /*[@id='tab-customer'], then the final addressed xpath is: /html/body/div[1]/*[@id='tab-customer']"></span></label>

View File

@ -349,8 +349,8 @@
</div>
<div v-if='!useLoop'>
<label>输入值用Field["字段名"]来输入某字段/自定义操作的最新提取/返回值,用&lt;enter&gt;&lt;ENTER&gt;表示模拟按下回车键):</label>
<input spellcheck=false onkeydown="inputDelete(event)" class="form-control" v-model='nowNode["parameters"]["value"]'></input>
<label>输入值用Field["字段名"]来输入某字段/自定义操作的最新提取/返回值,用&lt;enter&gt;&lt;ENTER&gt;表示模拟按下回车键)用JS("return JS代码")和eval("Python代码")来替换成JavaScript和Python代码返回值</label>
<textarea spellcheck=false onkeydown="inputDelete(event)" class="form-control" v-model='nowNode["parameters"]["value"]'></textarea>
</div>
<label>XPath <span style="font-size: 30px!important;" title="相对XPATH写法以/开头如循环项XPATH为/html/body/div[1],您的输入为/*[@id='tab-customer'],则最终寻址的xpath为/html/body/div[1]/*[@id='tab-customer']"></span></label>

View File

@ -0,0 +1 @@
{"id":306,"name":"XML Example","url":"https://www.chinanews.com.cn/rss/scroll-news.xml","links":"https://www.chinanews.com.cn/rss/scroll-news.xml","create_time":"2023-12-23 10:47:31","update_time":"2023-12-23 11:07:16","version":"0.6.0","saveThreshold":10,"quitWaitTime":60,"environment":0,"maximizeWindow":0,"maxViewLength":15,"recordLog":1,"outputFormat":"xlsx","saveName":"current_time","dataWriteMode":1,"inputExcel":"","startFromExit":0,"pauseKey":"p","containJudge":false,"browser":"chrome","removeDuplicate":0,"desc":"https://www.chinanews.com.cn/rss/scroll-news.xml","inputParameters":[{"id":0,"name":"urlList_0","nodeId":1,"nodeName":"打开网页","value":"https://www.chinanews.com.cn/rss/scroll-news.xml","desc":"要采集的网址列表,多行以\\n分开","type":"text","exampleValue":"https://www.chinanews.com.cn/rss/scroll-news.xml"}],"outputParameters":[{"id":0,"name":"自定义参数_1","desc":"","type":"text","recordASField":1,"exampleValue":"自定义值"}],"graph":[{"index":0,"id":0,"parentId":0,"type":-1,"option":0,"title":"root","sequence":[1,3],"parameters":{"history":1,"tabIndex":0,"useLoop":false,"xpath":"","iframe":false,"wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0},"isInLoop":false},{"id":1,"index":1,"parentId":0,"type":0,"option":1,"title":"打开网页","sequence":[],"isInLoop":false,"position":0,"parameters":{"useLoop":false,"xpath":"","wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"url":"https://www.chinanews.com.cn/rss/scroll-news.xml","links":"https://www.chinanews.com.cn/rss/scroll-news.xml","maxWaitTime":10,"scrollType":0,"scrollCount":1,"scrollWaitTime":1,"cookies":""}},{"id":3,"index":2,"parentId":2,"type":0,"option":3,"title":"提取数据","sequence":[],"isInLoop":true,"position":0,"parameters":{"history":1,"tabIndex":0,"useLoop":false,"xpath":"","iframe":false,"wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"clear":0,"newLine":1,"params":[{"nodeType":0,"contentType":0,"relative":true,"name":"自定义参数_1","desc":"","iframe":false,"extractType":0,"relativeXPath":"","recordASField":1,"allXPaths":[],"exampleValues":[{"num":0,"value":"自定义值"}],"default":"","beforeJS":"1","beforeJSWaitTime":0,"JS":"","JSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"downloadPic":0,"paraType":"text","splitLine":0}]}},{"id":2,"index":3,"parentId":0,"type":1,"option":8,"title":"循环 - 不固定元素列表","sequence":[2],"isInLoop":false,"position":1,"parameters":{"history":1,"tabIndex":0,"useLoop":false,"xpath":"/rss/channel/item","iframe":false,"wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"scrollType":0,"scrollCount":1,"scrollWaitTime":1,"loopType":1,"pathList":"","textList":"","code":"","waitTime":0,"exitCount":0,"exitElement":"//body","historyWait":2,"breakMode":0,"breakCode":"","breakCodeWaitTime":0,"skipCount":0}},{"id":-1,"index":4,"parentId":0,"type":0,"option":3,"title":"提取数据","sequence":[],"isInLoop":false,"position":1,"parameters":{"history":1,"tabIndex":0,"useLoop":false,"xpath":"","iframe":false,"wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"clear":0,"newLine":1,"params":[{"nodeType":0,"contentType":0,"relative":false,"name":"自定义参数_1","desc":"","iframe":false,"extractType":0,"relativeXPath":"/rss/channel/item","recordASField":1,"allXPaths":[],"exampleValues":[{"num":0,"value":"自定义值"}],"default":"","beforeJS":"","beforeJSWaitTime":0,"JS":"","JSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"downloadPic":0,"paraType":"text","splitLine":0}]}}]}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -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", "[11]", "--headless", "0", "--user_data", "0", "--keyboard", "0",
"args": ["--ids", "[60]", "--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"
}

View File

@ -1588,10 +1588,11 @@ class BrowserThread(Thread):
else:
value = param["value"]
# 将value中的Field[""]替换为outputParameters中的键值
pattern = r'Field\["([^"]+)"\]'
# pattern = r'Field\["([^"]+)"\]'
try:
replaced_text = re.sub(
pattern, lambda match: self.outputParameters.get(match.group(1), ''), value)
# replaced_text = re.sub(
# pattern, lambda match: self.outputParameters.get(match.group(1), ''), value)
replaced_text = replace_field_values(value, self.outputParameters, self)
replaced_text = re.sub(
'<enter>', '', replaced_text, flags=re.IGNORECASE)
except:

View File

@ -308,6 +308,18 @@ def replace_field_values(orginal_text, outputParameters, browser=None):
eval_replaced_text = str(eval(match.group(1)))
# 替换eval语句
replaced_text = replaced_text.replace(match.group(0), eval_replaced_text)
if re.search(r'JS\(', replaced_text, re.IGNORECASE): # 如果返回值中包含JS
replaced_text = replaced_text.replace("self.", "browser.")
pattern = re.compile(r'(?i)JS\("(.+?)"\)')
# 循环替换所有匹配到的JS语句
while True:
match = pattern.search(replaced_text)
if not match:
break
# 执行JS并将其结果转换为字符串形式
JS_replaced_text = str(browser.browser.execute_script(match.group(1)))
# 替换JS语句
replaced_text = replaced_text.replace(match.group(0), JS_replaced_text)
except Exception as e:
print("eval替换失败请检查eval语句是否正确。| Failed to replace eval, please check if the eval statement is correct.")
print(e)