forked from SideeX/sideex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
executable file
·340 lines (299 loc) · 13.1 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright 2017 SideeX committers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/* recording */
var currentRecordingTabId = -1;
var currentRecordingWindowId = -1;
var currentRecordingFrameLocation = "root";
var openedTabNames = new Object();
var openedTabIds = new Object();
var openedWindowIds = new Object();
var openedTabCount = 1;
var selfWindowId = -1;
var contentWindowId;
var notificationCount = 0;
/* playing */
var playingFrameLocations = {};
/* flags */
var isRecording = false;
var isPlaying = false;
var recordEnable = false;
var windowCreateFlag = false;
var tabCreateFlag = false;
var newWindowInfo = { tabId: undefined, windowId: undefined };
function onConnectError(error) {
console.log(`Error : ${error}`);
}
function setRecordEnable(vlaue){
recordEnable = value;
}
browser.tabs.onActivated.addListener(function(activeInfo) {
if (!isRecording) return;
// TODO: block of setTimeout() should only enclose addCommand
setTimeout(function(activeInfo) {
if (currentRecordingTabId === activeInfo.tabId && currentRecordingWindowId === activeInfo.windowId)
return;
// If no command has been recorded, ignore selectWindow command
// until the user has select a starting page to record the commands
if (getRecordsArray().length === 0)
return;
// Ignore all unknown tabs, the activated tab may not derived from
// other opened tabs, or it may managed by other SideeX panels
if (!openedTabIds[activeInfo.tabId])
return;
// Tab information has existed, add selectWindow command
currentRecordingTabId = activeInfo.tabId;
currentRecordingWindowId = activeInfo.windowId;
currentRecordingFrameLocation = "root";
addCommandAuto("selectWindow", [[openedTabIds[activeInfo.tabId]]], "");
}, 150, activeInfo);
})
browser.windows.onFocusChanged.addListener( function(windowId) {
if (!isRecording) return;
if (windowId === browser.windows.WINDOW_ID_NONE) {
// In some Linux window managers, WINDOW_ID_NONE will be listened before switching
// See MDN reference :
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/windows/onFocusChanged
return;
}
// If the activated window is the same as the last, just do nothing
// selectWindow command will be handled by tabs.onActivated listener
// if there also has a event of switching a activated tab
if (currentRecordingWindowId === windowId)
return;
browser.tabs.query({windowId: windowId, active: true})
.then(function(tabs) {
if(tabs.length === 0 || tabs[0].url.substr(0, 13) == 'moz-extension'
|| tabs[0].url.substr(0, 16) == 'chrome-extension') {
return;
}
// The activated tab is not the same as the last
if (tabs[0].id !== currentRecordingTabId) {
// If no command has been recorded, ignore selectWindow command
// until the user has select a starting page to record commands
if (getRecordsArray().length === 0)
return;
// Ignore all unknown tabs, the activated tab may not derived from
// other opened tabs, or it may managed by other SideeX panels
if (!openedTabIds[tabs[0].id])
return;
// Tab information has existed, add selectWindow command
currentRecordingWindowId = windowId;
currentRecordingTabId = tabs[0].id;
currentRecordingFrameLocation = "root";
addCommandAuto("selectWindow", [[openedTabIds[tabs[0].id]]], "");
}
});
});
browser.tabs.onUpdated.addListener(function(tabId, changeInfo, tabInfo) {
if (isRecording && changeInfo.url) {
currentRecordingFrameLocation = "root";
}
if (isPlaying && changeInfo.status == "loading") {
extCommand.setLoading(tabId);
}
if (isPlaying && changeInfo.status == "complete") {
extCommand.setComplete(tabId);
}
});
function handleMessage(message, sender, sendResponse) {
if (message.selectTarget) {
var target = message.target;
// show first locator by default
var locatorString = target[0][0];
var locatorList = document.createElement("datalist");
for (var m = 0; m < message.target.length; ++m) {
var option = document.createElement("option");
option.appendChild(document.createTextNode(message.target[m][0]));
option.innerText = message.target[m][0];
locatorList.appendChild(option);
}
var selectedRecordId = getSelectedRecord();
// If selecting a command, change the target inside.
if (selectedRecordId != "") {
var selectedRecord = document.getElementById(selectedRecordId);
var originalLocatorlist = selectedRecord.getElementsByTagName("td")[1].getElementsByTagName("datalist")[0];
// Update locator data list
originalLocatorlist.innerHTML = escapeHTML(locatorList.innerHTML);
// Update target view, show first locator by default
var adjustedString = adjustTooLongStr(locatorString, getTdShowValueNode(selectedRecord, 1));
var node = getTdShowValueNode(selectedRecord, 1);
if (node.childNodes && node.childNodes[0])
node.removeChild(node.childNodes[0]);
node.appendChild(document.createTextNode(adjustedString));
// Update hidden actual locator value
node = getTdRealValueNode(selectedRecord, 1);
if (node.childNodes && node.childNodes[0])
node.removeChild(node.childNodes[0]);
node.appendChild(document.createTextNode(locatorString));
} else if (document.getElementsByClassName("record-bottom active").length > 0) {
// If selecting a blank command;
addCommandAuto("", target, "");
}
// Update toolbar
document.getElementById("command-target").value = unescapeHtml(locatorString);
document.getElementById("target-dropdown").innerHTML = unescapeHtml(locatorList.innerHTML);
document.getElementById("command-target-list").innerHTML = escapeHTML(locatorList.innerHTML);
return;
}
if (message.cancelSelectTarget) {
var button = document.getElementById("selectElementButton");
isSelecting = false;
button.textContent = "Select";
browser.tabs.sendMessage(sender.tab.id, {selectMode: true, selecting: false});
return;
}
if (isPlaying && message.frameLocation) {
extCommand.setFrame(sender.tab.id, message.frameLocation, sender.frameId);
return;
}
if (!message.command || !isRecording) return;
if (!openedWindowIds[sender.tab.windowId])
return;
if (getRecordsArray().length === 0) {
currentRecordingTabId = sender.tab.id;
currentRecordingWindowId = sender.tab.windowId;
openedTabNames["win_ser_local"] = sender.tab.id;
openedTabIds[sender.tab.id] = "win_ser_local";
addCommandAuto("open", [
[sender.tab.url]
], "");
}
if (!openedTabIds[sender.tab.id])
return;
if (message.frameLocation !== currentRecordingFrameLocation) {
let newFrameLevels = message.frameLocation.split(':');
let oldFrameLevels = currentRecordingFrameLocation.split(':');
while (oldFrameLevels.length > newFrameLevels.length) {
addCommandAuto("selectFrame", [
["relative=parent"]
], "");
oldFrameLevels.pop();
}
while (oldFrameLevels.length != 0 && oldFrameLevels[oldFrameLevels.length - 1] != newFrameLevels[oldFrameLevels.length - 1]) {
addCommandAuto("selectFrame", [
["relative=parent"]
], "");
oldFrameLevels.pop();
}
while (oldFrameLevels.length < newFrameLevels.length) {
addCommandAuto("selectFrame", [
["index=" + newFrameLevels[oldFrameLevels.length]]
], "");
oldFrameLevels.push(newFrameLevels[oldFrameLevels.length]);
}
currentRecordingFrameLocation = message.frameLocation;
}
//Record: doubleClickAt
if (message.command == "doubleClickAt") {
var command = getRecordsArray();
var select = getSelectedRecord();
var length = (select == "") ? getRecordsNum() : select.split("-")[1] - 1;
var equaln = getCommandName(command[length - 1]) == getCommandName(command[length - 2]);
var equalt = getCommandTarget(command[length - 1]) == getCommandTarget(command[length - 2]);
var equalv = getCommandValue(command[length - 1]) == getCommandValue(command[length - 2]);
if (getCommandName(command[length - 1]) == "clickAt" && equaln && equalt && equalv) {
deleteCommand(command[length - 1].id);
deleteCommand(command[length - 2].id);
if (select != "") {
var current = document.getElementById(command[length - 2].id)
current.className += ' selected';
}
}
} else if (message.command.includes("store")) {
// In Google Chrome, window.prompt() must be triggered in
// an actived tabs of front window, so we let panel window been focused
browser.windows.update(selfWindowId, {focused: true})
.then(function() {
// Even if window has been focused, window.prompt() still failed.
// Delay a little time to ensure that status has been updated
setTimeout(function() {
message.value = prompt("Enter the name of the variable");
if (message.insertBeforeLastCommand) {
addCommandBeforeLastCommand(message.command, message.target, message.value);
} else {
notification(message.command, message.target, message.value);
addCommandAuto(message.command, message.target, message.value);
}
}, 100);
})
return;
}
//handle choose ok/cancel confirm
if (message.insertBeforeLastCommand) {
addCommandBeforeLastCommand(message.command, message.target, message.value);
} else {
notification(message.command, message.target, message.value);
addCommandAuto(message.command, message.target, message.value);
}
}
browser.tabs.onRemoved.addListener(function(tabId, removeInfo) {
if (!isRecording) return;
if (openedTabIds[tabId] && tabId === openedTabNames[openedTabIds[tabId]]) {
if (currentRecordingTabId !== tabId) {
addCommandAuto("selectWindow", [
[openedTabIds[tabId]]
], "");
addCommandAuto("close", [
[openedTabIds[tabId]]
], "");
addCommandAuto("selectWindow", [
[openedTabIds[currentRecordingTabId]]
]);
} else {
addCommandAuto("close", [
[openedTabIds[tabId]]
], "");
}
delete openedTabNames[openedTabIds[tabId]];
delete openedTabIds[tabId];
currentRecordingFrameLocation = "root";
}
});
browser.runtime.onMessage.addListener(handleMessage);
browser.webNavigation.onCreatedNavigationTarget.addListener(function(details) {
if (isRecording && openedTabIds[details.sourceTabId]) {
openedTabNames["win_ser_" + openedTabCount] = details.tabId;
openedTabIds[details.tabId] = "win_ser_" + openedTabCount;
openedWindowIds[details.windowId] = true;
openedTabCount++;
}
if (isPlaying && extCommand.hasTab(details.sourceTabId))
extCommand.setNewTab(details.tabId);
});
browser.runtime.onMessage.addListener(function contentWindowIdListener(message) {
if (message.selfWindowId != undefined && message.commWindowId != undefined) {
selfWindowId = message.selfWindowId;
contentWindowId = message.commWindowId;
extCommand.setContentWindowId(contentWindowId);
openedWindowIds[message.commWindowId] = true;
browser.runtime.onMessage.removeListener(contentWindowIdListener);
}
})
function notification(command, target, value) {
let tempCount = String(notificationCount);
notificationCount++;
// In Chrome, notification.create must have "iconUrl" key in notificationOptions
browser.notifications.create(tempCount, {
"type": "basic",
"iconUrl": "/icons/icons-48.png",
"title": "Record command!",
"message": "command: " + String(command) + "\ntarget: " + String(target[0][0]) + "\nvalue: " + String(value)
});
setTimeout(function() {
browser.notifications.clear(tempCount);
}, 1500);
}