Skip to content

Commit 80e9f8e

Browse files
Merge pull request #390 from AutomationSolutionz/inspector
Inspector UI and action improvements
2 parents b09f9a5 + 9b727b5 commit 80e9f8e

File tree

16 files changed

+19874
-243
lines changed

16 files changed

+19874
-243
lines changed

Apps/Web/AI_Recorder/background/back.js

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var metaData = {};
22

3-
fetch("data.json")
3+
fetch("./data.json")
44
.then(Response => Response.json())
55
.then(data => {
66
metaData = data;
@@ -9,6 +9,8 @@ fetch("data.json")
99
const browserAppData = chrome || browser;
1010

1111
import './back_zeuz.js';
12+
import './sentiment_analyzer.js';
13+
import './back_reocrder.js';
1214
// import '../common_files/poly_fill.js';
1315

1416
/* Zeuz function start */
@@ -19,7 +21,7 @@ var clickEnabled = true;
1921

2022
// import {getWindowSize} from "/back_zeuz.js";
2123
function getWindowSize(callback) {
22-
chrome.storage.local.get('window', function(result) {
24+
browserAppData.storage.local.get('window', function(result) {
2325
var height = 740;
2426
//var width = 780;
2527
var width = 1110;
@@ -112,60 +114,25 @@ function open_panel(tab) {
112114

113115
/* Create menu */
114116
function create_menus() {
115-
browserAppData.contextMenus.create({
116-
id: "verifyText",
117-
title: "verifyText",
118-
documentUrlPatterns: ["<all_urls>"],
119-
contexts: ["all"]
120-
});
121-
browserAppData.contextMenus.create({
122-
id: "verifyTitle",
123-
title: "verifyTitle",
124-
documentUrlPatterns: ["<all_urls>"],
125-
contexts: ["all"]
126-
});
127-
browserAppData.contextMenus.create({
128-
id: "verifyValue",
129-
title: "verifyValue",
130-
documentUrlPatterns: ["<all_urls>"],
131-
contexts: ["all"]
132-
});
133-
browserAppData.contextMenus.create({
134-
id: "assertText",
135-
title: "assertText",
136-
documentUrlPatterns: ["<all_urls>"],
137-
contexts: ["all"]
138-
});
139-
browserAppData.contextMenus.create({
140-
id: "assertTitle",
141-
title: "assertTitle",
142-
documentUrlPatterns: ["<all_urls>"],
143-
contexts: ["all"]
144-
});
145-
browserAppData.contextMenus.create({
146-
id: "assertValue",
147-
title: "assertValue",
148-
documentUrlPatterns: ["<all_urls>"],
149-
contexts: ["all"]
150-
});
151-
browserAppData.contextMenus.create({
152-
id: "storeText",
153-
title: "storeText",
154-
documentUrlPatterns: ["<all_urls>"],
155-
contexts: ["all"]
156-
});
157-
browserAppData.contextMenus.create({
158-
id: "storeTitle",
159-
title: "storeTitle",
160-
documentUrlPatterns: ["<all_urls>"],
161-
contexts: ["all"]
162-
});
163-
browserAppData.contextMenus.create({
164-
id: "storeValue",
165-
title: "storeValue",
166-
documentUrlPatterns: ["<all_urls>"],
167-
contexts: ["all"]
168-
});
117+
let menus = [
118+
["Go_to_link", "Go to link"],
119+
["Save_Text", "Save Text"],
120+
["Validate_Text", "Validate Text"],
121+
["Validate_Text_By_AI", "Validate Text by AI"],
122+
["Wait_For_Element_To_Appear", "Wait for Element to Appear"],
123+
["Wait_For_Element_To_Disappear", "Wait for Element to Disappear"],
124+
]
125+
for(let i =0; i< menus.length; i++){
126+
browserAppData.contextMenus.create({
127+
id: menus[i][0],
128+
title: menus[i][1],
129+
documentUrlPatterns: [
130+
"http://*/*",
131+
"https://*/*"
132+
],
133+
contexts: ["all"]
134+
})
135+
}
169136
}
170137

171138

@@ -189,47 +156,12 @@ browserAppData.contextMenus.onClicked.addListener(function(info, tab) {
189156

190157
browserAppData.runtime.onConnect.addListener(function(m) {
191158
port = m;
159+
console.log(port);
192160
});
193161

194162
/* After install open the url */
195-
chrome.runtime.onInstalled.addListener(function (details) {
163+
browserAppData.runtime.onInstalled.addListener(function (details) {
196164
if (details.reason === 'install') {
197165
console.log("Recorder Installed");
198166
}
199167
});
200-
201-
browserAppData.runtime.onMessage.addListener(
202-
function(request, sender, sendResponse) {
203-
if (request.apiName == 'ai_single_action') {
204-
var url = `${metaData.url}/ai_record_single_action/`
205-
var url_get = `${url}?${new URLSearchParams(request.dataj)}`;
206-
console.log(url)
207-
console.log(metaData.apiKey)
208-
console.log(request.data)
209-
console.log(request.dataj)
210-
// fetch(url, {
211-
// method: "GET",
212-
// headers: {
213-
// "Content-Type": "application/json",
214-
// "X-Api-Key": metaData.apiKey,
215-
// },
216-
// })
217-
// .then(response => response.text())
218-
// .then(text => {console.log(text);sendResponse(text);})
219-
// .catch(error => console.error(error))
220-
221-
fetch(url, {
222-
method: "POST",
223-
headers: {
224-
// "Content-Type": "application/json",
225-
"X-Api-Key": metaData.apiKey,
226-
},
227-
body: request.data,
228-
})
229-
.then(response => response.json())
230-
.then(text => {console.log(text);sendResponse(text);})
231-
232-
return true; // Will respond asynchronously.
233-
}
234-
}
235-
);
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
var metaData = {};
2+
3+
fetch("./data.json")
4+
.then(Response => Response.json())
5+
.then(data => {
6+
metaData = data;
7+
});
8+
9+
const browserAppData = chrome || browser;
10+
11+
var idx = 0;
12+
var recorded_actions = [];
13+
var action_name_convert = {
14+
type: "text",
15+
open: "go to link",
16+
Go_to_link: "go to link",
17+
doubleClick: "double click",
18+
Validate_Text: "validate full text",
19+
Validate_Text_By_AI: "validate full text by ai",
20+
Save_Text: "save attribute",
21+
Wait_For_Element_To_Appear: "wait",
22+
Wait_For_Element_To_Disappear: "wait disable",
23+
}
24+
25+
async function start_recording(){
26+
idx = 0;
27+
recorded_actions = [];
28+
}
29+
async function stop_recording(){
30+
// When there are 2 iframes. it saves 3 times. this is a temporary fix. Should be fixed properly
31+
if (recorded_actions.length > 0)
32+
browserAppData.storage.local.set({
33+
recorded_actions: recorded_actions,
34+
}).then(()=>{
35+
idx = 0;
36+
recorded_actions = [];
37+
});
38+
}
39+
async function fetchAIData(idx, command, value, url, document){
40+
if (command === 'go to link'){
41+
let go_to_link = {
42+
action: 'go to link',
43+
data_list: [url],
44+
element: "",
45+
is_disable: false,
46+
name: `Open ${(url.length>25) ? url.slice(0,20) + '...' : url}`,
47+
value: url,
48+
main: [['go to link', 'selenium action', url]],
49+
xpath: "",
50+
};
51+
recorded_actions[idx] = go_to_link;
52+
console.log(recorded_actions);
53+
browserAppData.storage.local.set({
54+
recorded_actions: recorded_actions,
55+
})
56+
return;
57+
}
58+
recorded_actions[idx] = 'empty';
59+
browserAppData.storage.local.set({
60+
recorded_actions: recorded_actions,
61+
})
62+
let validate_full_text_by_ai = false
63+
if (command === 'validate full text by ai'){
64+
command = 'validate full text';
65+
validate_full_text_by_ai = true;
66+
}
67+
68+
var dataj = {
69+
"page_src": document,
70+
"action_name": command,
71+
"action_type": "selenium",
72+
"action_value": value,
73+
"source": "web",
74+
};
75+
var data = JSON.stringify(dataj);
76+
77+
const url_ = `${metaData.url}/ai_record_single_action/`
78+
const input = {
79+
method: "POST",
80+
headers: {
81+
// "Content-Type": "application/json",
82+
"X-Api-Key": metaData.apiKey,
83+
},
84+
body: data,
85+
}
86+
var r = await fetch(url_, input)
87+
var resp = await r.json();
88+
let response = resp.ai_choices;
89+
90+
if (validate_full_text_by_ai){
91+
let text_classifier = await browserAppData.runtime.sendMessage({
92+
action: 'content_classify',
93+
text: value,
94+
});
95+
96+
console.log("text_classifier", text_classifier);
97+
let label = text_classifier[0].label;
98+
label = label.charAt(0).toUpperCase() + label.slice(1).toLowerCase();
99+
let offset = Number((text_classifier[0].score * 0.9).toFixed(2));
100+
offset = Math.max(0.8, offset);
101+
response[0].data_set = response[0].data_set.slice(0,-1)
102+
.concat([[label, "text classifier offset", offset]])
103+
.concat(response[0].data_set.slice(-1))
104+
value = '';
105+
}
106+
else if (command === 'save attribute'){
107+
response[0].data_set = response[0].data_set.slice(0,-1)
108+
.concat([
109+
["text", "save parameter", "var_name"],
110+
["save attribute", "selenium action", "save attribute"],
111+
])
112+
value = '';
113+
}
114+
else if (['wait', 'wait disable'].includes(command)){
115+
value = 10;
116+
}
117+
response[0].short.value = value;
118+
if (value) response[0].data_set[response[0].data_set.length-1][response[0].data_set[0].length-1] = value;
119+
recorded_actions[idx] = {
120+
action: response[0].short.action,
121+
data_list: [response[0].short.value],
122+
element: response[0].short.element,
123+
is_disable: false,
124+
name: response[0].name,
125+
value: response[0].short.value,
126+
main: response[0].data_set,
127+
xpath: response[0].xpath,
128+
};
129+
console.log(recorded_actions);
130+
browserAppData.storage.local.set({
131+
recorded_actions: recorded_actions,
132+
})
133+
}
134+
135+
async function record_action(command, value, url, document){
136+
if (Object.keys(action_name_convert).includes(command)) command = action_name_convert[command]
137+
console.log("... Action recorder start");
138+
idx += 1;
139+
if (recorded_actions.length === 0 || recorded_actions.length > 0 && recorded_actions[0].action != 'go to link'){
140+
let go_to_link = {
141+
action: 'go to link',
142+
data_list: [url],
143+
element: "",
144+
is_disable: false,
145+
name: `Open ${(url.length>25) ? url.slice(0,20) + '...' : url}`,
146+
value: url,
147+
main: [['go to link', 'selenium action', url]],
148+
xpath: "",
149+
};
150+
if (recorded_actions.length === 0) recorded_actions[0] = go_to_link;
151+
else recorded_actions.unshift(go_to_link);
152+
idx += 1;
153+
}
154+
fetchAIData(idx-1, command, value, url, document);
155+
}
156+
browserAppData.runtime.onMessage.addListener(
157+
function(request, sender, sendResponse) {
158+
if (request.apiName == 'start_recording') {
159+
start_recording();
160+
}
161+
else if (request.apiName == 'record_action') {
162+
record_action(
163+
request.command,
164+
// request.target,
165+
request.value,
166+
request.url,
167+
request.document,
168+
);
169+
}
170+
else if (request.apiName == 'stop_recording') {
171+
stop_recording();
172+
}
173+
}
174+
);

Apps/Web/AI_Recorder/background/back_zeuz.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var attachedTabs = {};
2-
const browser = chrome || browser;
2+
const browserAppData = chrome || browser;
33
/* Fetch the window size */
44
function getWindowSize(callback) {
55
chrome.storage.local.get('window', function(result) {
@@ -222,10 +222,10 @@ if (chrome.debugger) {
222222

223223
var externalCapabilities = {};
224224

225-
browser.runtime.onMessage.addListener(function(request, sender, sendResponse, type) {
225+
browserAppData.runtime.onMessage.addListener(function(request, sender, sendResponse, type) {
226226
if (request.captureEntirePageScreenshot) {
227227
var windowId = request.captureWindowId || sender.tab.windowId;
228-
browser.tabs.captureVisibleTab(windowId, { format: 'png' }).then(function(image) {
228+
browserAppData.tabs.captureVisibleTab(windowId, { format: 'png' }).then(function(image) {
229229
sendResponse({
230230
image: image
231231
});
@@ -288,7 +288,7 @@ chrome.runtime.onMessageExternal.addListener(function(message, sender) {
288288
}
289289
});
290290

291-
browser.runtime.onMessage.addListener(function(message, sender, sendResponse) {
291+
browserAppData.runtime.onMessage.addListener(function(message, sender, sendResponse) {
292292
if (message.getExternalCapabilities) {
293293
var now = new Date().getTime();
294294
Object.keys(externalCapabilities).forEach(function(capabilityGlobalId) {
@@ -302,7 +302,7 @@ browser.runtime.onMessage.addListener(function(message, sender, sendResponse) {
302302
}
303303
});
304304

305-
browser.runtime.onMessage.addListener(function(message, sender, sendResponse) {
305+
browserAppData.runtime.onMessage.addListener(function(message, sender, sendResponse) {
306306
if (message.checkChromeDebugger) {
307307
sendResponse({
308308
status: !!chrome.debugger

0 commit comments

Comments
 (0)