Skip to content

Commit da12416

Browse files
Merge pull request #381 from AutomationSolutionz/recorder
Recorder
2 parents 1713209 + 691d1a8 commit da12416

File tree

10 files changed

+343
-204
lines changed

10 files changed

+343
-204
lines changed

Apps/Web/AI_Recorder/background/back.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ function getWindowSize(callback) {
3434

3535
function open_panel(tab) {
3636
browserAppData.storage.local.set({
37-
meta_data: metaData
38-
})
37+
meta_data: metaData,
38+
recorded_actions: [],
39+
});
3940
let contentWindowId = tab.windowId;
4041
if (master[contentWindowId] != undefined) {
4142
browserAppData.windows.update(master[contentWindowId], {
@@ -188,4 +189,40 @@ chrome.runtime.onInstalled.addListener(function (details) {
188189
if (details.reason === 'install') {
189190
console.log("Recorder Installed");
190191
}
191-
});
192+
});
193+
194+
browserAppData.runtime.onMessage.addListener(
195+
function(request, sender, sendResponse) {
196+
if (request.apiName == 'ai_single_action') {
197+
var url = `${metaData.url}/ai_record_single_action/`
198+
var url_get = `${url}?${new URLSearchParams(request.dataj)}`;
199+
console.log(url)
200+
console.log(metaData.apiKey)
201+
console.log(request.data)
202+
console.log(request.dataj)
203+
// fetch(url, {
204+
// method: "GET",
205+
// headers: {
206+
// "Content-Type": "application/json",
207+
// "X-Api-Key": metaData.apiKey,
208+
// },
209+
// })
210+
// .then(response => response.text())
211+
// .then(text => {console.log(text);sendResponse(text);})
212+
// .catch(error => console.error(error))
213+
214+
fetch(url, {
215+
method: "POST",
216+
headers: {
217+
// "Content-Type": "application/json",
218+
"X-Api-Key": metaData.apiKey,
219+
},
220+
body: request.data,
221+
})
222+
.then(response => response.json())
223+
.then(text => {console.log(text);sendResponse(text);})
224+
225+
return true; // Will respond asynchronously.
226+
}
227+
}
228+
);

Apps/Web/AI_Recorder/content/recorder.js

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* ZeuZ Start the recording function */
2+
browserAppData = chrome || browser;
23
class Recorder {
34

45
/* exq initial time */
@@ -11,6 +12,14 @@ class Recorder {
1112
frameLocation: this.frameLocation
1213
}).catch(function(reason) {
1314
});
15+
this.recorded_actions = [];
16+
this.idx = 0;
17+
// Convert to the zeuz defined action names
18+
this.action_name_convert = {
19+
type: "text",
20+
open: "go to link",
21+
doubleClick: "double click",
22+
}
1423
}
1524

1625
/* Parse the key */
@@ -40,8 +49,81 @@ class Recorder {
4049
}
4150

4251
/* Recorder */
52+
async fetchAIData(target, idx, command, value){
53+
var html = document.createElement('html');
54+
html.innerHTML = document.documentElement.outerHTML;
55+
56+
for (let each of target) if (each[1] == 'xpath:position') {var xpath = each[0];break;}
57+
var xPathResult = document.evaluate(xpath, html);
58+
if(xPathResult) var main_elem = xPathResult.iterateNext();
59+
else return;
60+
61+
main_elem.setAttribute('zeuz', 'aiplugin');
62+
console.log(main_elem.hasAttribute('zeuz'), main_elem);
63+
64+
// get all <head> elements from html
65+
var elements = html.getElementsByTagName('head');
66+
while (elements[0])
67+
elements[0].parentNode.removeChild(elements[0])
68+
69+
// get all <script> elements from html
70+
var elements = html.getElementsByTagName('script');
71+
while (elements[0])
72+
elements[0].parentNode.removeChild(elements[0])
73+
74+
// get all <style> elements from html
75+
var elements = html.getElementsByTagName('style');
76+
while (elements[0])
77+
elements[0].parentNode.removeChild(elements[0])
78+
79+
var xPathResult = document.evaluate(xpath, document);
80+
if(xPathResult) console.log('doc true')
81+
else console.log('doc false');
82+
83+
var xPathResult = document.evaluate(xpath, html);
84+
if(xPathResult) console.log('html true')
85+
else console.log('html false');
86+
87+
var dataj = {
88+
"page_src": html.outerHTML,
89+
"action_name": command,
90+
"action_type": "selenium",
91+
"action_value": value,
92+
"source": "web",
93+
};
94+
var data = JSON.stringify(dataj);
95+
96+
browserAppData.runtime.sendMessage({
97+
apiName: 'ai_single_action',
98+
data: data,
99+
dataj: dataj,
100+
},
101+
response => {
102+
response[0].short.value = value;
103+
if (value) response[0].data_set[response[0].data_set.length-1][response[0].data_set[0].length-1] = value;
104+
this.recorded_actions[idx] = {
105+
action: response[0].short.action,
106+
data_list: [response[0].short.value],
107+
element: response[0].short.element,
108+
is_disable: false,
109+
name: response[0].name,
110+
value: response[0].short.value,
111+
main: response[0].data_set,
112+
xpath: response[0].xpath,
113+
};
114+
console.log(idx);
115+
console.log(this.recorded_actions);
116+
browserAppData.storage.local.set({
117+
recorded_actions: this.recorded_actions,
118+
})
119+
}
120+
);
121+
}
43122
record(command, target, value, insertBeforeLastCommand, actualFrameLocation) {
44123
let self = this;
124+
if (command == 'doubleClick')
125+
console.log('doubleClick')
126+
if (Object.keys(this.action_name_convert).includes(command)) command = this.action_name_convert[command]
45127
console.log("... Action recorder start");
46128
let signal = {
47129
command: command,
@@ -50,6 +132,34 @@ class Recorder {
50132
insertBeforeLastCommand: insertBeforeLastCommand,
51133
frameLocation: (actualFrameLocation != undefined ) ? actualFrameLocation : this.frameLocation,
52134
};
135+
this.idx += 1;
136+
if (this.recorded_actions.length === 0){
137+
this.recorded_actions[0] = {
138+
action: 'go to link',
139+
data_list: [window.location.href],
140+
element: "",
141+
is_disable: false,
142+
name: `Open ${(window.location.href.length>30) ? window.location.href.slice(0,30) + '...' : window.location.href}`,
143+
value: window.location.href,
144+
main: [['go to link', 'selenium action', window.location.href]],
145+
xpath: "",
146+
};
147+
this.idx += 1;
148+
}
149+
if (this.recorded_actions.length > 0 && this.recorded_actions[0].action != 'go to link'){
150+
this.recorded_actions.unshift({
151+
action: 'go to link',
152+
data_list: [window.location.href],
153+
element: "",
154+
is_disable: false,
155+
name: `Open ${(window.location.href.length>30) ? window.location.href.slice(0,30) + '...' : window.location.href}`,
156+
value: window.location.href,
157+
main: [['go to link', 'selenium action', window.location.href]],
158+
xpath: "",
159+
});
160+
this.idx += 1;
161+
}
162+
this.fetchAIData(target, this.idx-1, command, value)
53163
console.log(signal);
54164
browser.runtime.sendMessage(signal).catch (function(reason) {
55165
console.log(reason);
@@ -58,10 +168,21 @@ class Recorder {
58168

59169
/* attach */
60170
attach() {
171+
61172
console.log('attach2');
62-
if (this.attached) {
63-
return;
64-
}
173+
if (this.attached) return;
174+
browserAppData.storage.local.get('recorded_actions')
175+
.then(res=>{
176+
if(res.recorded_actions){
177+
console.log(res);
178+
this.idx = res.recorded_actions.length;
179+
this.recorded_actions = res.recorded_actions;
180+
}
181+
else{
182+
this.idx = 0;
183+
this.recorded_actions = [];
184+
}
185+
});
65186
this.attached = true;
66187
this.eventListeners = {};
67188
var self = this;
@@ -84,10 +205,18 @@ class Recorder {
84205
}
85206
/*detach */
86207
detach() {
208+
console.log('detach2');
87209
if (!this.attached) {
88210
return;
89211
}
90212
this.attached = false;
213+
browserAppData.storage.local.set({
214+
recorded_actions: this.recorded_actions,
215+
}).then(()=>{
216+
this.idx = 0;
217+
this.recorded_actions = [];
218+
});
219+
91220
for (let event_key in this.eventListeners) {
92221
var event_info = this.parse_the_event_key(event_key);
93222
var event_name = event_info.event_name;

Apps/Web/AI_Recorder/manifest.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
"48": "panel/assets/images/small_logo.png",
1010
"128": "panel/assets/images/small_logo.png"
1111
},
12+
"commands": {
13+
"toggle-xpath": {
14+
"suggested_key": {
15+
"default": "Ctrl+Shift+U",
16+
"mac": "Command+Shift+U"
17+
},
18+
"description": "Toggle plugin"
19+
}
20+
},
1221
"permissions": [
1322
"tabs",
1423
"activeTab",

Apps/Web/AI_Recorder/panel/assets/js/background/play_back.js

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -66,71 +66,75 @@ window.onload = function() {
6666
var logState=true;
6767
var referenceState=false;
6868

69-
recordButton.addEventListener("click", function(){
69+
setTimeout(()=>{ // Add listener after 2 sec
70+
recordButton.addEventListener("click", function(){
7071

71-
/* Custom Function */
72-
clean_panel(); // clean the panel after one record is complate
73-
$('#records-grid').html('<input id="records-count" type="hidden" value="0">');
74-
/* Custom Function */
75-
76-
isRecording = !isRecording;
77-
if (isRecording) {
78-
recorder.attach();
79-
notificationCount = 0;
80-
if (contentWindowId) {
81-
browser.windows.update(contentWindowId, {focused: true});
82-
}
83-
browser.tabs.query({windowId: extCommand.getContentWindowId(), url: "<all_urls>"})
84-
.then(function(tabs) {
85-
for(let tab of tabs) {
86-
browser.tabs.sendMessage(tab.id, {attachRecorder: true});
87-
}
88-
});
89-
90-
recordButton.childNodes[1].textContent = " Stop";
91-
switchRecordButton(false);
92-
93-
$('#play_wrap,#pause_wrap,#resume_wrap,#replay_wrap,#play_all_wrap,#export_wrap,#import_wrap').addClass('disable_action');
94-
95-
}
96-
else {
97-
recorder.detach();
98-
browser.tabs.query({windowId: extCommand.getContentWindowId(), url: "<all_urls>"})
99-
.then(function(tabs) {
100-
for(let tab of tabs) {
101-
browser.tabs.sendMessage(tab.id, {detachRecorder: true});
102-
}
103-
});
104-
105-
recordButton.childNodes[1].textContent = " Record";
106-
switchRecordButton(true);
107-
console.log(777);
108-
$('#play_wrap,#pause_wrap,#resume_wrap,#replay_wrap,#play_all_wrap,#export_wrap,#import_wrap').removeClass('disable_action');
109-
}
110-
})
111-
112-
/* Custom */
113-
recordStopButton.addEventListener("click", function(){
114-
isRecording = !isRecording;
115-
if (!isRecording) {
116-
saveData();
117-
recorder.detach();
118-
browser.tabs.query({windowId: extCommand.getContentWindowId(), url: "<all_urls>"})
119-
.then(function(tabs) {
120-
for(let tab of tabs) {
121-
browser.tabs.sendMessage(tab.id, {detachRecorder: true});
122-
}
123-
});
124-
switchRecordButton(true);
125-
$('#play_wrap,#pause_wrap,#resume_wrap,#replay_wrap,#play_all_wrap,#export_wrap,#import_wrap').removeClass('disable_action');
12672
/* Custom Function */
127-
//clean_panel(); // clean the panel after one record is complate
128-
//$('#records-grid').html('<input id="records-count" type="hidden" value="0">');
73+
clean_panel(); // clean the panel after one record is complate
74+
$('#records-grid').html('<input id="records-count" type="hidden" value="0">');
12975
/* Custom Function */
130-
131-
}
132-
})
133-
76+
77+
isRecording = !isRecording;
78+
isRecording = true;
79+
if (isRecording) {
80+
recorder.attach();
81+
notificationCount = 0;
82+
if (contentWindowId) {
83+
browser.windows.update(contentWindowId, {focused: true});
84+
}
85+
browser.tabs.query({windowId: extCommand.getContentWindowId(), url: "<all_urls>"})
86+
.then(function(tabs) {
87+
for(let tab of tabs) {
88+
browser.tabs.sendMessage(tab.id, {attachRecorder: true});
89+
}
90+
});
91+
92+
recordButton.childNodes[1].textContent = " Stop";
93+
switchRecordButton(false);
94+
95+
$('#play_wrap,#pause_wrap,#resume_wrap,#replay_wrap,#play_all_wrap,#export_wrap,#import_wrap').addClass('disable_action');
96+
97+
}
98+
else {
99+
recorder.detach();
100+
browser.tabs.query({windowId: extCommand.getContentWindowId(), url: "<all_urls>"})
101+
.then(function(tabs) {
102+
for(let tab of tabs) {
103+
browser.tabs.sendMessage(tab.id, {detachRecorder: true});
104+
}
105+
});
106+
107+
recordButton.childNodes[1].textContent = " Record";
108+
switchRecordButton(true);
109+
console.log(777);
110+
$('#play_wrap,#pause_wrap,#resume_wrap,#replay_wrap,#play_all_wrap,#export_wrap,#import_wrap').removeClass('disable_action');
111+
}
112+
})
113+
114+
/* Custom */
115+
recordStopButton.addEventListener("click", function(){
116+
isRecording = !isRecording;
117+
if (!isRecording) {
118+
recorder.detach();
119+
saveData();
120+
browser.tabs.query({windowId: extCommand.getContentWindowId(), url: "<all_urls>"})
121+
.then(function(tabs) {
122+
for(let tab of tabs) {
123+
browser.tabs.sendMessage(tab.id, {detachRecorder: true});
124+
}
125+
});
126+
switchRecordButton(true);
127+
$('#play_wrap,#pause_wrap,#resume_wrap,#replay_wrap,#play_all_wrap,#export_wrap,#import_wrap').removeClass('disable_action');
128+
/* Custom Function */
129+
//clean_panel(); // clean the panel after one record is complate
130+
//$('#records-grid').html('<input id="records-count" type="hidden" value="0">');
131+
/* Custom Function */
132+
133+
}
134+
})
135+
136+
},0)
137+
134138

135139
playButton.addEventListener("click", function() {
136140
if($('#records-count').val() == 0){

0 commit comments

Comments
 (0)